diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-05-15 16:45:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-15 16:45:20 -0400 |
| commit | efc2a652edb6436965e12883bedf035ea081284d (patch) | |
| tree | 7e9940d4f72fe1e8d984b3746af2d30dff96330b /challenge-320/packy-anderson/python/ch-2.py | |
| parent | 6886bd3e06ccf8b0a564f3c434cd53131d126f70 (diff) | |
| parent | ed7588dadb77d63adca3d3aefc6cd325164e0947 (diff) | |
| download | perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.tar.gz perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.tar.bz2 perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-320/packy-anderson/python/ch-2.py')
| -rwxr-xr-x | challenge-320/packy-anderson/python/ch-2.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-320/packy-anderson/python/ch-2.py b/challenge-320/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..07dfbd812c --- /dev/null +++ b/challenge-320/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def subDiff(ints): + element_sum = sum(ints) + digits = [ int(i) for i in list(int_join("", ints)) ] + digit_sum = sum(digits) + abs_diff = abs(element_sum - digit_sum) + int_list = int_join(" + ", ints) + digit_list = int_join(" + ", digits) + return ( + abs_diff, + f"Element sum: {int_list} => {element_sum}\n" + + f"Digit sum: {digit_list} => {digit_sum}\n" + + f"Absolute difference: | {element_sum} - {digit_sum} | " + + f"=> {abs_diff}" + ) + +def solution(ints): + print(f'Input: @ints = ({int_join(", ", ints)})') + diff, explain = subDiff(ints) + print(f'Output: {diff}\n\n{explain}') + +print('Example 1:') +solution([1, 23, 4, 5]) + +print('\nExample 2:') +solution([1, 2, 3, 4, 5]) + +print('\nExample 3:') +solution([1, 2, 34]) |
