diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-14 12:18:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-14 12:18:28 +0000 |
| commit | a032c84e79bf314d6118fcd6ebb15fdcc63c8998 (patch) | |
| tree | 92dc95c7aa0b288fe730fde93d3fde3899ee09f7 /challenge-251/sgreen/python | |
| parent | 53668b050d994a10fdebda5fbafba13ce8f74bc5 (diff) | |
| parent | 74305a8889574d47d371f158170892e1697fa13f (diff) | |
| download | perlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.tar.gz perlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.tar.bz2 perlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.zip | |
Merge pull request #9390 from simongreen-net/master
Simon's solution to challenge 251
Diffstat (limited to 'challenge-251/sgreen/python')
| -rwxr-xr-x | challenge-251/sgreen/python/ch-1.py | 25 | ||||
| -rwxr-xr-x | challenge-251/sgreen/python/ch-2.py | 40 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-251/sgreen/python/ch-1.py b/challenge-251/sgreen/python/ch-1.py new file mode 100755 index 0000000000..258ef4da0d --- /dev/null +++ b/challenge-251/sgreen/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + solution = 0 + half = len(ints) // 2 + + # If we have an odd number of integers, use the middle value + if len(ints) % 2 == 1: + solution += ints[half] + + # Combine the concatenation of the remaining integers, starting with first + # and last, then second and second last, and so on. + for i in range(half): + solution += int(str(ints[i]) + str(ints[-1-i])) + + print(solution) + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) diff --git a/challenge-251/sgreen/python/ch-2.py b/challenge-251/sgreen/python/ch-2.py new file mode 100755 index 0000000000..643760029c --- /dev/null +++ b/challenge-251/sgreen/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def main(matrix): + # Calculate the size of the matrix + rows = len(matrix) + cols = len(matrix[0]) + + # Check that all columns are of equal length + for r in range(1, rows): + if len(matrix[r]) != cols: + raise ValueError(f'Row {r} has different number of columns') + + # Calculate the column maximums + column_max = [] + for c in range(cols): + column_max.append(max(map(lambda i: i[c], matrix))) + + solution = -1 + + # Go through each row + for r in matrix: + # Get the value of all columns that are the minimum value for this row + # and the maximum for the column + row_min = min(r) + if any(True for i, value in enumerate(r) + if value == row_min and column_max[i] == value): + # We have a lucky number (row_min) + solution = row_min + break + + print(solution) + + +if __name__ == '__main__': + # Read the input as JSON + main(json.loads(sys.argv[1])) |
