Created
April 4, 2010 06:44
-
-
Save showyou/355175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import copy | |
| def machi(num): | |
| """ | |
| >>> machi('1112224588899') | |
| (111)(222)(888)(99)[45] | |
| >>> machi('1122335556799') | |
| (123)(123)(555)(99)[67] | |
| (123)(123)(55)(567)[99] | |
| (123)(123)(99)(567)[55] | |
| >>> machi('1112223335559') | |
| (123)(123)(123)(555)[9] | |
| (111)(222)(333)(555)[9] | |
| >>> machi('1223344888999') | |
| (234)(234)(888)(999)[1] | |
| (123)(234)(888)(999)[4] | |
| (123)(44)(888)(999)[23] | |
| >>> machi('1112345678999') | |
| >>> machi('1122445577889') | |
| """ | |
| def sort(i): | |
| l = [] | |
| for j in i: | |
| l.append(int(j)) | |
| l.sort() | |
| return l | |
| l = sort(num) | |
| answer = [] | |
| history = [] | |
| find_7toitsu(l,history) | |
| find_loop(l,answer,history) | |
| def find_loop(l,answer,history): | |
| p2(l) | |
| for i,l2 in find_kotsu(l): | |
| p2(i) | |
| p2("(%d%d%d)" % (i,i,i)) | |
| a2 = copy.deepcopy(answer) | |
| a2.append((i,i,i)) | |
| find_loop(l2,a2,history) | |
| for i,l2 in find_shuntsu(l): | |
| p2(i) | |
| p2("(%d%d%d)" % (i,i+1,i+2)) | |
| a2 = copy.deepcopy(answer) | |
| a2.append((i,i+1,i+2)) | |
| find_loop(l2,a2,history) | |
| if len(l) <5: | |
| for i,l2 in find_toitsu(l): | |
| p2(i) | |
| p2("(%d%d)" % (i,i)) | |
| a2 = copy.deepcopy(answer) | |
| a2.append((i,i)) | |
| print_answer(l2, a2,history) | |
| else: | |
| if len(l) < 3: | |
| print_answer(l, answer,history) | |
| def print_answer(lest, a2, history): | |
| if find_answer_history(a2,lest,history): | |
| return | |
| else: | |
| history.append((a2,lest)) | |
| p2("a") | |
| if len(lest)==2: | |
| if abs(lest[0]-lest[1]) >1: | |
| return | |
| result = "" | |
| for a in a2: | |
| result += "(" | |
| for b in a: | |
| result+= str(b) | |
| result += ")" | |
| result += "[" | |
| for j in lest: | |
| result += str(j) | |
| result += "]" | |
| print result | |
| def find_answer_history(answer,lest,history): | |
| if len(answer) == 0: return False | |
| for h in history: | |
| a2 = copy.deepcopy(answer) | |
| l = copy.deepcopy(lest) | |
| for hi in h[0]: | |
| flag = False | |
| for a in a2: | |
| if hi == a: | |
| flag = True | |
| break | |
| if flag: | |
| a2.remove(hi) | |
| for i in h[1]: | |
| flag = False | |
| for m in l: | |
| if i == m: | |
| flag = True | |
| break | |
| if flag: l.remove(i) | |
| if len(a2) + len(l) == 0: return True | |
| return False | |
| def find_kotsu(l): | |
| # find (111) | |
| for i in range(10): | |
| if l.count(i) >= 3: | |
| l2 = l[:] | |
| for k in range(3): | |
| l2.remove(i) | |
| yield i,l2 | |
| def find_shuntsu(l): | |
| # find (123) | |
| for i in range(8): | |
| if l.count(i) and l.count(i+1) and l.count(i+2): | |
| l2 = l[:] | |
| for k in range(3): | |
| l2.remove(i+k) | |
| yield i,l2 | |
| def find_toitsu(l): | |
| for i in range(10): | |
| if l.count(i) >=2: | |
| l2 = l[:] | |
| for k in range(2): | |
| l2.remove(i) | |
| yield i,l2 | |
| def find_7toitsu(l,history): | |
| def find_toitsu(l): | |
| for i in range(10): | |
| if l.count(i) == 2: | |
| l2 = l[:] | |
| for k in range(2): | |
| l2.remove(i) | |
| return i,l2 | |
| else: return 0,l | |
| answer = [] | |
| n,l = find_toitsu(l) | |
| while(n): | |
| answer.append((n,n)) | |
| n,l = find_toitsu(l) | |
| if len(l) == 1: | |
| print_answer(l,answer,history) | |
| def p2(str): | |
| #print str | |
| pass | |
| def p3(str,pflag): | |
| #if pflag: print str | |
| pass | |
| if __name__ == '__main__': | |
| #machi("1112224588899") | |
| import doctest | |
| doctest.testmod(verbose=True) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trying:
machi('1112224588899')
Expecting:
(111)(222)(888)(99)[45]
ok
Trying:
machi('1122335556799')
Expecting:
(123)(123)(555)(99)67(123)(55)(567)99(123)(99)(567)[55]
File "machi.py", line 7, in main.machi
Failed example:
machi('1122335556799')
Expected:
(123)(123)(555)(99)67(123)(55)(567)99(123)(99)(567)[55]
Got:
(555)(123)(123)(99)67(123)(567)(55)99(123)(567)(99)[55]
Trying:
machi('1112223335559')
Expecting:
(123)(123)(123)(555)9(222)(333)(555)[9]
File "machi.py", line 11, in main.machi
Failed example:
machi('1112223335559')
Expected:
(123)(123)(123)(555)9(222)(333)(555)[9]
Got:
(111)(222)(333)(555)9(123)(123)(123)[9]
Trying:
machi('1223344888999')
Expecting:
(234)(234)(888)(999)1(234)(888)(999)4(44)(888)(999)[23]
File "machi.py", line 14, in main.machi
Failed example:
machi('1223344888999')
Expected:
(234)(234)(888)(999)1(234)(888)(999)4(44)(888)(999)[23]
Got:
(888)(999)(123)(234)4(999)(123)(44)23(999)(234)(234)[1]
Trying:
machi('1112345678999')
Expecting nothing
File "machi.py", line 18, in main.machi
Failed example:
machi('1112345678999')
Expected nothing
Got:
(111)(999)(234)(567)8(999)(234)(678)5(999)(345)(678)2(234)(567)(99)89(234)(789)(99)56(456)(789)(99)23(123)(456)(11)78(123)(678)(11)45(345)(678)(11)12(456)(789)(11)99(456)(789)(99)[11]
Trying:
machi('1122445577889')
Expecting nothing
File "machi.py", line 19, in main.machi
Failed example:
machi('1122445577889')
Expected nothing
Got:
(11)(22)(44)(55)(77)(88)[9]
10 items had no tests:
main
main.find_7toitsu
main.find_answer_history
main.find_kotsu
main.find_loop
main.find_shuntsu
main.find_toitsu
main.p2
main.p3
main.print_answer
1 items had failures:
5 of 6 in main.machi
6 tests in 11 items.
1 passed and 5 failed.
_Test Failed_ 5 failures.