somme récursive python de chiffre
def sum_digits(num: int) -> int:
#base case when your "positive" integer get to 0
if num == 0:
return 0
#base case when your "negative" integer get is between -10 and 0
if num > -10 and num < 0:
return num
# recursion for positive integer
elif num > 0:
return (num % 10) + sum_digits(num//10)
# recursion for negative integer
elif num < 0:
return -(abs(num) % 10) + sum_digits(-(abs(num)//10))
sum_digits(123) # returns 6
sum_digits(-123) # returns -6
Average Armadillo