如何使用嵌套的for循环,使用范围(包括字符串索引)来清理长乘法代码行



我写了一些代码来解决项目欧拉#8。我在网上看到过一些其他的解决这个问题的方法,但是我不明白他们是如何创建嵌套循环的,或者它是一个不同的方法。

问题概述:在1000位数字(无分隔符的字符串)中找到乘积最大的13个相邻数字。这个产品的价值是多少?

我让它工作了,但我找不到一种方法来简化从一串数字中得到的13个数字的乘积。

我是这么想的:

num_string = '73167176531330624919225119674426574742355349...'
length = 13
products_of_13 = []
largest_product_of_13 =0
for i in range(0,(len(num_string) - 12)):
    product_13=0
    for j in range (i,i+12):
        product_13 *= int(num_string[j])
    products_of_13.append(product_13)
for num in products_of_13:
    if num > largest_product_of_13:
       # print(product_13)
        largest_product_of_13 = num
print(largest_product_of_13)

每次的结果都是0。我添加了

print(int(num_string[j]))

在上面的代码中插入来检查我是否得到了正确的数字,但是看起来好像在循环的每次迭代之间放置了一个零。

问题:我如何创建一个循环来计算product_13,以避免输入每个字符串索引并将其转换为整数?

我尝试了很多不工作的东西,我使用了下面的代码,这是可怕的阅读:

num_string='''7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'''

products_of_13 = []
largest_product_of_13 =0
for i in range(0,(len(num_string) - 12)):
    product_13= int(num_string[i])*int(num_string[i+1])*int(num_string[i+2])*int(num_string[i+3])*int(num_string[i+4])*int(num_string[i+5])*int(num_string[i+6])*int(num_string[i+7])*int(num_string[i+8])*int(num_string[i+9])*int(num_string[i+10])*int(num_string[i+11])*int(num_string[i+12])
    products_of_13.append(product_13)
for num in products_of_13:
    if num > largest_product_of_13:
       # print(product_13)
        largest_product_of_13 = num
print(largest_product_of_13)

更新日期:解决方案是23514624000

您需要分配product_13 = 1而不是0。这是因为任何数字乘以0只会得到0的结果。

我在我的笔记本电脑上尝试了这个问题,我得出了这个解决方案

在你的解决方案中,产品似乎是静态的,这可能会产生问题。

也是一个建议,在字符串中可以使用多个数字reduce(lambda x,y: x*y, map(int, your_string))

from functools import reduce
a = """73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450"""
a = a.replace("n", "") # convert multi line string to single string
i = 0 # start from index 0
adj = 13 # how many adjacent characters to take
max_ = -1 # store max value as -1 from start
res = ""  # store the result as string in this
while i<(len(a)-adj): # loop from i=0 till length of string - adjacent characters
  start = a[i:i+adj]  # take the substring from given index to next 13 characters
  prod = reduce(lambda x,y: x*y, map(int, start)) # this will multiply all integers in substring and get the product of those
  max_ = max(max_, prod) # check if the product obtained above is more than max if yes than store the res
  if max_ == prod:
    res = start
  i += 1
res
# output : 5576689664895

这个解可以进一步优化,取13字符串的窗口,计算前13个数字的乘积,然后除以第1个数字,再乘以第13个数字。这样我们就不需要一次又一次地把整个13个数字相乘了。

相关内容

最新更新