๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ›„๋ณดํ‚ค

๐ŸŽฎinspirer9 2023. 2. 22. 14:13
728x90
๋ฐ˜์‘ํ˜•

ํ’€์ด

  • ํ›„๋ณดํ‚ค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ค‘๋ณต์ด ์—†๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๋ฌธ์ œ.
    • ์กฐํ•ฉ๋งŒ๋“œ๋Š” ๊ฑด
      • for๋ฌธ์„... for๋ฌธ ๋ง๊ณ  ์ข€ ํŽธํ•œ๊ฑฐ ์—†๋‚˜? ์กฐํ•ฉ ๋งŒ๋“ค์–ด์•ผ ๋˜๋„ค. 4+3+2+1...
      • ์•„! 1์€ ํ•„์š”์—†๋‹ค. ๋‹ค ๋“ค์–ด์žˆ๋Š”๊ฑฐ๋ผ์„œ... 4+3+2
    • ๊ทธ๋ฆฌ๊ณ  ์ค‘๋ณต ์ฒดํฌ๋Š” ์นด์šดํ„ฐ ์“ฐ๋ฉด ๋  ๋“ฏ?
      • ์†๋„๋Š” ์ข€ ํฌ๊ธฐํ•˜๊ณ  ํŽธํ•˜๊ฒŒ ๊ฐ€์ž...
  • ๋ญ˜ ์จ์•ผํ•˜๋‚˜? ์ผ๋‹จ itertools๊ฐ€ ์ƒ๊ฐ๋‚ฌ๋‹ค.
 

itertools — Functions creating iterators for efficient looping

This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set...

docs.python.org

  • ์ฝค๋น„๋„ค์ด์…˜ ์“ฐ๋ฉด ๋˜์ง€?
    • ์•„๋‹ˆ๋‹ค. ๊ฑฐ๊พธ๋กœ ์กฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ์ž–์•„?
    • ์•„๋‹Œ๊ฐ€? ์ •๋ฐฉํ–ฅ ์กฐํ•ฉํ•ด์„œ ์ค‘๋ณต์ด๋ฉด ์—ญ๋ฐฉํ–ฅ ์กฐํ•ฉํ•ด๋„ ์ค‘๋ณต์ด์ž–์•„?
    • ๊ทธ๋ƒฅ ์ฝค๋น„๋„ค์ด์…˜์ด ๋งž๋„ค?
product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2) AB AC AD BC BD CD
combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
  • ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š”

  • ์ด๋ ‡๊ฒŒ ์„ž์–ด์•ผ ํ•˜๋Š”๋ฐ...?
    • ์‘? 9๊ฐœ ๋„˜๋„ค.
for i in range(1,4):
    a = combinations(["ํ•™๋ฒˆ","์ด๋ฆ„","์ „๊ณต","ํ•™๋…„"], i)
    for aa in a:
        print(aa)
            
์ถœ๋ ฅ ใ€‰
('ํ•™๋ฒˆ',)
('์ด๋ฆ„',)
('์ „๊ณต',)
('ํ•™๋…„',)
('ํ•™๋ฒˆ', '์ด๋ฆ„')
('ํ•™๋ฒˆ', '์ „๊ณต')
('ํ•™๋ฒˆ', 'ํ•™๋…„')
('์ด๋ฆ„', '์ „๊ณต')
('์ด๋ฆ„', 'ํ•™๋…„')
('์ „๊ณต', 'ํ•™๋…„')
('ํ•™๋ฒˆ', '์ด๋ฆ„', '์ „๊ณต')
('ํ•™๋ฒˆ', '์ด๋ฆ„', 'ํ•™๋…„')
('ํ•™๋ฒˆ', '์ „๊ณต', 'ํ•™๋…„')
('์ด๋ฆ„', '์ „๊ณต', 'ํ•™๋…„')
  • 4๊ฐœ ์ค‘์— 1๊ฐœ ๋ฝ‘์•„ : 4๊ฐœ = 4C1 = 4P1 / 1P1
  • 4๊ฐœ ์ค‘์— 2๊ฐœ ๋ฝ‘์•„(์ค‘๋ณต์—†์ด) : 6๊ฐœ = 4C2 = 4P2 / 2P2
  • 4๊ฐœ ์ค‘์— 3๊ฐœ ๋ฝ‘์•„(์ค‘๋ณต์—†์ด) : 4๊ฐœ = 4C3 = 4P2 / 3P3

  • ํ•˜... ๋‚˜ ์ˆ˜ํฌ์ž๋ผ ์ˆ˜ํ•™ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์—‰.
  • ใ…‹ใ…‹ใ…‹
from itertools import combinations

def solution(relation):
    answer = 0
    C = len(relation[0])

    for i in range(1, C):
        a = combinations([i for i in range(C)], i)
        for aa in a:
            print(aa)
    
    return answer
    
์ถœ๋ ฅ ใ€‰
(0,)
(1,)
(2,)
(3,)
(0, 1)
(0, 2)
(0, 3)
(1, 2)
(1, 3)
(2, 3)
(0, 1, 2)
(0, 1, 3)
(0, 2, 3)
(1, 2, 3)
  • ์ž! ์ด์ œ ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•˜์ง€!
    • ๋•ก!
from itertools import combinations

def solution(relation):
    answer = 0
    C = len(relation[0])

    
    for i in range(1, C):
        a = combinations([i for i in range(C)], i)
        for aa in a:
            _txt_array = []
            for l in range(len(relation)):
                _txt = ""
                for aaa in aa:
                    _txt += relation[l][aaa]
                _txt_array.append(_txt)
            len_source = len(_txt_array)
            len_target = len(list(set(_txt_array)))
            if len_source == len_target:
                answer += 1
    return answer
    

ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[["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
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ 9์ด ๊ธฐ๋Œ“๊ฐ’ 2๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  • ์ด์ƒํ•˜๋‹ค .ใ…ก.ใ…ก.
    • ์•„. 1๊ฐœ ์งœ๋ฆฌ๋กœ ์„ฑ๊ณตํ–ˆ์œผ๋ฉด ๊ทธ๊ฑด ์ง€์›Œ์•ผ ํ•˜๋Š”๊ตฌ๋‚˜?

  • ์„ฑ๊ณตํ•œ ์กฐํ•ฉ์„ ๋บ„๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ...
    • ์ฝค๋น„๋„ค์ด์…˜์—๋Š” remove( ) ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค.
  • ๋จธ๋ฆฌ๊ฐ€ ๋Œ์ด๋ผ์„œ ํ•˜๋ฃจ ์ง€๋‚ฌ๋‹ค. ใ…‹ใ…‹ใ…‹
    • ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค.
    • ํ•„๋“œ ์ž์ฒด๊ฐ€ ์ค‘๋ณต์ผ ๊ฒฝ์šฐ...

  • ์ด๋ ‡๊ฒŒ ํ•„๋“œ ์ž์ฒด๊ฐ€ ์ค‘๋ณต์ด๋ฉด... ํ›„๋ณดํ‚ค๋ฅผ ์•„์˜ˆ ๋ชป ๋งŒ๋“ค์ง€?
    • ๊ทผ๋ฐ ๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ํ•„๋“œ๋ฅผ ํ†ต์œผ๋กœ ์ง€์›Œ๋ฒ„๋ ค๋„ ๋˜๋Š”๋ฐ...
      • ์ด๊ฑธ ํ™•์ธํ•ด์„œ ์ง€์šฐ๋ฉด ์†๋„๊ฐ€ ์ข€ ๋นจ๋ผ์ง€๋ ค๋‚˜?
        • ํ•˜์ง€๋งŒ ์ด๋ฏธ ์ง€๊ธˆ ๋น ๋ฅธ๊ฑธ...ใ…ก.ใ…ก;
        • ์–ด์ฐจํ”ผ ๊ธธ์ด๋„, 8, 20, 8์ด๋ผ์„œ ๋‹ค ๊ณฑํ•ด๋ดค์ž 1280๋ฐ–์— ์•ˆ๋จ.
  • ์•„. ์ด๊ฑด๊ฐ€?!
    • "ํ›„๋ณด ํ‚ค์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ."
      • ์ค‘๋ณต์„ ์ง€์šฐ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ ์ค‘๋ณต์„ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ๋งŽ๋“ค์–ด์„œ... ์ตœ์†Œ์„ฑ์„ ๋‚˜์ค‘์— ์ฒดํฌํ•˜๋Š”๊ฑด๊ฐ€!?
      • ์™ ์ง€ ๊ทธ๋Ÿฐ ๋“ฏ... ๊ทธ๋Ÿฌ๋ฉด answer+=1์ด ์•„๋‹ˆ๋ผ ์ค‘๋ณต๊นŒ์ง€ ์ „๋ถ€ ๋ฆฌ์ŠคํŠธ์— ๋•Œ๋ ค๋„ฃ๊ณ .
        • ๊ทธ ๋ฆฌ์ŠคํŠธ๋ฅผ... ์–ด๋–ป๊ฒŒ ํ•˜์ง€? ์ผ๋‹จ ๋ฆฌ์ŠคํŠธ ๋ถ€ํ„ฐ ๋งŒ๋“ค์ž!
        • ์–ด์ฉ์ง€... 1280๊ฐœ ๋ฐ–์— ์•ˆ๋˜๋”๋ผ๋‹ˆ... ๊ทธ๋Ÿด๋ฆฌ๊ฐ€ ์—†์ง€. ์ณ‡.
  • ์ฝ”๋“œ ์ˆ˜์ •... ๋จผ์ € ์กฐํ•ฉ์„ ๋‹ค ๋งŒ๋“ค์–ด์„œ ๋ฐฐ์—ด์— ๋„ฃ๊ณ 

  • ๋ฐฐ์—ด ์•ˆ์— ์ค‘๋ณต์ด ์žˆ์œผ๋ฉด ์ œ๊ฑฐํ•˜๊ณ .
    • if len(_tmp) == len(list(set(_tmp))): ์œผ๋กœ
    • ์ด๋Ÿฌ๋ฉด ๋‚ด๊ฐ€ ๋งŒ๋“  ์ค‘๋ณต 100%์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์—ฌ๊ธฐ์„œ ๊ฑธ๋Ÿฌ์ง€๊ณ ,

  • ๋งŒ๋“ค์–ด์ง„ ์กฐํ•ฉ(Candidate Key) ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ํ‚ค๋ฅผ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ๋Š”... ์–ด๋–ป๊ฒŒ ์ฐพ์•„ใ…‹
    • ...๋Š” Candidate Key์—์„œ ์ค‘๋ณตํ–ˆ๋˜ ์• ๋“ค ์ง€์šธ ๋•Œ, Comb ๋ฐฐ์—ด์—์„œ๋„ ์กฐํ•ฉ์„ ์ง€์›Œ๋ฒ„๋ฆฌ๋ฉด ๋˜๊ฒ ๋‹ค.
    • ๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ๋‚จ๊ณ ... ์—ฌ๊ธฐ์„œ ์ค‘๋ณต๋œ ์›์†Œ ๋นผ๊ณ  ์ œ์ผ ๋งŽ์ด ์‚ด์•„๋‚จ๋Š”๊ฑธ ์ฐพ์œผ๋ฉด ๋˜๋Š”๋ฐ...

  • ์—ฌ๊ธฐ์„œ ๋ฉˆ์ถค ใ…‹ใ…‹ใ…‹
    • (0,), (0, 1), (0, 2), (0, 3), (1, 2), (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)
      • ์—ฌ๊ธฐ์„œ... (0)์„ ์„ ํƒํ•˜๋ฉด, 6๊ฐœ ๋ชป ์“ฐ๊ณ ,
      • (0,1)์„ ์„ ํƒํ•˜๋ฉด, 0๊ณผ 1์„ ๋ชป ์“ด๋‹ค.
        • ๊ทผ๋ฐ... ์ด๊ฒŒ ๋งž๋‚˜?
        • ๊ธธ์ด๊ฐ€ ์งง์€ ์• ๋“ค๋ถ€ํ„ฐ... ์ฒ˜๋ฆฌ?
          • ์กฐํ•ฉ์ž์ฒด๊ฐ€ ๊ธธ์ด๊ฐ€ ์งง์€ ์• ๋“ค๋ถ€ํ„ฐ ๋˜์–ด ์žˆ๊ธด ํ•จ.
            • ์ง€์ ธ๋ถ„ ํ•˜์ง€๋งŒ, ์ ์‹ฌ ๋นจ๋ฆฌ ๋จน๊ณ  30๋ถ„ ๋™์•ˆ ์ƒ๊ฐํ•œ ๊ฑธ๋กœ ์งœ๋ด„.
from itertools import combinations

def solution(relation):
    answer = 0
    Field = [i for i in range(len(relation[0]))]
    
    Comb = [] # ํ•„๋“œ ์ธ๋ฑ์Šค๋กœ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
    for i in range(1, len(Field)):
        Comb += list(combinations(Field, i))
    
    CandiKey = [] # ์‹ค์ œ ํ›„๋ณดํ‚ค ๋งŒ๋“ค๊ธฐ
    for c in Comb:
        _tmp = []
        for r in range(len(relation)):
            _txt = ""
            for cc in c:
                _txt += relation[r][cc]
            _tmp.append(_txt)
        if len(_tmp) == len(list(set(_tmp))):
            CandiKey.append(_tmp)
        else:
            Comb.remove(c)
            Comb.insert(0,[])
            
    Comb = [c for c in Comb if len(c) > 0] # remove ์“ธ๋•Œ ์ธ๋ฑ์Šค ์•ˆ๋ฐ”๋€Œ๊ฒŒ ํ•˜๋ ค๊ณ  insertํ•œ ๋นˆ์นธ ์ง€์šฐ๊ธฐ
    
    print(answer, Comb)
    while len(Comb) > 0:
        x = Comb.pop(0)
        answer += 1
        for xx in x:
            for i in range(len(Comb)):
                if xx in Comb[i]:
                    Comb[i] = []
        Comb = [c for c in Comb if len(c) > 0]
        print(answer, Comb)
        
    return answer
  • ํ…Œ์ผ€ ์ž˜ ๋”. ์•„์นจ์—๋„ ํ…Œ์ผ€๋Š” ์ž˜ ๋”. ๊ฑฑ์ •
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[["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
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 [(0,), (0, 1), (0, 2), (0, 3), (1, 2), (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
1 [(1, 2), (1, 2, 3)]
2 []

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	[["100", "ryan", "music", "2"], ["100", "ryan", "music", "2"], ["200", "apeach", "math", "2"], ["300", "tube", "computer", "3"], ["400", "con", "computer", "4"], ["500", "muzi", "music", "3"], ["600", "apeach", "music", "2"]]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 []
  • ์‹คํŒจํ•จ.
    • ใ…‹ใ…‹ใ…‹ ์•„๋†”
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (2.86ms, 10.4MB)
  • ์ด๊ฑฐ... ์ธ๋ฑ์Šค ์กฐํ•ฉ์œผ๋กœ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ํ›„๋ณดํ‚ค ๋ผ๋ฆฌ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š”๊ฑด๊ฐ€?
    • ์•„... ํ˜น์‹œ ์ˆœ์„œ๊ฐ€? ๊ทธ๋ ‡๊ตฌ๋‚˜!
      • (0)์€ ํ•˜๋‚˜๋‹ˆ๊นŒ ๊ทธ๋ƒฅ ์ญˆ์šฑ ๋น„๊ตํ•˜๋ฉด์„œ ์ง€์šฐ๋ฉด ๋˜๋Š”๊ฑฐ๊ตฌ...
      • (1,2)(1,2,3)์€, (1,2,3)์— (1,2)๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์„œ... ใ… .ใ… ;
        • ์‹คํŒจ
    Comb = [c for c in Comb if len(c) > 0] # remove ์“ธ๋•Œ ์ธ๋ฑ์Šค ์•ˆ๋ฐ”๋€Œ๊ฒŒ ํ•˜๋ ค๊ณ  insertํ•œ ๋นˆ์นธ ์ง€์šฐ๊ธฐ
    Final_Comb = []
    while len(Comb) > 0:
        answer += 1
        x = Comb.pop(0)
        Final_Comb.append(x)
        xlen = len(x)
        for ci in range(len(Comb)):
            clen = len(Comb[ci])
            for i in range(clen-xlen+1):
                is_subset = True
                #rint(x, Comb[ci])
                for xi in range(xlen):
                    if x[xi] != Comb[ci][i+xi]:
                        #rint(x[xi], Comb[ci][i+xi], end=" ")
                        is_subset = False
                        break
                if is_subset == True:
                    #rint("์‚ญ์ œ")
                    Comb[ci] = []
                    break
                #rint("๋ฉ”๋กฑ")
        Comb = [c for c in Comb if len(c) > 0]
        print(answer, x, Comb)
    print(Final_Comb)
  • print๋ฌธ ๋•์ง€ ๋•์ง€ ๋ถ™์—ฌ์„œ ๊ณ„์† ๋ณด๋‹ค๋ณด๋‹ˆ, 
    • ์•„... (0,2)๋ž‘ (0,2,4)๊ฐ€ ์žˆ์œผ๋ฉด (0,2,4)๋„ ์•ˆ๋˜๊ณ , (0,1,2)๋„ ์•ˆ๋˜๋Š”๊ฑฐ๊ตฌ๋‚˜?
      • ์ด๋Ÿฌ๋ฉด ์ˆœ์„œ๋Œ€๋กœ ๋น„๊ตํ•  ํ•„์š”์—†์ด...? ๊ทธ๋ƒฅ ๋นผ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€?
    • ๊ฒฐ๊ตญ ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ด๋ฅผ ๋ชป ํ–ˆ๋‹ค๋Š” ๊ฑธ๋กœ ๊ฒฐ๋ก .
      • ์ด๊ฒƒ๋„ ์‹คํŒจ.... ๋„ˆ๋ฌดํ•˜๋„ค. ๋ฌธ์ œ๋‚˜ ๋‹ค์‹œ ์ฝ์–ด๋ณด์ž.
from itertools import combinations

def solution(relation):
    answer = 0
    Field = [i for i in range(len(relation[0]))]
    
    Comb = [] # ํ•„๋“œ ์ธ๋ฑ์Šค๋กœ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
    for i in range(1, len(Field)):
        Comb += list(combinations(Field, i))
    
    CandiKey = [] # ์‹ค์ œ ํ›„๋ณดํ‚ค ๋งŒ๋“ค๊ธฐ
    for c in Comb:
        _tmp = []
        for r in range(len(relation)):
            _txt = ""
            for cc in c:
                _txt += relation[r][cc]
            _tmp.append(_txt)
        if len(_tmp) == len(list(set(_tmp))):
            CandiKey.append(_tmp)
        else:
            Comb.remove(c)
            Comb.insert(0,[])
            
    Comb = [c for c in Comb if len(c) > 0] # remove ์“ธ๋•Œ ์ธ๋ฑ์Šค ์•ˆ๋ฐ”๋€Œ๊ฒŒ ํ•˜๋ ค๊ณ  insertํ•œ ๋นˆ์นธ ์ง€์šฐ๊ธฐ
    final_Comb = []
    while len(Comb) > 0:
        answer += 1
        x = Comb.pop(0)
        final_Comb.append(x)
        for i in range(len(Comb)):
            if set(Comb[i]) == set(Comb[i])|set(x):
                Comb[i] = []
        Comb = [c for c in Comb if len(c) > 0]
    
    return len(final_Comb)
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.32ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (2.57ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (3.82ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (3.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (3.82ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	์‹คํŒจ (2.77ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.70ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (2.30ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (3.24ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (2.61ms, 10.3MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.32ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.4MB)
  • ์•„............ ๋‚œ ๋ณ‘์‹ ์ด๋‹ค. ๋ณ‘์‹ ์ด์•ผ....
    • ๋งจ ์ฒ˜์Œ์— ์กฐํ•ฉ ๋งŒ๋“ค๋•Œ... len(Field)+1์„ ์•ˆํ•ด์ค˜์„œ ํ•˜๋‚˜ ๋ชจ์ž๋ž๋˜ ๊ฑฐ์•ผ.
      • ์‹คํŒจ
        •     Comb = [] # ํ•„๋“œ ์ธ๋ฑ์Šค๋กœ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
              for i in range(1, len(Field)):
                  Comb += list(combinations(Field, i))
      • ์„ฑ๊ณต
        •     Comb = [] # ํ•„๋“œ ์ธ๋ฑ์Šค๋กœ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
              for i in range(1, len(Field)+1):
                  Comb += list(combinations(Field, i))
    • ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜
from itertools import combinations

def solution(relation):
    answer = 0
    Field = [i for i in range(len(relation[0]))]
    
    Comb = [] # ํ•„๋“œ ์ธ๋ฑ์Šค๋กœ ์กฐํ•ฉ ๋งŒ๋“ค๊ธฐ
    for i in range(1, len(Field)+1):
        Comb += list(combinations(Field, i))
    
    CandiKey = [] # ์‹ค์ œ ํ›„๋ณดํ‚ค ๋งŒ๋“ค๊ธฐ
    for c in Comb:
        _tmp = []
        for r in range(len(relation)):
            _txt = ""
            for cc in c:
                _txt += relation[r][cc]
            _tmp.append(_txt)
        if len(_tmp) == len(list(set(_tmp))):
            CandiKey.append(_tmp)
        else:
            Comb.remove(c)
            Comb.insert(0,[])
    Comb = [c for c in Comb if len(c) > 0]
    #print(CandiKey)
    #print(Comb)
    
    final_Comb = []
    while len(Comb) > 0:
        answer += 1
        x = Comb.pop(0)
        final_Comb.append(x)
        for i in range(len(Comb)):
            #print(x, Comb[i])
            if set(Comb[i]) == set(Comb[i])|set(x):
                Comb[i] = []
        Comb = [c for c in Comb if len(c) > 0]
    #print(final_Comb)
    return len(final_Comb)
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.60ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (5.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (1.88ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (3.70ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (2.55ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.65ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (2.39ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (3.07ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (4.35ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.2MB)

์ด๊ฒƒ ๋•Œ๋ฌธ์— ์ดํ‹€์„ ๊ณ ์ƒํ–ˆ๋„ค...

  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๋‚ด๊ฒƒ๋ณด๋‹ค ๋น ๋ฅด๊ณ  ์ฝ”๋“œ๊ฐ€ ๋” ์˜ˆ์œ ๊ฒƒ๋“ค ์œ„์ฃผ๋กœ
def solution(relation):
    def is_powerset(parent, child):
        return parent | child == parent

    n_attr = len(relation[0])
    candidate_key = []

    for key in range(1, 1 << n_attr):
        # ํ˜„์žฌ๊นŒ์ง€ ์ฐพ์€ ํ›„๋ณดํ‚ค์„ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๊ฐ€์ง€์ง€ ์•Š๋Š”์ง€๋ฅผ ๋จผ์ € ํŒ๋‹จ
        flag = False
        for e in candidate_key:
            if is_powerset(key, e):
                flag = True
                break
        if flag:
            continue

        # ํ˜„์žฌ ํ‚ค๊ฐ€ ์œ ์ผ์„ฑ์„ ๋งŒ์กฑํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จ
        sliced_relation = []
        for row in relation:
            sliced_row = []
            for j in range(0, n_attr):
                # print(bin(copied_key >> j))
                if (key >> j) & 1 == 1:
                    sliced_row.append(row[j])
            sliced_relation.append(tuple(sliced_row))

        if len(sliced_relation) != len(set(sliced_relation)):
            continue

        # ํ›„๋ณดํ‚ค ๋ชฉ๋ก์— ์ถ”๊ฐ€
        candidate_key.append(key)

    answer = len(candidate_key)
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (2.10ms, 10.4MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.88ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (1.62ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (1.68ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (5.26ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.99ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.78ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (1.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (2.36ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.78ms, 10.2MB)
  • ์ด์ชฝ๋„ ๊ณ ์ˆ˜
from itertools import combinations

def solution(relation):
    row = len(relation)
    col = len(relation[0])

    combi_list = []
    for i in range(1, col +1):
        combi_list.extend(combinations(range(col),i))

    answer_list = []
    for i in combi_list:
        tmp = [tuple([item[key] for key in i]) for item in relation]

        if len(set(tmp)) == row: # ์œ ์ผ์„ฑ ํ™•์ธ
            flag = True

            for j in answer_list:
                if set(j).issubset(set(i)): # ์ตœ์†Œ์„ฑ ํ™•์ธ j๊ฐ€ i์˜ sub ์ธ์ง€ ํ™•์ธ
                    flag = False
                    break

            if flag : answer_list.append(i)


    answer = len(answer_list)
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (1.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (4.58ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (1.36ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (5.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (3.78ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (1.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (1.27ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (3.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (2.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.45ms, 10.1MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.72ms, 10.4MB)

๊ณ ์ˆ˜๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด... ๋‚ด๊ฐ€ ์ฐธ ๋ฐ”๋ณด ๊ฐ™๋‹ค. ์‹ถ๋‹ค...

728x90
๋ฐ˜์‘ํ˜•