aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/steven-wilson/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-320/steven-wilson/python/ch-2.py')
-rw-r--r--challenge-320/steven-wilson/python/ch-2.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-320/steven-wilson/python/ch-2.py b/challenge-320/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..5c5b4cbe35
--- /dev/null
+++ b/challenge-320/steven-wilson/python/ch-2.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+
+def sum_difference(integers):
+ """ Given an array of positive integers, return the absolute difference
+ between digit sum and element sum of the given array.
+
+ >>> sum_difference([1, 23, 4, 5])
+ 18
+ >>> sum_difference([1, 2, 3, 4, 5])
+ 0
+ >>> sum_difference([1, 2, 34])
+ 27
+ """
+ element_sum = sum(integers)
+ digit_sum = sum(int(i) for i in "".join(map(str, integers)))
+ return abs(digit_sum - element_sum)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)