Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """In this kata, you must create a digital root function.
- A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has two digits, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
- Here's how it works (Ruby example given):
- digital_root(16)
- => 1 + 6
- => 7
- digital_root(942)
- => 9 + 4 + 2
- => 15 ...
- => 1 + 5
- => 6
- """
- def digital_root(n):
- ints = [str(x) for x in str(n)]
- y = 0
- for integers in ints:
- y += int(integers)
- if len([str(x) for x in str(y)]) > 1:
- return digital_root(y)
- else:
- return y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement