Created
August 2, 2013 06:05
-
-
Save tos-kamiya/6137798 to your computer and use it in GitHub Desktop.
Compute sum of single-digit numbers
This file contains 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
#coding: utf-8 | |
import sys | |
def count(v): | |
""" | |
Get sum of single-digit numbers in string. Use 'x' for 10. | |
>>> count("123") | |
6 | |
>>> count("123x") | |
16 | |
""" | |
sc = 0 | |
for d in v: | |
assert d in "0123456789xX" | |
sc += 10 if d in "xX" else (ord(d) - ord('0')) | |
return sc | |
if __name__ == '__main__': | |
sys.stdout.write("%d\n" % count(sys.argv[1])) | |
# python -m doctest -v ./sdc.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment