Skip to content

Instantly share code, notes, and snippets.

@keizie
Last active December 20, 2015 14:19
Show Gist options
  • Save keizie/6145738 to your computer and use it in GitHub Desktop.
Save keizie/6145738 to your computer and use it in GitHub Desktop.
http://zariski.wordpress.com/2013/08/02/%EC%98%A4%EB%8A%98%EC%9D%98-%EB%B8%8C%EB%A0%88%EC%9D%B8%ED%8B%B0%EC%A0%80/ 문제를 확인하기 위한 코드. 결과적으로 35개의 3으로 이루어진 목록은 나오지만 최종적으로 1만으로 이루어진 데까지는 가지 않는다.
# -*- coding:utf-8 -*-
from itertools import combinations
from pprint import pprint
bundle = [51,49,5]
candidates = [bundle]
loop = True
loop_count = 0
largest_member = []
while loop:
loop_count = loop_count + 1
max_number_of_members = 0
max_member = 0
#if len(largest_member) > 33: candidates = [largest_member]
#print candidates
for i,b in enumerate(candidates):
splitable = False
if max_number_of_members < len(b):
max_number_of_members = len(b)
largest_member = b
# 나눌 수 있으면
for key,val in enumerate(b):
if val % 2 == 0:
splitable = True
# 현재 묶음은 제거
del candidates[i][key]
# 나눈 값으로 새 묶음 생성
candidates[i].append(val/2)
candidates[i].append(val/2)
candidates[i].sort()
if max_member < max(candidates[i]):
max_member = max(candidates[i])
# 나눌 수 없었으면
if splitable == False:
# 합해서 다른 묶음 생성
for c in combinations(b,2):
_b = list(b)
_b.remove(c[0])
_b.remove(c[1])
_b.append(c[0] + c[1])
_b.sort()
candidates.append(_b)
# 현재 묶음은 제거
del candidates[i]
candidates = reduce(lambda x, y: x if y in x else x + [y], candidates, [])
pprint(candidates)
print len(candidates), max_number_of_members, max_member, largest_member
if loop_count >= 1000:
loop = False
if len(candidates) == 0:
loop = False
if max_number_of_members == 1:
loop = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment