Python递归函数中添加签名。

huangapple go评论75阅读模式
英文:

python add sign in recursive function

问题

I created a recursive function that sums digits of a number n, I want to add a negative sign to the return if n is negative, I only could create the sign outside the function itself because inside the function the sign is changing with every recursive call, Is there any way to calculate the sign inside the function itself?
here is my code

def sum1ton(n):
    if n < 10:
        return n
    return n % 10 + sum1ton(n // 10)

n = int(input())
sign = -1 if n < 0 else 1
print(f'Sum of digits of {n} = {sign * sum1ton(abs(n))}')
英文:

I created a recursive function that sums digits of a number n, I want to add a negative sign to the return if n is negative, I only could create the sign outside the function it self because inside the function the sign is changing with every recursive call, Is there any way to calculate the sign inside the function it self?
here is my code

def sum1ton (n):
    if n &lt; 10:
        return n
    return n%10 + sum1ton (n//10)

    
n = int(input())
sign = -1 if n&lt;0 else 1
print (f&#39;Sum of digits of {n} = {sign * sum1ton(abs(n))}&#39;)

.................................................................

答案1

得分: 0

You can make the recursive function an inner function of sum1ton():

def sum1ton(n):
    n = int(n)
    sign = -1 if n < 0 else 1
    def inner(n):
        if n < 10:
            return n
        return n % 10 + sum1ton(n // 10)
    return sign * inner(abs(n))

n = input()
print(f'数字{n}的各位数字之和 = {sum1ton(n)}')
英文:

You can make the recursive function an inner function of sum1ton():

def sum1ton(n):
    n = int(n)
    sign = -1 if n&lt;0 else 1
    def inner(n):
        if n &lt; 10:
            return n
        return n%10 + sum1ton (n//10)
    return sign * inner(abs(n))
    

n = input()
print (f&#39;Sum of digits of {n} = {sum1ton(n)}&#39;)

答案2

得分: 0

你不能同时要求一个递归函数在(没有符号的)数字中工作并传播符号,例如 sum1ton(-123) 不是 -1 + 2 + 3,而是 -(1 + 2 + 3) = -sum1ton(123)。如果你放弃了递归的要求,我建议使用以下代码:

def sum1ton(n):
    return (+1 if n >= 0 else -1) * sum(int(i) for i in str(abs(n)))
英文:

You cannot at the same time require a recursive function to work in (sign-less) digits and to propagate the sign, e.g. sum1ton(-123) is not -1 + 2 + 3, but -(1 + 2 + 3) = -sum1ton(123). If you drop the recursive requirement, I suggest

def sum1ton(n):
    return (+1 if n &gt;= 0 else -1) * sum(int(i) for i in str(abs(n)))

答案3

得分: 0

以下是您要翻译的内容:

我相信我们可以在不使用存储和恢复符号或内部函数的情况下完成这个任务。您在这里遇到的问题是:

n % 10
n // 10

因为它们不能保留我们在处理负数时所需的数字:

123 % 10
3
-123 % 10
7

123 // 10
12
-123 // 10
-13

因此,我们需要使用math.fmod()代替%(模数),并使用int(n / 10)代替n // 10

from math import fmod

def sum1ton(n):
    if -10 < n < 10:
        return n

    return int(fmod(n, 10)) + sum1ton(int(n / 10))

输出

sum1ton(1234567890)
45
sum1ton(-1234567890)
-45

英文:

I believe we can do this without a store and restore of the sign, nor an inner function. Your problems here are:

n % 10
n // 10

as they don't preserve the digits we need with negative numbers:

&gt;&gt;&gt; 123 % 10
3
&gt;&gt;&gt; -123 % 10
7

&gt;&gt;&gt; 123 // 10
12
&gt;&gt;&gt; -123 // 10
-13

So instead, we need to substitute math.fmod() for % (modulus) and int(n / 10) for n // 10:

from math import fmod

def sum1ton(n):
	if -10 &lt; n &lt; 10:
		return n

	return int(fmod(n, 10)) + sum1ton(int(n / 10))

OUTPUT

&gt;&gt;&gt; sum1ton(1234567890)
45
&gt;&gt;&gt; sum1ton(-1234567890)
-45
&gt;&gt;&gt; 

huangapple
  • 本文由 发表于 2023年5月7日 21:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定