diff options
| author | Simon Green <mail@simon.green> | 2024-01-28 22:06:54 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-01-28 22:06:54 +1100 |
| commit | 7018e1db2d362e4b31cee9ec0c62d43ad0439bd2 (patch) | |
| tree | d4be33e63e5d64b6bd344caf9cf534bdc7d09e48 /challenge-253/sgreen/python | |
| parent | 9d7dc816f7775abfee7d90f0a2a969611902be54 (diff) | |
| download | perlweeklychallenge-club-7018e1db2d362e4b31cee9ec0c62d43ad0439bd2.tar.gz perlweeklychallenge-club-7018e1db2d362e4b31cee9ec0c62d43ad0439bd2.tar.bz2 perlweeklychallenge-club-7018e1db2d362e4b31cee9ec0c62d43ad0439bd2.zip | |
Simon's solution to challenge 253
Diffstat (limited to 'challenge-253/sgreen/python')
| -rwxr-xr-x | challenge-253/sgreen/python/ch-1.py | 20 | ||||
| -rwxr-xr-x | challenge-253/sgreen/python/ch-2.py | 41 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-253/sgreen/python/ch-1.py b/challenge-253/sgreen/python/ch-1.py new file mode 100755 index 0000000000..661c767134 --- /dev/null +++ b/challenge-253/sgreen/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import sys + + +def split_strings(words, sep): + # Join the words together by the separator + combined = sep.join(words) + + # Split them by the separator excluding empty strings + return [ w for w in combined.split(sep) if w != ''] + +def main(inputs): + # The separator is the last character + sep = inputs.pop() + result = split_strings(inputs, sep) + print('"' + '", "'.join(result) + '"') + +if __name__ == '__main__': + main(sys.argv[1:])
\ No newline at end of file diff --git a/challenge-253/sgreen/python/ch-2.py b/challenge-253/sgreen/python/ch-2.py new file mode 100755 index 0000000000..aad97b55d0 --- /dev/null +++ b/challenge-253/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def validate_matrix(matrix): + # Calculate the size of the matrix + rows = len(matrix) + cols = len(matrix[0]) + + for r in range(0, rows): + # Check that all columns are of equal length + if len(matrix[r]) != cols: + raise ValueError(f'Row {r} has different number of columns') + + # Check all values are 0 or 1 + if any(True for i in matrix[r] if i not in (0, 1)): + raise ValueError(f'Row {r} has a value other than 0 or 1') + + # Check there are no ones zeros after zero. + if any(True for c in range(1, cols) if matrix[r][c-1] == 0 and matrix[r][c] == 1): + raise ValueError(f'Row {r} has a 1 after 0') + + +def weakest_row(matrix): + # Start by validating any issues with the input + validate_matrix(matrix) + + # Sort by the rows with the most 1s, followed by a numerical sort + return sorted(range(len(matrix)), key=lambda i: sum(matrix[i])) + + +def main(json_string): + matrix = json.loads(json_string) + result = weakest_row(matrix) + print('(' + ', '.join(map(str, result)) + ')') + + +if __name__ == '__main__': + main(sys.argv[1]) |
