diff options
| author | librasteve <40125330+librasteve@users.noreply.github.com> | 2023-11-07 20:11:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-07 20:11:22 +0000 |
| commit | 89f6be07a89a98d25f352f18850f24bf0ff7d985 (patch) | |
| tree | 032ab890354b0bc8b876ee9e0ba23cda7f244d1a /challenge-242/jeanluc2020/python/ch-1.py | |
| parent | c9d5f2834f979267d8d5db8fdaba52504a0c0b95 (diff) | |
| parent | db4d9fe6bf77ff58c31ec3e9d2f71d8acf7d58d4 (diff) | |
| download | perlweeklychallenge-club-89f6be07a89a98d25f352f18850f24bf0ff7d985.tar.gz perlweeklychallenge-club-89f6be07a89a98d25f352f18850f24bf0ff7d985.tar.bz2 perlweeklychallenge-club-89f6be07a89a98d25f352f18850f24bf0ff7d985.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-242/jeanluc2020/python/ch-1.py')
| -rwxr-xr-x | challenge-242/jeanluc2020/python/ch-1.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-242/jeanluc2020/python/ch-1.py b/challenge-242/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..f17e000f6f --- /dev/null +++ b/challenge-242/jeanluc2020/python/ch-1.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK1 +# +# Task 1: Missing Members +# ======================= +# +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +## Example 1 +## +## Input: @arr1 = (1, 2, 3) +## @arr2 = (2, 4, 6) +## Output: ([1, 3], [4, 6]) +## +## (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +## (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# +## Example 2 +## +## Input: @arr1 = (1, 2, 3, 3) +## @arr2 = (1, 1, 2, 2) +## Output: ([3]) +## +## (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +## (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of each array, if it is not in the other array, +# save it for the output. +# Example 2 has a caveat though, by not returning an empty array in +# case of no missing elements it is unclear in which of the arrays +# the element was originally. It's better to return an empty array +# here, so let's just do that instead. + +def missing_members(arr1: list, arr2: list): + seen: dict = {} + res1: list = [] + res2: list = [] + print("Input: ([", ", ".join(str(x) for x in arr1), "], [", ", ".join(str(x) for x in arr2), "])") + for elem in arr1: + if elem in seen: + next + else: + if elem not in arr2: + seen[elem] = 1 + res1.append(elem) + seen = {} + for elem in arr2: + if elem in seen: + next + else: + if elem not in arr1: + seen[elem] = 1 + res2.append(elem) + print("Output: ([", ", ".join(str(x) for x in res1), "], [", ", ".join(str(x) for x in res2), "])") + +missing_members( [ 1, 2, 3 ], [ 2, 4, 6 ] ); +missing_members( [ 1, 2, 3, 3 ], [ 1, 1, 2, 2 ] ); + |
