Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/util.py
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-02-11 15:43:55 +0100
committerArthur de Jong <arthur@arthurdejong.org>2012-02-11 15:43:55 +0100
commita574e6c21caf9c6f41198ef5442fccc5d5c4d53c (patch)
tree3175174ae83d8cf2a1ed12954f0e12be441aa899 /stdnum/util.py
parent84d1ee771be812f258828d272a1c441158a5ea47 (diff)
implement a digitsum() function to find the sub of all digits in a number
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@113 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum/util.py')
-rw-r--r--stdnum/util.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/stdnum/util.py b/stdnum/util.py
index 7cc26ae..b22c2aa 100644
--- a/stdnum/util.py
+++ b/stdnum/util.py
@@ -17,13 +17,23 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
-"""Common functions for other stdnum modules.
-
->>> clean('123-456:78 9', ' -:')
-'123456789'
-"""
+"""Common functions for other stdnum modules."""
def clean(number, deletechars):
- """Remove the specified characters from the supplied number."""
+ """Remove the specified characters from the supplied number.
+
+ >>> clean('123-456:78 9', ' -:')
+ '123456789'
+ """
return ''.join(x for x in number if x not in deletechars)
+
+
+def digitsum(numbers):
+ """Returns the sum of the individual digits of the provided numbers.
+
+ >>> digitsum([12, 55])
+ 13
+ """
+ # note: this only works for two-digit numbers
+ return sum((x // 10) + (x % 10) for x in numbers)