Created
May 24, 2016 02:44
-
-
Save davidmfoley/3437b720d3ace085d47fd3f7a46a0b47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def digit(number): | |
| """Add digits as long as there is more than one digit | |
| >>> digit(2) | |
| 2 | |
| >>> digit(12) | |
| 4 | |
| >>> digit(99) | |
| 9 | |
| >>> digit(83928) | |
| 3 | |
| >>> digit(123456789) | |
| 9 | |
| >>> digit(99999999999) | |
| 9 | |
| """ | |
| if number < 10: | |
| return number | |
| ones = (number % 10) | |
| tens = (number / 10) | |
| return digit(tens + ones) | |
| def digit_no_recursion(number): | |
| """Add digits as long as there is more than one digit | |
| >>> digit_no_recursion(2) | |
| 2 | |
| >>> digit_no_recursion(12) | |
| 3 | |
| >>> digit_no_recursion(99) | |
| 9 | |
| >>> digit_no_recursion(83928) | |
| 3 | |
| >>> digit_no_recursion(123456789) | |
| 9 | |
| >>> digit_no_recursion(99999999999) | |
| 9 | |
| """ | |
| while number > 10: | |
| number = (number / 10) + (number % 10) | |
| return number | |
| if __name__ == '__main__': | |
| import doctest | |
| doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment