aboutsummaryrefslogtreecommitdiff
path: root/challenge-336/packy-anderson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-336/packy-anderson/python')
-rwxr-xr-xchallenge-336/packy-anderson/python/ch-1.py75
-rwxr-xr-xchallenge-336/packy-anderson/python/ch-2.py53
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-336/packy-anderson/python/ch-1.py b/challenge-336/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..e8f3514075
--- /dev/null
+++ b/challenge-336/packy-anderson/python/ch-1.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def divides_unevenly(smallest, bag):
+ return [ n for n in bag.values() if n % smallest != 0 ]
+
+def equal_group(ints):
+ bag = Counter(ints)
+
+ # if we don't have more than 2 instances of
+ # a particular int, we can't make groups
+ if [ n for n in bag.values() if n < 2 ]:
+ return False, ""
+
+ # find the smallest number of instances of
+ # an int in the list
+ smallest = min(bag.values())
+
+ # can we divide the list evenly into multiples
+ # of the smallest group?
+ while divides_unevenly(smallest, bag) and smallest > 2:
+ smallest = int(smallest / 2)
+
+ # make the groups and return the result
+ groups = ""
+ if divides_unevenly(smallest, bag):
+ for k in bag.keys():
+ group = []
+ for i in range(bag[k]): group.append(k)
+ groups += "(" + int_join(",",group) + ") "
+ return False, groups
+ else:
+ for k in bag.keys():
+ count = bag[k]
+ while count > 0:
+ group = []
+ for i in range(smallest): group.append(k)
+ groups += "(" + int_join(",",group) + ") "
+ count -= smallest
+ return True, groups
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(",", ints)})')
+ result, groups = equal_group(ints)
+ print(f'Output: {result}')
+ if groups != "":
+ print(f'\nGroups: {groups}')
+
+print('Example 1:')
+solution([1,1,2,2,2,2])
+
+print('\nExample 2:')
+solution([1,1,1,2,2,2,3,3])
+
+print('\nExample 3:')
+solution([5,5,5,5,5,5,7,7,7,7,7,7])
+
+print('\nExample 4:')
+solution([1,2,3,4])
+
+print('\nExample 5:')
+solution([8,8,9,9,10,10,11,11])
+
+print('\nExample 6 (2a):')
+solution([1,1,1,1,2,2,2,2,3,3])
+
+print('\nExample 7 (2b):')
+solution([1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3])
+
+print('\nExample 8 (3a):')
+solution([5,5,5,5,5,7,7,7,7,7,7]) \ No newline at end of file
diff --git a/challenge-336/packy-anderson/python/ch-2.py b/challenge-336/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..4d43d4745b
--- /dev/null
+++ b/challenge-336/packy-anderson/python/ch-2.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+
+def final_score(scores):
+ stack = []
+ rounds = ""
+ count = 0
+ for action in scores:
+ if stack: previous = stack[-1]
+ match action:
+ case '+':
+ stack.append( stack[-1] + stack[-2] )
+ case 'C':
+ stack.pop()
+ case 'D':
+ stack.append( stack[-1] * 2 )
+ case _:
+ stack.append( int(action) )
+ count += 1
+ rounds += "Round {:2d}: ".format(count)
+ rounds += " + ".join([
+ f'({n})' if n < 0 else f'{n}' for n in stack
+ ])
+ if action == "+":
+ rounds += " (sum of previous two scores)"
+ if action == "D":
+ rounds += f" (double the previous score {previous})"
+ if action == "C":
+ rounds += f" (invalidate the previous score {previous})"
+ rounds += "\n"
+ total = sum(stack)
+ rounds += f"\nTotal Scores: {total}"
+ return (total, rounds)
+
+def solution(scores):
+ score_list = ', '.join([ f'"{s}"' for s in scores ])
+ print(f'Input: @scores = ({score_list})')
+ output, rounds = final_score(scores)
+ print(f'Output: {output}\n\n{rounds}')
+
+print('Example 1:')
+solution(["5","2","C","D","+"])
+
+print('\nExample 2:')
+solution(["5","-2","4","C","D","9","+","+"])
+
+print('\nExample 3:')
+solution(["7","D","D","C","+","3"])
+
+print('\nExample 4:')
+solution(["-5","-10","+","D","C","+"])
+
+print('\nExample 5:')
+solution(["3","6","+","D","C","8","+","D","-2","C","+"])