728x90
๋ฐ์ํ
ํ์ด
- ํ๋ณดํค๋ฅผ ๋ง๋ค์ด์ ์ค๋ณต์ด ์๋์ง ์ฒดํฌํ๋ ๋ฌธ์ .
- ์กฐํฉ๋ง๋๋ ๊ฑด
- for๋ฌธ์... for๋ฌธ ๋ง๊ณ ์ข ํธํ๊ฑฐ ์๋? ์กฐํฉ ๋ง๋ค์ด์ผ ๋๋ค. 4+3+2+1...
- ์! 1์ ํ์์๋ค. ๋ค ๋ค์ด์๋๊ฑฐ๋ผ์... 4+3+2
- ๊ทธ๋ฆฌ๊ณ ์ค๋ณต ์ฒดํฌ๋ ์นด์ดํฐ ์ฐ๋ฉด ๋ ๋ฏ?
- ์๋๋ ์ข ํฌ๊ธฐํ๊ณ ํธํ๊ฒ ๊ฐ์...
- ์กฐํฉ๋ง๋๋ ๊ฑด
- ๋ญ ์จ์ผํ๋? ์ผ๋จ itertools๊ฐ ์๊ฐ๋ฌ๋ค.
- ์ฝค๋น๋ค์ด์
์ฐ๋ฉด ๋์ง?
- ์๋๋ค. ๊ฑฐ๊พธ๋ก ์กฐํฉํ ์๋ ์์์?
- ์๋๊ฐ? ์ ๋ฐฉํฅ ์กฐํฉํด์ ์ค๋ณต์ด๋ฉด ์ญ๋ฐฉํฅ ์กฐํฉํด๋ ์ค๋ณต์ด์์?
- ๊ทธ๋ฅ ์ฝค๋น๋ค์ด์ ์ด ๋ง๋ค?
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๋ถ ๋์ ์๊ฐํ ๊ฑธ๋ก ์ง๋ด.
- ์กฐํฉ์์ฒด๊ฐ ๊ธธ์ด๊ฐ ์งง์ ์ ๋ค๋ถํฐ ๋์ด ์๊ธด ํจ.
- (0,), (0, 1), (0, 2), (0, 3), (1, 2), (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)
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)๋ ์๋๋๊ฑฐ๊ตฌ๋?
- ์ด๋ฌ๋ฉด ์์๋๋ก ๋น๊ตํ ํ์์์ด...? ๊ทธ๋ฅ ๋นผ๋ฉด ๋๋๊ฑฐ ์๋๊ฐ?
- ๊ฒฐ๊ตญ ๋ฌธ์ ๋ฅผ ์ ๋๋ก ์ดํด๋ฅผ ๋ชป ํ๋ค๋ ๊ฑธ๋ก ๊ฒฐ๋ก .
- ์ด๊ฒ๋ ์คํจ.... ๋๋ฌดํ๋ค. ๋ฌธ์ ๋ ๋ค์ ์ฝ์ด๋ณด์.
- ์... (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 = [] # ํ๋ ์ธ๋ฑ์ค๋ก ์กฐํฉ ๋ง๋ค๊ธฐ
- ์ฑ๊ณต
- Comb = [] # ํ๋ ์ธ๋ฑ์ค๋ก ์กฐํฉ ๋ง๋ค๊ธฐ
for i in range(1, len(Field)+1):
Comb += list(combinations(Field, i))
- Comb = [] # ํ๋ ์ธ๋ฑ์ค๋ก ์กฐํฉ ๋ง๋ค๊ธฐ
- ์คํจ
- ํํํํํํํํํํํํํํํ
- ๋งจ ์ฒ์์ ์กฐํฉ ๋ง๋ค๋... len(Field)+1์ ์ํด์ค์ ํ๋ ๋ชจ์๋๋ ๊ฑฐ์ผ.
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
๋ฐ์ํ
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฝ๋ฉํ ์คํธ ์ ์ ์ผ๊ฐํ (0) | 2023.02.23 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ (ํฌํ์ด์งํธ๋ฆฌ) (0) | 2023.02.22 |
ํ๋ก๊ทธ๋๋จธ์ค ์ต์ต๋จ์ ์ธ์ฐ์ (0) | 2023.02.22 |
ํ๋ก๊ทธ๋๋จธ์ค [1์ฐจ] ํ๋ ์ฆ4๋ธ๋ก (1) | 2023.02.21 |
ํ๋ก๊ทธ๋๋จธ์ค [1์ฐจ] ๋ด์คํด๋ฌ์คํฐ๋ง (0) | 2023.02.21 |