我对编程很不熟悉,而且不可否认的是,我很不好意思分享我的代码以供评论。这段代码可以工作,并对下面的问题(https://projecteuler.net/problem=17)给出了正确的答案,但我希望我可以得到一些关于如何使它不那么难看和更精简的评论,甚至可以从一个全新的角度来处理这个问题,以便我能更好地从这个解决方案中学习非常感谢你的帮助!

# the final list that holds the string lengths
addList = []

# dictionary holding integer:corresponding word pairs
numbersDict = {
0:"zero",
1:"one",
2:"two",
3:"three",
4:"four",
5:"five",
6:"six",
7:"seven",
8:"eight",
9:"nine",
10:"ten",
11:"eleven",
12:"twelve",
13:"thirteen",
14:"fourteen",
15:"fifteen",
16:"sixteen",
17:"seventeen",
18:"eighteen",
19:"nineteen",
20:"twenty",
30:"thirty",
40:"forty",
50:"fifty",
60:"sixty",
70:"seventy",
80:"eighty",
90:"ninety"
}

### There has to be an easier way to do all this below ###

def numberLetters(num):

    letters = ""

    if 0 < num <= 20:
        letters += numbersDict[num]

    if 21 <= num <= 99:
        a,b = divmod(num, 10)
        if b == 0:
            letters += numbersDict[a*10]
        else:
            letters += numbersDict[a*10] + numbersDict[b]

    if 100 <= num <= 999:
        if num % 100 == 0:
            letters += numbersDict[int(num / 100)] + "hundred"
        else:
            digit = int(num / 100)
            num = num - digit * 100
            if 0 < num <= 20:
                letters += numbersDict[digit] + "hundredand" + numbersDict[num]
            if 21 <= num <= 99:
                a,b = divmod(num, 10)
                if b == 0:
                    letters += numbersDict[digit] + "hundredand" + numbersDict[a*10]
                else:
                    letters += numbersDict[digit] + "hundredand" + numbersDict[a*10] + numbersDict[b]
    if num == 1000:
        letters += "onethousand"

    return letters

for i in range(1,1001):
    addList.append(len(numberLetters(i)))
print(sum(addList))

最佳答案

这是我的代码:

words = [
(1,   'one'      ,''   ),
(2,   'two'      ,''   ),
(3,   'three'    ,''   ),
(4,   'four'     ,''   ),
(5,   'five'     ,''   ),
(6,   'six'      ,''   ),
(7,   'seven'    ,''   ),
(8,   'eight'    ,''   ),
(9,   'nine'     ,''   ),
(10,  'ten'      ,''   ),
(11,  'eleven'   ,''   ),
(12,  'twelve'   ,''   ),
(13,  'thirteen' ,''   ),
(14,  'fourteen' ,''   ),
(15,  'fifteen'  ,''   ),
(16,  'sixteen'  ,''   ),
(17,  'seventeen',''   ),
(18,  'eighteen' ,''   ),
(19,  'nineteen' ,''   ),
(20,  'twenty'   ,''   ),
(30,  'thirty'   ,''   ),
(40,  'forty'    ,''   ),
(50,  'fifty'    ,''   ),
(60,  'sixty'    ,''   ),
(70,  'seventy'  ,''   ),
(80,  'eighty'   ,''   ),
(90,  'ninety'   ,''   ),
(100, 'hundred'  ,'and'),
]
words.reverse()

def spell(n, i=0):
    global words
    word = ""
    while n > 0:
        for num in words[i:]:
            if num[0] <= n:
                div = n // num[0]
                n = n % num[0]
                if num[2]: word+=' '+spell(div,i)
                word+=' '+num[1]
                if num[2] and n: word+=' '+num[2]
                break
    return word[1:]

count = lambda s: sum(1 for i in s if i!=' ')
print(sum(count(spell(n)) for n in range(1001)))

注意,我使用递归函数,并且添加了临时参数'I'来拼写,它记录单词中使用的最后一个索引,从而在for num in words循环中省去了一些迭代(实际上不是很有用,但它只需要6个字符^^)然后,我使用了一个多余的lambda函数来生成count函数,因为函数的名称是某种注释。
最后,在生成器表达式count(spell(n)) for n in range(1001)上使用sum函数。
如果删除所有出现的' '+并将count替换为len,则代码可能会更短,但使用此代码,可以使用正确的spell函数。
如果你不懂黑体字,那么你应该读一本关于python 3的书。
如果您想抽出一些时间,甚至可以记住spell函数,或者创建一个您记住的count(spell())函数(因为字母计数整数比字符串轻)。

10-08 11:24