๋ด์ฝ๋๋ ์๋๋ฐ ๋๋ณด๋ค ๋ ์์ง์ ๊ฐ์ ธ์ด(์๋, ๊ฐ๋ ์ฑ)
def visit(node, graph, visited):
visited[node] = 1
for child in range(len(graph[node])):
if not visited[child] and graph[node][child] == 1:
visit(child, graph, visited)
def solution(n, computers):
visited = [0] * n
answer = 0
for node in range(n):
if not visited[node]:
visit(node, computers, visited)
answer += 1
if sum(visited) == n:
break
return answer
from collections import defaultdict
from itertools import product
def solution(N, number):
if N == number : return 1
N_combinations = defaultdict(set)
calculations = ['+', '-', '*', '/']
for n in range(1, 9) :
N_combinations[n].add(int(str(N)*n))
for i in range(1, n) :
for x, sign, y in product(N_combinations[i], calculations, N_combinations[n-i]) :
if sign == '/' and y == 0 : continue
res = eval(str(x)+sign+str(y))
N_combinations[n].add(res)
if number in N_combinations[n] :
return n
return -1
from functools import reduce
def solution(n):
return reduce(lambda x, n :[x[1], x[0] + x[1]], range(n), [0, 1])[-1] % 1000000007
import pandas as pd
from itertools import chain, combinations
def key_options(items):
return chain.from_iterable(combinations(items, r) for r in range(1, len(items)+1) )
def solution(relation):
Candidate = []
df = pd.DataFrame(data = relation)
for candidate in key_options(list(df)):
deduped = df.drop_duplicates(candidate)
if len(deduped.index) == len(df.index):
Candidate.append(set(candidate))
k = 0
while k <len(Candidate) :
for i in Candidate[k+1:] :
if Candidate[k].issubset(i) :
Candidate.remove(i)
k += 1
return len(Candidate)
def solution(record):
table = {}
room = []
answer = []
for i in record :
tmp = i.split(' ')
if len(tmp) == 3 :
action, ID, name = tmp
table[ID] = name
else :
action, ID = tmp
room.append([ID, action])
for ID, action in room :
if action == 'Change' :
continue
if action == 'Enter' :
answer.append('{}๋์ด ๋ค์ด์์ต๋๋ค.'.format(table[ID]))
else :
answer.append('{}๋์ด ๋๊ฐ์ต๋๋ค.'.format(table[ID]))
return answer
table
์ key
๊ฐ๋ง ๋ฐ๋ณต๋ฌธ ๋๋ฆฌ๋ฉด์ ๋ฐ๊ฟ์ฃผ๋ฉด ๋๋ค. ์ด๊ฒ๋ ๋๋ฌด ์ฌ์์ ๋ฑํ ์ค๋ช
์ ์๋ค. ๊ทธ๋๋ก ํ๋ฉด๋๋ค.
def solution(n,a,b):
return ((a-1)^(b-1)).bit_length()
์ด๋ฌธ์ 1์งฑ์ ์ฝ๋.
def solution(n, words):
for i in range(1, len(words)) :
if words[i] in words[:i] or words[i-1][-1] != words[i][0]:
q, r = divmod(i, n)
return [r+1, q+1]
return [0, 0]
๋๋ง์๊ธฐ ๊ท์น์ ์ด๊ธด ๋จ์ด์ index
๋ฅผ n
์ผ๋ก ๋๋์์ ๋์ ๋ชซ, ๋๋จธ์ง๋ก ๋๊ฐ ๋ช๋ฒ์งธ์ ํ๋ ธ๋์ง๋ฅผ ์ ์ ์์ต๋๋ค.
from itertools import combinations
def prime_number(x):
answer = 0
for i in range(1,int(x**0.5)+1):
if x%i==0:
answer+=1
return 1 if answer==1 else 0
def solution(nums):
return sum(prime_number(sum(c)) for c in combinations(nums,3))
import re
def solution(files):
file_dict = {}
for file in files :
digit = re.findall('\d+', file)[0]
file_dict[file] = [file[:file.find(digit)].lower(), int(digit)]
return [x[0] for x in sorted(file_dict.items(), key = lambda x : (x[1][0], x[1][1]))]