Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Created August 2, 2013 06:05
Show Gist options
  • Save tos-kamiya/6137798 to your computer and use it in GitHub Desktop.
Save tos-kamiya/6137798 to your computer and use it in GitHub Desktop.
Compute sum of single-digit numbers
#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