import random
import string
import csv
import datetime
import calendar
random_numbers = set()
for _ in range(120):
res = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(16))
random_numbers.add(res)
def add_months(sourcedate, months) -> datetime.date:
month = sourcedate.month - 1 + months
year = sourcedate.year + month // 12
month = month % 12 + 1
return datetime.date(year,month,1)
somedate = datetime.date(2018, 4, 1)
with open('random_number.txt', 'w', newline='\n') as f:
w = csv.writer(f, delimiter=' ', quoting=csv.QUOTE_NONE)
for idx, num in enumerate(random_numbers):
w.writerow([add_months(somedate, idx).strftime('%Y-%m'), num])
# print(len(random_numbers))
2018년 10월 29일 월요일
[python] 월별로 난수 생성하기
파이썬에는 아쉬운게 timedelta에 month를 더하는 것이 없다. 다른 유틸을 받아서 사용하면 된다고 하는데 이렇게 해도 잘 되는 것 같다.
[codility][python] max counter
codility max counter
https://app.codility.com/demo/results/trainingUBUCBJ-RVE/def solution(N, A):
res = [0] * N
max_counter = 0
for a in A:
if a <= N: # N카운터 존재
res[a-1] +=1 # 카운터 올림
if max_counter < res[a-1]: # max_counter 보다 크면 현재 카운터를 tmp_max로 변경
max_counter = res[a-1]
else: # max counter 실행
for i in enumerate(res): # 전부 max_counter로 덮어씌운다.
res[i] = max_counter
return res
빅오노테이션이 O(MxN) 인 관계로 이중포문에서 하나를 없애야 한다.
# N 카운터가 있음. 0으로 세팅
# max counter : 모든 카운터들이 아무 카운터의 최대 값으로 세팅됨.
# A[K] = X 이면, K오퍼레이션은 X를 증가시킨다.
# -> 값이 X면 X를 증가시킴 (X범위 1<=X<=N)
# A[K] = N + 1 이면 K는 max counter (??? K가 N보다 크면 max??)
# 아아아 max counter라는 걸 실행함!!!
def solution(N, A):
res = [0] * N
max_counter = 0
tmp_max = 0
for a in A:
if a <= N: # N카운터 존재
if max_counter > res[a-1]: # 1 max_counter가 더 크면 max_counter가 갱신된 것
res[a-1] = max_counter # 2 max_counter 값으로 변경해준다.
res[a-1] +=1 # 3 그리고 그 다음에 카운터 시킨다
if tmp_max < res[a-1]: # 4 tmp_max보다 크면 현재 카운터를 tmp_max로 변경
tmp_max = res[a-1]
else: # max_counter 에 현재 tmp_max를 넣는다. (max_counter는 그때그때 실행할 것이다.)
max_counter = tmp_max
# 아직 max_counter가 변경 된 이후에 counter가 변경된 이력이 없으면 max_counter로 덮어씌워준다.
# 물론 max_counter보다 작은 경우.
for idx, _ in enumerate(res):
if (res[idx] < max_counter):
res[idx] = max_counter
return res
a = [3,4,4,6,1,4,4]
N = 5
print(solution(5, a))
[hackerrank][python]Journey to the Moon
hackerrank: Journey to the Moon
1st attempt
# Determine how many pairs of astronauts from different countries they can choose from.
# UN 연합은 달에 사람들을 보내려고 한다.
# 그들은 이 사람들이 서로 다른 나라였으면 한다.
# 당신은 두 쌍의 우주조종사 id 리스트를 받을 것이다.
# 각 쌍은 같은 나라 우주조종사 id의 두 쌍으로 이루어져 있다.
# - 얼마나 많은 조종사 쌍을 만들 수 있는지 말해라.
# n : number of astronaut
# p : the number of pairs
from collections import defaultdict
# 일단 연결된 것들을 만든다.
def dfs(graph, start):
visited, stack = set(), [start]
while stack:
v = stack.pop()
visited.add(v)
# 해당 정점에 연결된 것들을 stack에 넣는다.
# 방문한 것은 제외
stack.extend(graph[v] - visited)
# 연결된 것들을 리턴.
return visited
def journeyToMoon(n, ast_pairs):
graph = defaultdict(set)
asts = set()
# 그래프 연결...
for a1, a2 in ast_pairs:
graph[a1].add(a2)
graph[a2].add(a1)
asts.add(a1)
asts.add(a2)
# 고립된 애들이 있을 수 있나? 일단 받아놔... (있는듯...)
iso_asts = set([i for i in range(n)]) - asts
groups = [] # divied by contry
while asts:
ast = asts.pop()
connected_asts = dfs(graph, ast)
groups.append(connected_asts)
# 한번 방문한 팀은 다시 방문하지 않는다.
asts = asts - connected_asts
for i in iso_asts:
groups.append({i})
idx1, idx2 = 0, 1
group_sum = 0
while idx1 < len(groups)-1:
idx2 = idx1 + 1
while idx2 < len(groups):
group_sum += len(groups[idx1]) * len(groups[idx2])
idx2 += 1
idx1 += 1
return group_sum
n, p = list(map(int, input().strip().split(' ')))
ast_pairs = []
for _ in range(p):
astronaut = [int(a) for a in input().strip().split(' ')]
ast_pairs.append(astronaut)
result = journeyToMoon(n, ast_pairs)
print(result)
def dfs(graph, start):
visited, stack = set(), [start]
while stack:
v = stack.pop()
visited.add(v)
stack.extend(graph[v] - visited)
return visited
def journeyToMoon(n, ast_pairs):
graph = defaultdict(set)
asts = set()
for a1, a2 in ast_pairs:
graph[a1].add(a2)
graph[a2].add(a1)
asts.add(a1)
asts.add(a2)
iso_asts = set([i for i in range(n)]) - asts
groups = [] # divied by contry
while asts:
ast = asts.pop()
connected_asts = dfs(graph, ast)
groups.append(connected_asts)
# 한번 방문한 팀은 다시 방문하지 않는다.
asts = asts - connected_asts
for i in iso_asts:
groups.append({i})
# 뒤에 추가된 것들을 따로 분리해야 한다. 그래야 추가된 녀석들이 만드는
# 패턴을 알 수 있다.
# A*B + A*C + B*C -- A*B + A*C + B*C -- A*B + (A+B)*C
# A*B + A*C + A*D + B*C + B*D + C*D
# - A*B + (A+B)*C (A+B+C)*D
# A*B + A*C + A*D + A*E + B*C + B*D + B*E + C*D + C*E + D*E
# - A*B + (A+B)*C + (A+B+C)*D + (A+B+C+D)*E
idx1, idx2 = 0, 1
group_sum = 0
tmp_sum = 0
while idx1 < len(groups):
group_sum += tmp_sum*len(groups[idx1])
tmp_sum += len(groups[idx1])
# group_sum += tmp_sum*(len(groups[idx1 + 1]))
idx1 += 1
return group_sum
n, p = list(map(int, input().strip().split(' ')))
ast_pairs = []
for _ in range(p):
astronaut = [int(a) for a in input().strip().split(' ')]
ast_pairs.append(astronaut)
result = journeyToMoon(n, ast_pairs)
print(result)
[javascript] 접근자 defineProperties를 이용한 생성자 설정.
function Person(name) {
this.name = name;
}
Person.prototype = {
yell: function() {
console.log("my name is " + this.name);
}
}
/* 수정불가 */
var user = Object.create(Person.prototype, {
name: {
value: 'new name'
}
})
/* 수정가능 */
user = Object.create(Person.prototype, {
name : {
value: 'new name2',
configurable: true,
enumerable: true,
writable: true
}
})
/* 접근자 활용의 예 */
Object.defineProperties(user, {
firstName: {
value: "Younghwan",
writable: true
},
lastName: {
value: "Yang",
writable: true
},
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function(value) {
var res = value.split(' ');
if (res.length > 1) {
this.firstName = res[0];
this.lastName = res[1];
} else {
console.log('wrong format');
}
}
});
console.log(user.fullName);
user.fullName = "Hello world!";
console.log(user.firstName); // Hello
console.log(user.lastName); // World!
피드 구독하기:
덧글 (Atom)