我一直在學習Python並與Project Euler一起玩,以重新修復我的一些數學技能。我遇到了Problem 35的問題。我已經產生了100萬以下的所有獎金,淘汰了那些包含任何偶數的獎金,現在我只想用剩下的〜3k素數運行最後一張支票。Python:'For'loop只運行一次,Project Euler
這個功能應該是:
- 就拿〜3K素數的列表。
- 返回由原始列表中每個項目的所有旋轉列表組成的新列表。
這裏是我有,人有我所理解的每一行:
def rotations(lst):
newlist = []
for i in lst: # Take each int item in list.
s = [int(j) for j in str(i)] # Turn each item into a list of digit strings
num = ([s[k:]+s[:-len(s)+k] for k in range(len(s))]) # Generate list of rotations of strings
tmplst = []
for l in num: # For each string rotation
tmplst.append(int(''.join(map(str,l)))) # Turn it into an integer, add that int to tmplst
newlist.append(tmplst) # Add each tmplist to 'newlist'
return newlist
輸入rotations([123,456])
只收益率:
[[123, 231, 312]]
當我期待
[[123, 231, 312],[456,564,645]]
任何可能會出錯的線索?
不能再現,得到'[[123,231,312],[456,564,645]]'。 – 2013-03-05 17:36:17