aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/packy-anderson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-320/packy-anderson/python')
-rwxr-xr-xchallenge-320/packy-anderson/python/ch-1.py34
-rwxr-xr-xchallenge-320/packy-anderson/python/ch-2.py33
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-320/packy-anderson/python/ch-1.py b/challenge-320/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..8e103a1509
--- /dev/null
+++ b/challenge-320/packy-anderson/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+def maxCount(ints):
+ pos = 0
+ neg = 0
+ for int in ints:
+ if int > 0:
+ pos += 1
+ elif int < 0:
+ neg +=1
+ max_count = max(pos, neg)
+ explain = (
+ f"There are {pos} positive integers.\n" +
+ f"There are {neg} negative integers.\n" +
+ f"The maximum between {pos} and {neg} is {max_count}."
+ )
+ return max_count, explain
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ max_count, explain = maxCount(ints)
+ print(f'Output: {max_count}\n\n{explain}')
+
+print('Example 1:')
+solution([-3, -2, -1, 1, 2, 3])
+
+print('\nExample 2:')
+solution([-2, -1, 0, 0, 1])
+
+print('\nExample 3:')
+solution([1, 2, 3, 4])
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])