Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
divmod and recursion solution in Clear category for Sum of Digits by kdim
def sum_digits(num: int) -> int:
sum = 0
while num > 9:
num, n = divmod(num, 10)
sum += n
sum += num
return sum if sum < 10 else sum_digits(sum)
print("Example:")
print(sum_digits(38))
assert sum_digits(38) == 2
assert sum_digits(0) == 0
assert sum_digits(10) == 1
assert sum_digits(132) == 6
assert sum_digits(232) == 7
assert sum_digits(811) == 1
assert sum_digits(702) == 9
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 27, 2022
Comments: