Project Euler问题17 Python
请告诉我怎么修复这个代码。我尝试了很多次,改了很多地方,但我离正确答案还差10!
如果把数字1到5写成单词:one, two, three, four, five,那么总共用了3 + 3 + 5 + 4 + 4 = 19个字母。
如果把从1到1000(one thousand)所有的数字都写成单词,总共会用多少个字母呢?
注意:不要计算空格或连字符。例如,342(three hundred and forty-two)包含23个字母,而115(one hundred and fifteen)包含20个字母。写数字时使用“and”是符合英国用法的。
我的解决方案
sd={0:0,1: 3, 2: 3, 3: 5, 4: 4, 5: 4, 6: 3, 7: 5, 8: 5, 9: 4}
dd1={10:3,11:6,12:6,13:8,14:8,15:7,16:7,17:9,18:9,19:8}
dd2={2:6,3:6,4:5,5:5,6:5,7:7,8:6,9:6}
td= {0: 10, 1: 13, 2: 13, 3: 15, 4: 14, 5: 14, 6: 13, 7: 15, 8: 15, 9: 14}
cd={0:0,1: 3, 2: 3, 3: 5, 4: 4, 5: 4, 6: 3, 7: 5, 8: 5, 9: 4,10:3,11:6,12:6,13:8,14:8,15:7,16:7,17:9,18:9,19:8}
def cw(n) :
if n/10 == 0 : # If the number is less than 10 execute this section
return sd[n%10]
elif n/100 == 0 : # If the number is less than 100 execute this section
if n<20 :
return(dd1[n]) # Directly map to dd1
else :
return(dd2[n/10]+sd[n%10]) # If the number is > 20 do a construction
elif n/1000==0 :
if n%100==0:
return sd[n/100] + 7 # If the number is multiples of 100 give assuming single digit and 7 for hundred
elif n%100 < 20 :
return td[n/100] + cd[n%100] # If 3 digit numbers not more than *20 , then direct mapping
else :
return td[n/100] + dd2[(n%100)/10] + sd[n%10]
count = 0
for i in range(1,1000) :
count = count + cw(i)
print count + 11
我得到了21134,但正确答案是...(剧透:请将鼠标悬停在下一行查看)
21124
真让人烦人!
3 个回答
这是我解决这个问题的方法
# Number letter counts
def e17():
n1=(0,3,3,5,4,4,3,5,5,4) # 1 to 9
n10=(0,3,6,6,5,5,5,7,6,6) # 10 to 90
n11=(0,6,6,8,8,7,7,9,8,8) # 11 to 19
n=(7,10,11) # hundred, hundred and, one thousand
n1to99x10 = (sum(n1)*9 + n10[1] + sum(n11) + sum(n10[2:])*10)*10 # from 1 to 99 all
n100to900all = n[0]*9 + n[1]*99*9 + sum(n1)*100 # from 100 to 900
letters = n1to99x10 + n100to900all + n[2]
return letters
print(e17()) # 21124
你给的代码里面充满了神秘的数字,实在是让人看不懂。正如其他人所建议的,还是让电脑自己来计算各种单词的长度比较好。我的看法是,照你现在的写法,我实在想不出这段代码除了用在这个Project Euler的问题上还有什么用处。我采取的方法是写了一个叫“num2words(i)”的函数,给定一个整数i,它会返回这个数字的英文单词。然后主循环就把1到1000的每个数字转换成单词,并计算这些单词的长度,使用正则表达式来排除空格,只计算字母的数量。这样性能还不错,我觉得我的方法也更容易调试。虽然我现在没有特别需要用到num2words,但我可以想象将来可能会在某个支票打印程序中重用这段代码。
顺便说一下,我的num2words函数是递归调用的,它会把大数字的前面几位分开(比如kddd),然后计算num2words(k) + " thousand",如果剩下的数字不为零,就再加上+" "+num2words(ddd)。处理百位的代码也是类似的。要添加处理百万的代码也是很简单的。
说到神秘数字,你的主循环为什么在999的时候就停了,然后最后再加11来计算“one thousand”中的字母?如果有人要把你的程序改成其他语言,能有多大概率会注意到最后需要加的那个11呢?
在我看来,如果你解决Project Euler问题的目标只是得到正确答案,那你就错过了很多做这些题目的教育价值。应该努力写出干净的代码。即使你的代码已经能给出正确答案,也要坐下来重新阅读你的代码,试着让它变得更好(比如更容易阅读,更“Pythonic”,或者是你愿意拿给程序员朋友看的东西)。
“十八”这个词只有八个字母,而不是九个。因为在1到1000这个范围内,它出现了十次,这就解释了这个差异。
顺便说一下,如果你在检查n是否小于10,为什么不直接用 n<10
呢,而要用 n/10 == 0
呢?