aboutsummaryrefslogtreecommitdiff
path: root/challenge-286/packy-anderson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-286/packy-anderson/python')
-rwxr-xr-xchallenge-286/packy-anderson/python/ch-1.py9
-rwxr-xr-xchallenge-286/packy-anderson/python/ch-2.py49
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-286/packy-anderson/python/ch-1.py b/challenge-286/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..bfe31a7b8b
--- /dev/null
+++ b/challenge-286/packy-anderson/python/ch-1.py
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+
+from random import randrange
+
+words = []
+with open(__file__) as fh:
+ words.extend(fh.read().split())
+
+print(f'{ words[ randrange( len(words) ) ] }') \ No newline at end of file
diff --git a/challenge-286/packy-anderson/python/ch-2.py b/challenge-286/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..6047131b92
--- /dev/null
+++ b/challenge-286/packy-anderson/python/ch-2.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+def pairwise(iterable):
+ # pairwise('ABCDEFGH') → AB CD EF GH
+ iterator = iter(iterable)
+ a = next(iterator, None)
+ for b in iterator:
+ yield a, b
+ a = next(iterator, None)
+
+def orderGame(ints):
+ loop = 0
+ explain = []
+ while (len(ints) > 1):
+ new = []
+ loop += 1
+ explain.append(f"Operation {loop}:")
+ explain.append("")
+ flip = 1
+ for a, b in pairwise(ints):
+ if (flip == 1):
+ val = min(a, b)
+ func = 'min'
+ else: # $flip is -1
+ val = max(a, b)
+ func = 'max'
+ new.append(val)
+ explain.append(f" {func}({a}, {b}) = {val}")
+ flip *= -1; # switch from min to max and back
+ explain.append("")
+ ints = new # replace list with new list
+ return ints[0], "\n".join(explain)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ output, explain = orderGame(ints)
+ print(f'Output: {output}\n\n{explain}')
+
+print('Example 1:')
+solution([2, 1, 4, 5, 6, 3, 0, 2])
+
+print('\nExample 2:')
+solution([0, 5, 3, 2])
+
+print('\nExample 3:')
+solution([9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8])