Home > Blockchain >  Can i make this recursion work with negative numbers
Can i make this recursion work with negative numbers

Time:12-01

I wrote this code and it's alright with positive numbers, but when I tried negative it crashes. Can you give any hints on how to make it work with negative numbers as well? Also, I need it with recursion. The function needs to calculate the sum of the digits of an integer.

def sum_digits(n):
    if n != 0:
        return (n % 10   sum_digits(n // 10))
    else:
        return 0
    
if __name__=='__main__':
    
    print(sum_digits(123))


Input: 123
Output: 6

CodePudding user response:

On the assumption that the 'sum' of the three digits of a negative number is the same as that of the absolute value of that number, this will work:

def sum_digits(n):
    if n < 0:
        return sum_digits(-n)
    elif n != 0:
        return (n % 10   sum_digits(n // 10))
    else:
        return 0

That said, your actual problem here is that Python's handling of modulo for a negative number is different than you expect:

>>> -123 % 10
7

Why is that? It's because of the use of trunc() in the division. This page has a good explanation, but the short answer is that when you divide -123 by 10, in order to figure out the remainder, Python truncates in a different direction than you'd expect. (For good, if obscure, reasons.) Thus, in the above, instead of getting the expected 3 you get 7 (which is 10, your modulus, minus 3, the leftover).

Similarly, it's handling of integer division is different:

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

This is un-intuitively correct because it is rounding 'down' rather than 'towards zero'. So a -12.3 rounds 'down' to -13.

These reasons are why the easiest solution to your particular problem is to simply take the absolute value prior to doing your actual calculation.

CodePudding user response:

Separate your function into two functions: one, a recursive function that must always be called with a non-negative number, and two, a function that checks its argument can calls the recursive function with an appropriate argument.

def sum_digits(n):
    return _recursive_sum_digits(abs(n))


def _recursive_sum_digits(n):
    if n != 0:
        return (n % 10   sum_digits(n // 10))
    else:
        return 0

Since _recursive_sum_digits can assume its argument is non-negative, you can dispense with checking its sign on every recursive call, and guarantee that n // 10 will eventually produce 0.

CodePudding user response:

If you want to just sum the digits that come after the negative sign, remove the sign by taking the absolute value of the number. If you're considering the first digit of the negative number to be a negative digit, then manually add that number in after performing this function on the rest of the digits.

CodePudding user response:

Here is your hint. This is happening because the modulo operator always yields a result with the same sign as its second operand (or zero). Look at these examples:

>>> 13 % 10
3
>>> -13 % 10
7

In your specific case, a solution is to first get the absolute value of the number, and then you can go on with you approach:

def sum_digits(n):
    n = abs(n)
    if n != 0:
        return (n % 10   sum_digits(n // 10))
    else:
        return 0
  • Related