aboutsummaryrefslogtreecommitdiff
path: root/challenge-242/packy-anderson/python/ch-1.py
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2023-11-06 09:29:12 -0500
committerPacky Anderson <packy@cpan.org>2023-11-06 09:31:49 -0500
commit52d9817d77c3d699702e36868234ff8d34ab2437 (patch)
treeab3b5ac6f224578242890f3443f62c1addace600 /challenge-242/packy-anderson/python/ch-1.py
parenta82dd587d3773d2a36a1fcc4b3c525c031433a4a (diff)
downloadperlweeklychallenge-club-52d9817d77c3d699702e36868234ff8d34ab2437.tar.gz
perlweeklychallenge-club-52d9817d77c3d699702e36868234ff8d34ab2437.tar.bz2
perlweeklychallenge-club-52d9817d77c3d699702e36868234ff8d34ab2437.zip
Challenge 242 solutions by Packy Anderson
* Raku * Perl * Python 1 Blog post
Diffstat (limited to 'challenge-242/packy-anderson/python/ch-1.py')
-rwxr-xr-xchallenge-242/packy-anderson/python/ch-1.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-242/packy-anderson/python/ch-1.py b/challenge-242/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..8126d38ebf
--- /dev/null
+++ b/challenge-242/packy-anderson/python/ch-1.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def findMissing(source, target, output, explanation):
+ # convert the target into a map with each element as keys
+ targetMap = { x: 1 for x in target }
+
+ # see which elements in the source are not in the target
+ missing = []
+ for elem in source:
+ if not elem in targetMap:
+ missing.append(elem)
+
+ # format output explaining what we found
+ explanation += "\n(" + comma_join(source) + ") has "
+ explanation += str(len(missing))
+ explanation += ' member ' if len(missing) == 1 \
+ else ' members '
+ if (len(missing) > 0):
+ explanation += '(' + comma_join(missing) + ') '
+ output.append(set(missing))
+ explanation += 'missing from the array '
+ explanation += '(' + comma_join(target) + ')'
+ return explanation
+
+
+def findSolution(arr1, arr2, output):
+ explanation = ''
+ explanation = findMissing(arr1, arr2, output, explanation)
+ explanation = findMissing(arr2, arr1, output, explanation)
+ return explanation
+
+
+def solution(arr1, arr2):
+ print(f'Input: @arr1 = ({comma_join(arr1)})')
+ print(f' @arr2 = ({comma_join(arr2)})')
+
+ output = []
+ explanation = findSolution(arr1, arr2, output)
+ formatted_subarray = []
+ for subarray in output:
+ formatted_subarray.append('[' + comma_join(subarray) + ']')
+ print(f'Output: ({comma_join(formatted_subarray)})')
+ print(explanation)
+
+
+print('Example 1:')
+solution([1, 2, 3], [2, 4, 6])
+
+print('\nExample 2:')
+solution([1, 2, 3, 3], [1, 1, 2, 2]) \ No newline at end of file