简体   繁体   English

数字中的最大交替总和

[英]Maximum alternating sum in a number

Alternating sum is defined like this : 交替和定义如下: 交替总和

I'm supposed to write a function which finds the maximum alternating sum with a specific length: for example the number 43805 has alternating sums with length 3: 我应该编写一个函数,该函数查找具有特定长度的最大交替和:例如,数字43805的长度为3:

4-3+8,
3-8+0,
8-0+5

An example of the function output: >>> altsum_digits(5**36, 12) 18 函数输出的示例: >>> altsum_digits(5**36, 12) 18

I wrote my code but for some reason I don't get the exact result, for the number 5**36 I get the answer 20 instead of 18, Can you please tell me what should I fix in my code? 我写了我的代码,但由于某种原因我没有得到确切的结果,因为数字5 ** 36我得到的答案是20而不是18,您能告诉我我应该在代码中解决什么吗?

Another question: let's define nplus, nminus, nmult as the number of adds, Subtractions and multiplication the function does each run, I'm supposed to write a Mathematical expression for each one the parameters according to the inputs n and d, I'm a beginner and I have no clue how to do this, I would really appreciate any help. 另一个问题:让我们将nplus,nminus,nmult定义为函数每次运行的加,减和乘的数量,我应该根据输入n和d为每个参数写一个数学表达式,初学者,我不知道如何执行此操作,非常感谢您的帮助。

Here is my code: 这是我的代码:

def altsum_digits(n,d):
    c = [int(l) for l in str(n)]

    maxaltsum=0
    tmpmax=0
    for i in range(0,d):
       tmpmax=tmpmax+((-1)**(i)*c[i])


    it=(len(c)-d)
    for i in range(1,it):
        tmpmax=c[i+d-1]-(tmpmax-c[i-1])
        if (tmpmax)>maxaltsum:
            maxaltsum=tmpmax
    print(maxaltsum)

Based on simple debugging, I think your calculation of tmpmax based on previous tmpmax is incorrect, you missed a minus sign: use tmpmax=-c[i+d-1]-(tmpmax-c[i-1]) instead of tmpmax=c[i+d-1]-(tmpmax-c[i-1]) . 基于简单的调试,我认为您根据先前的tmpmax计算出的tmpmax不正确,您错过了减号:使用tmpmax=-c[i+d-1]-(tmpmax-c[i-1])代替tmpmax=c[i+d-1]-(tmpmax-c[i-1]) Just think it through, it is logical. 仔细考虑一下,这是合乎逻辑的。 It is always very easy to get lost in the forest of alternating signs. 在交替出现的标志森林中迷路总是很容易的。

Of the three bugs mentioned in elias' answer and the comments (wrong initialisation of maxaltsum , wrong number of iterations in the second loop, and missing a - in the tmpmax calculation in the second loop), you can avoid ever running into at least the first one by using Python's builtins. 在Elias的答复中提到的3个缺陷和评论(的错误初始化的maxaltsum ,在第二循环中错误的迭代次数,以及缺少一个-在第二循环中的tmpmax计算),你能避免以往任何时候都运行到至少第一个使用Python的内置函数。

Whenever you have a problem that is "work out the maximum something ", and your way of doing it involves exhaustively searching all the possible somethings for the maximum, you can use Python's builtin function max . 只要你有一个问题,就是“制定出的最大的东西 ”,而你做它涉及详尽搜索所有可能出头的上限,您可以使用Python的内置函数的方式max Here's how you would do it here using a Python generator: 使用Python生成器的操作方法如下:

def altsums(n, d):
    c = [int(l) for l in str(n)] 

    alt = 0
    for i in range(0,d):
       alt=alt+((-1)**(i)*c[i])

    yield alt

    it=(len(c)-d)
    for i in range(1,it):
        alt = -c[i+d-1]-(alt-c[i-1])
        yield alt

print(max(altsums(5**36, 12))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM