From 52d9817d77c3d699702e36868234ff8d34ab2437 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 6 Nov 2023 09:29:12 -0500 Subject: Challenge 242 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-242/packy-anderson/python/ch-2.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 challenge-242/packy-anderson/python/ch-2.py (limited to 'challenge-242/packy-anderson/python/ch-2.py') 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 -- cgit