ref : http://blog.readiz.com/112
두번째를 하고 나니 코드가 나에게 외치고 있었다.
'재귀함수로 바꿔주세요. 재귀함수로 바꿔주세요...'
코드의 목소리를 외면할 수 없었다...ㅠ.ㅠ
재귀함수를 잘 설계하면 리스트를 넣으면 가능한 조합의 리스트를 출력하는 것도 가능할 거 같다.
quizS3.py
1 from quizS2 import isMatchCase 2 3 __iGotIt=False 4 def makeAvailableListCore(srcList=range(1, 10), dstList=[], length=len(range(1, 10)), callback=None): 5 global __iGotIt 6 7 if not len(dstList) == length: 8 for x in filter(lambda x: x not in dstList, srcList): 9 dstList.append(x) 10 makeAvailableListCore(filter(lambda x: x not in dstList, srcList), dstList, length=length, callback=callback) 11 dstList.remove(x) 12 else: 13 #print dstList 14 if callback: 15 if callback(dstList): 16 __iGotIt = True 17 18 def makeAvailableList(srcList=range(1, 10), callback=None): 19 makeAvailableListCore(srcList=srcList, length=len(srcList), callback=callback) 20 21 if __name__ == '__main__': 22 makeAvailableList(callback=isMatchCase) 23 if __iGotIt == False: 24 print "Cannot find numbers set(s)."
댓글