반응형
#!/usr/bin/env python
def is_two_listlist_identical(lla, llb):
#print "XXXXXXXXXXXXXXXXXXXXXXX"
#print lla
#print llb
for i in range(len(lla)):
la = lla[i]
lb = llb[i]
if len(la) != len(lb):
return False
else:
if len(la) != 0:
for j in range(len(la)):
if la[j] != lb[j]:
return False
return True
def is_already_in(lll, ll):
la = [ le for le in ll ]
for e_ll in lll:
lb = [ le for le in e_ll ]
if is_two_listlist_identical(la, lb):
return True
return False
def find_partition(slist, elist):
#print "==============="
#print len(slist), slist
#print elist
if len(elist) == 0:
return [ [ [] for s in slist ] ]
r = []
e = elist[0]
elist_new = elist[1:]
for i in range(len(slist)):
#print " -- i",i
slist_new = list(slist)
slist_new[i] -= e
if slist_new[i] < 0:
continue
r_sub = find_partition(slist_new, elist_new)
for e_r_sub in r_sub:
#print " e_r_sub", e_r_sub
e_r_sub[i].append(e)
e_r_sub[i].sort()
if not is_already_in(r, e_r_sub) :
r.append(e_r_sub)
return r
sl = [ 100, 200, 300 ]
el = [ 10, 20, 20, 20, 50, 50, 50, 50, 60, 60, 60, 70, 80 ]
for a in find_partition(sl, el):
print "=========XXXXXXX=========="
for b in a:
print " ", sum(b),":", b
def is_two_listlist_identical(lla, llb):
#print "XXXXXXXXXXXXXXXXXXXXXXX"
#print lla
#print llb
for i in range(len(lla)):
la = lla[i]
lb = llb[i]
if len(la) != len(lb):
return False
else:
if len(la) != 0:
for j in range(len(la)):
if la[j] != lb[j]:
return False
return True
def is_already_in(lll, ll):
la = [ le for le in ll ]
for e_ll in lll:
lb = [ le for le in e_ll ]
if is_two_listlist_identical(la, lb):
return True
return False
def find_partition(slist, elist):
#print "==============="
#print len(slist), slist
#print elist
if len(elist) == 0:
return [ [ [] for s in slist ] ]
r = []
e = elist[0]
elist_new = elist[1:]
for i in range(len(slist)):
#print " -- i",i
slist_new = list(slist)
slist_new[i] -= e
if slist_new[i] < 0:
continue
r_sub = find_partition(slist_new, elist_new)
for e_r_sub in r_sub:
#print " e_r_sub", e_r_sub
e_r_sub[i].append(e)
e_r_sub[i].sort()
if not is_already_in(r, e_r_sub) :
r.append(e_r_sub)
return r
sl = [ 100, 200, 300 ]
el = [ 10, 20, 20, 20, 50, 50, 50, 50, 60, 60, 60, 70, 80 ]
for a in find_partition(sl, el):
print "=========XXXXXXX=========="
for b in a:
print " ", sum(b),":", b
728x90
'프로그래밍 > Python' 카테고리의 다른 글
TORNADO too many file descriptors in select() (0) | 2015.11.17 |
---|---|
twisted counter 연습문제 (0) | 2015.04.15 |
[PYTHON|PANDAS] pandas.read_csv MemoryError 문제 (0) | 2015.04.01 |
[번역|StackOverflow] 파이썬 람다 - 왜써? / Python Lambda - why? (0) | 2011.10.18 |
[Py] 신이 보여준 정리 (2) | 2009.07.24 |