aboutsummaryrefslogtreecommitdiff
path: root/challenge-268/packy-anderson/python
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2024-05-07 01:26:42 -0400
committerPacky Anderson <packy@cpan.org>2024-05-07 01:26:42 -0400
commit9df1740d3165b92d26f3e39a019cbabd6141d672 (patch)
tree3693ecbdd4c39673cfb6f970503741dd02adcdce /challenge-268/packy-anderson/python
parent8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b (diff)
downloadperlweeklychallenge-club-9df1740d3165b92d26f3e39a019cbabd6141d672.tar.gz
perlweeklychallenge-club-9df1740d3165b92d26f3e39a019cbabd6141d672.tar.bz2
perlweeklychallenge-club-9df1740d3165b92d26f3e39a019cbabd6141d672.zip
Challenge 268 solutions by Packy Anderson
* Raku that maybe looks like Raku * Perl * Python that definitely looks like Perl 1 Blog post
Diffstat (limited to 'challenge-268/packy-anderson/python')
-rwxr-xr-xchallenge-268/packy-anderson/python/ch-1.py39
-rwxr-xr-xchallenge-268/packy-anderson/python/ch-2.py30
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-268/packy-anderson/python/ch-1.py b/challenge-268/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..902a1fe2fb
--- /dev/null
+++ b/challenge-268/packy-anderson/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+def magicNumber(x, y):
+ xS = sorted(x)
+ yS = sorted(y)
+ magic = yS.pop(0) - xS.pop(0)
+ while xS:
+ if yS.pop(0) - xS.pop(0) != magic:
+ return None; # no magic number
+ return magic
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(x, y):
+ print(f'Input: @x = ({comma_join(x)})')
+ print(f' @y = ({comma_join(y)})')
+ magic = magicNumber(x, y)
+ if magic is None:
+ print('Output: no magic number')
+ else:
+ print(f'Output: {magic}\n')
+ print(f'@x = ({comma_join(x)})')
+ magicStr = ' '.join(map(lambda i: str(magic), x))
+ print(f' + {magicStr}')
+ yStr = ', '.join(map(lambda i: str(i+magic), x))
+ print(f'@y = ({yStr}')
+
+print('Example 1:')
+solution([3, 7, 5], [9, 5, 7])
+
+print('\nExample 2:')
+solution([1, 2, 1], [5, 4, 4])
+
+print('\nExample 3:')
+solution([2], [5])
+
+print('\nExample 4:')
+solution([1, 2], [4, 2]) \ No newline at end of file
diff --git a/challenge-268/packy-anderson/python/ch-2.py b/challenge-268/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..51acaf6615
--- /dev/null
+++ b/challenge-268/packy-anderson/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+def numberGame(ints):
+ intSorted = sorted(ints)
+ new = []
+ while intSorted:
+ x = intSorted.pop(0)
+ y = intSorted.pop(0)
+ if x > y:
+ new.extend([x, y])
+ else:
+ new.extend([y, x])
+ return new
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ new = numberGame(ints)
+ print(f'Output: ({comma_join(new)})')
+
+print('Example 1:')
+solution([2, 5, 3, 4])
+
+print('\nExample 2:')
+solution([9, 4, 1, 3, 6, 4, 6, 1])
+
+print('\nExample 3:')
+solution([1, 2, 2, 3]) \ No newline at end of file