gpt4 book ai didi

python - 欧拉计划没有。 17 - 简化 Python

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:06:42 24 4
gpt4 key购买 nike

我是编程新手,诚然,我很尴尬地分享我的代码以供批评。这段代码有效并为以下问题 (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) 使用了求和函数。如果您删除所有出现的 ' '+ 并将 count 替换为 len,则代码会更短,但使用此代码您将拥有正确的 spell 函数。

如果您不理解粗体字的术语,那么您应该阅读一本关于 Python 3 的书。

如果你想节省一些时间,你甚至可以内存拼写函数,或者制作一个你内存的 count(spell()) 函数(因为字母计数整数比字符串更轻)。

关于python - 欧拉计划没有。 17 - 简化 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com