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/packy-anderson/python | |
| 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/packy-anderson/python')
| -rwxr-xr-x | challenge-242/packy-anderson/python/ch-1.py | 53 | ||||
| -rwxr-xr-x | challenge-242/packy-anderson/python/ch-2.py | 28 |
2 files changed, 81 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 diff --git a/challenge-242/packy-anderson/python/ch-2.py b/challenge-242/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..dd5dc075ab --- /dev/null +++ b/challenge-242/packy-anderson/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +def flipMatrix(matrix): + for index in range(0, len(matrix)): + matrix[index] = map( + lambda i: abs(i - 1), reversed(matrix[index]) + ) + return matrix + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def formatArray(matrix): + formatted = [] + for subarray in matrix: + formatted.append('[' + comma_join(subarray) + ']') + return '(' + comma_join(formatted) + ')' + +def solution(matrix): + print(f'Input: @matrix = {formatArray(matrix)}') + output = flipMatrix(matrix) + print(f'Output: {formatArray(output)}') + +print('Example 1:') +solution([[1, 1, 0], [1, 0, 1], [0, 0, 0]]) + +print('\nExample 2:') +solution([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]])
\ No newline at end of file |
