Skip to content

Instantly share code, notes, and snippets.

@jettero
Created September 12, 2017 12:36
Show Gist options
  • Save jettero/7fc0d911bb8a1e7675820dcdfbd9201c to your computer and use it in GitHub Desktop.
Save jettero/7fc0d911bb8a1e7675820dcdfbd9201c to your computer and use it in GitHub Desktop.
This is 9.
#!/usr/bin/env python
# coding: UTF-8
import argparse
def nine(x):
retval = x # <--- the given integer
# side note: recursion can always be converted to a loop
# particularly tail recursion like this would have been
while retval > 10:
# the sum of all the digits
digit_total = sum([ int(i) for i in str(retval) ])
# subtract the sum of the digits of the current return value
retval -= digit_total
return retval # <--- this is 9
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="this is 9")
parser.add_argument("some_number", type=int)
args = parser.parse_args()
if int( args.some_number ) < 10:
print "given number must be a positive integer greater than 9"
exit(1)
print nine(args.some_number)
@jettero
Copy link
Author

jettero commented Jun 30, 2018

~$ ~/code/python/nine.py 72
9
~$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment