카카오 2019 후보키

김준오·2021년 8월 30일
0

알고리즘

목록 보기
50/91
post-thumbnail

문제

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

풀이

from itertools import combinations

def solution(relation):
    global answer
    answer = 0
    
    len_relation = len(relation[0])
    global check_list
    check_list = {}
    
    for i in range(1,len_relation+1):
        kk = [i for i in range(1,len_relation+1)]
        
        temp = list(combinations(kk,i))
        
        for k in temp:
            check_list[k] = True

    def listCheck(x):
        set_x = set(x)
        for i in check_list:
            if len(set_x - set(i)) == 0:
                check_list[i] = False
                
    def sameCheck(x):
        global answer
        temp = []
        checkk = True
        for i in relation:
            temp2 = []
            for j in x:
                temp2.append(i[j-1])
            
            if temp2 not in temp:
                temp.append(temp2)
                # temp[temp2] = 1
            
            else :
                checkk = False
                break
        
        if checkk == True:
            answer += 1
            listCheck(x)
                
    for i in check_list:
        if check_list[i] == False:
            continue
            
        sameCheck(i)
            
    return answer

공부한것

변수 이름을 너무 막지었더니 가독성이 안좋다
이름도 좀 적절하게 짓는걸 연습해야겠다

튜플

  • 1개짜리 튜플은 무조건 (1,) 이런식으로 콤마가 붙는다는걸 알았다

  • 처음에 잘못 생각했던 부분이 (1,2) 는 (1,2,3) 에 포함된다고 생각을해서 in으로 처리하면 True가 나올것이라 생각했다. 당연히 아니다

  • 튜플과 list 등 모두 set으로 변환 가능하다.a = (1,2,3) -> set(a) 이런식

포함되는지 체크하는 부분을 set으로 차집합 해서 처리했다

def listCheck(x):
        set_x = set(x)
        for i in check_list:
            if len(set_x - set(i)) == 0:
                check_list[i] = False

요부분

끝.

profile
jooooon

0개의 댓글

관련 채용 정보