Advertisement
nher1625

digital_root_solution

Jun 2nd, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. """In this kata, you must create a digital root function.
  2.  
  3. 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.
  4.  
  5. Here's how it works (Ruby example given):
  6. digital_root(16)
  7. => 1 + 6
  8. => 7
  9.  
  10. digital_root(942)
  11. => 9 + 4 + 2
  12. => 15 ...
  13. => 1 + 5
  14. => 6
  15. """
  16.  
  17. def digital_root(n):
  18.     ints = [str(x) for x in str(n)]
  19.     y = 0
  20.     for integers in ints:
  21.         y += int(integers)
  22.     if len([str(x) for x in str(y)]) > 1:
  23.         return digital_root(y)
  24.     else:
  25.         return y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement