aboutsummaryrefslogtreecommitdiff
path: root/challenge-170/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-06-24 23:17:16 +1000
committerSimon Green <mail@simon.green>2022-06-24 23:17:16 +1000
commitcdd967fb112b2fe41e9b9909b43b35c2fce76dc7 (patch)
tree2de1a3eb323a73dad70a63ed7c20dbe7521b27cf /challenge-170/sgreen/python/ch-2.py
parentbb6f23fd223e77e35e199fbca2cc224ad21f0f1c (diff)
downloadperlweeklychallenge-club-cdd967fb112b2fe41e9b9909b43b35c2fce76dc7.tar.gz
perlweeklychallenge-club-cdd967fb112b2fe41e9b9909b43b35c2fce76dc7.tar.bz2
perlweeklychallenge-club-cdd967fb112b2fe41e9b9909b43b35c2fce76dc7.zip
sgreen solutions to challenge 170
Diffstat (limited to 'challenge-170/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-170/sgreen/python/ch-2.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-170/sgreen/python/ch-2.py b/challenge-170/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..fc0700a967
--- /dev/null
+++ b/challenge-170/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+import sys
+import yaml
+
+
+def check_array(a):
+ ''' Check that all rows in the array are the same length'''
+ for i in range(1, len(a)):
+ if (len(a[0]) != len(a[i])):
+ raise ValueError('mismatched array width')
+
+ return a
+
+
+def main(file_name):
+ # Read the supplied file in YAML format
+ with open(file_name, 'r') as file:
+ arrays = yaml.safe_load(file)
+
+ if len(arrays) != 2:
+ raise ValueError('Expecting two arrays')
+
+ array1 = check_array(arrays[0])
+ array2 = check_array(arrays[1])
+
+ solution = []
+ for row in array1:
+ for second_row in array2:
+ solution.append(list( str(c1 * c2) for c1 in row for c2 in second_row))
+
+ # Format each row uniformly.
+ max_length = max(max(len(col) for col in row) for row in solution)
+ fmt = '[' + (' {:>' + str(max_length) + 's}') * len(solution[0]) + ' ]'
+
+ for row in solution:
+ print(fmt.format(*row))
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])