title: "[프로그래머스] 후보키 Python 파이썬 해설 (Level 2) - 이도훈"
cleanUrl: "programmers/42890"
description: "프로그래머스 Level 2 문제 [후보키]의 풀이를 정리합니다."
import itertools
def powerset(x):
return itertools.chain(*[itertools.combinations(x, i) for i in range(1, len(x) + 1)])
def solution(relation):
n = len(relation)
candidates = []
col_idxs = list(range(len(relation[0])))
for cols in powerset(col_idxs):
items = set(tuple(r[col] for col in cols) for r in relation)
if len(items) == n and not any(x in candidates for x in powerset(cols)):
candidates.append(cols)
return len(candidates)
프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/challenges