重複の可能性:
ベキ集合と集合のデカルト積 python
古い問題をスクラッチします。私はすべてを理解しました。今、私はさらにクレイジーな問題を抱えています。これが私が得るべきものです:
入力: scoreList(["a", "s", "m", "t", "p"])
出力: [['a', 1], ['am', 4], ['at', 2], ['spam', 8]]
この I/O はうまく機能しますが、次のように 6 番目の要素を追加すると:
入力: scoreList(["a", "s", "m", "t", "p", "e"])
プログラムは狂ったようにバグアウトします。これを修正する方法を教えてください。どんな助けにも感謝します
私のコード:
from itertools import chain, combinations
def ind(e,L):
if L==[] or L=="":
return 0
elif L[0]==e:
return 0
else:
return ind(e,L[1:])+1
def letterScore(letter, scorelist):
if scorelist[0][0] == letter:
return scorelist[0][1]
elif (len(scorelist) == 1) and (scorelist[0][0] != letter):
return 'lol. stop trying to crash my program'
else:
return letterScore(letter, scorelist[1:])
scorelist = [ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2], ["h", 4], ["i", 1], ["j", 8], ["k", 5], ["l", 1], ["m", 3], ["n", 1], ["o", 1], ["p", 3], ["q", 10], ["r", 1], ["s", 1], ["t", 1], ["u", 1], ["v", 4], ["w", 4], ["x", 8], ["y", 4], ["z", 10] ]
def wordScore(S, scorelist):
if (len(S) == 1):
return letterScore(S[0],scorelist)
elif (letterScore(S[0],scorelist) == 'lol. stop trying to crash my program'):
return 'you really want to crash me, dont you'
else:
return letterScore(S[0],scorelist) + wordScore(S[1:], scorelist)
def perm(l):
sz = len(l)
if sz <= 1:
return [l]
return [p[:i]+[l[0]]+p[i:]
for i in xrange(sz) for p in perm(l[1:])]
from itertools import combinations, permutations
def findall(my_input):
return [''.join(p) for x in range(len(my_input)) for c in combinations(my_input, x+1)
for p in permutations(c)]
d = ["a", "am", "cab", "apple", "at", "bat", "bar", "babble", "can", "foo", "spam", "spammy", "zzyzva"]
def match(lis):
return match2(findall(lis))
def match2(lis):
if lis == []:
return []
elif(len(d) != ind(lis[0],d)):
return [lis[0]] + match2(lis[1:])
else:
return match2(lis[1:])
def scoreList(lis):
return match3(match(lis))
def match3(lis):
if (lis == []):
return []
else:
return [[lis[0],wordScore(lis[0],scorelist)]] + match3(lis[1:])