aboutsummaryrefslogtreecommitdiff
path: root/challenge-266/packy-anderson/python/ch-2.py
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2024-04-24 01:10:11 -0400
committerPacky Anderson <packy@cpan.org>2024-04-24 01:10:11 -0400
commitd8ae12d13bdb74b6f9ce88dd3c8732dfe3984e18 (patch)
tree05472ced28aef27e3c1a6f6983dfd5ddd151b007 /challenge-266/packy-anderson/python/ch-2.py
parent25c88efc3e83fc333e98da6a0157e1853be61044 (diff)
downloadperlweeklychallenge-club-d8ae12d13bdb74b6f9ce88dd3c8732dfe3984e18.tar.gz
perlweeklychallenge-club-d8ae12d13bdb74b6f9ce88dd3c8732dfe3984e18.tar.bz2
perlweeklychallenge-club-d8ae12d13bdb74b6f9ce88dd3c8732dfe3984e18.zip
Challenge 266 solutions by Packy Anderson
* Raku that looks like Perl * Perl * Python that looks like Perl 1 Blog post
Diffstat (limited to 'challenge-266/packy-anderson/python/ch-2.py')
-rwxr-xr-xchallenge-266/packy-anderson/python/ch-2.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/challenge-266/packy-anderson/python/ch-2.py b/challenge-266/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..0cefeed570
--- /dev/null
+++ b/challenge-266/packy-anderson/python/ch-2.py
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+
+def isDiagonal(x, y, N):
+ return (
+ N == 1 or N == 2 or x == y or x + y == N - 1
+ )
+
+def isXMatrix(matrix):
+ N = len(matrix)
+ for y, row in enumerate(matrix):
+ for x, value in enumerate(row):
+ # fail if diagonal values are zero or
+ # non-diagonal values are non-zero
+ if isDiagonal(x, y, N) != (value != 0):
+ return False
+ return True
+
+def formatMatrix(matrix, indent=17):
+ output = []
+ for row in matrix:
+ output_row = ' ' * indent + ' ['
+ output_row += ', '.join(map(lambda i: str(i), row))
+ output_row += ']'
+ output.append(output_row)
+
+ return(
+ "[\n" + ",\n".join(output) + "\n" +
+ ' ' * indent + ']'
+ )
+
+def solution(matrix):
+ print(f'Input: $matrix = {formatMatrix(matrix)}')
+ print(f'Output: {isXMatrix(matrix)}')
+
+print('Example 1:')
+solution([
+ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+])
+
+print('\nExample 2:')
+solution([
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+])
+
+print('\nExample 3:')
+solution([
+ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+])
+
+print('\nExample 4:')
+solution([
+ [1, 0, 0, 0, 1],
+ [0, 1, 0, 1, 0],
+ [0, 0, 1, 0, 0],
+ [0, 1, 0, 1, 0],
+ [1, 0, 0, 0, 1],
+])
+
+print('\nExample 5:')
+solution([
+ [1, 0, 1, 0, 1],
+ [0, 1, 0, 1, 0],
+ [0, 0, 1, 0, 0],
+ [0, 1, 0, 1, 0],
+ [1, 0, 0, 0, 1],
+])
+
+print('\nExample 6:')
+solution([
+ [1, 1],
+ [1, 1]
+])
+
+print('\nExample 7:')
+solution([
+ [1, 0],
+ [1, 1]
+])