와이유스토리

[문자열] 프로그래머스 후보키 Python 본문

코딩테스트/문자열

[문자열] 프로그래머스 후보키 Python

유(YOO) 2022. 3. 3. 21:35

 

https://programmers.co.kr/learn/courses/30/lessons/42890

 

코딩테스트 연습 - 후보키

[["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] 2

programmers.co.kr

from itertools import combinations

def solution(relation):
    row = len(relation)
    col = len(relation[0])
    
    comb = []
    for s in range(1, col+1):
        comb+=combinations(range(col), s) # 열 조합 생성

    # 유일성
    li = []
    for c in comb:
        temp = [tuple([item[i] for i in c]) for item in relation] # 중요
        if len(set(temp)) == row:
            li.append(c) # + = append != join != extend
    
    # 최소성
    answer = set(li) # {}
    for i in range(len(answer)):
        for j in range(i+1, len(li)):
            if len(li[i]) == len(set(li[i]) & set(li[j])): # 교집합
                answer.discard(li[j])

    return len(answer)
from itertools import combinations

def solution(relation):
    row = len(relation)
    col = len(relation[0])
    
    comb = []
    for s in range(1, col+1):
        comb+=combinations(range(col), s) # 열 조합 생성

    # 유일성
    li = []
    for c in comb:
        temp = []
        for item in relation:
            a = []
            for i in c:
                a.append(item[i])
            temp.append(tuple(a))
        if len(set(temp)) == row:
            li.append(c) # append != join != extend
    
    # 최소성
    answer = set(li) # {}
    for i in range(len(answer)):
        for j in range(i+1, len(li)):
            if len(li[i]) == len(set(li[i]) & set(li[j])): # 교집합
                answer.discard(li[j])

    return len(answer)
Comments