aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-251/sgreen/python')
-rwxr-xr-xchallenge-251/sgreen/python/ch-1.py25
-rwxr-xr-xchallenge-251/sgreen/python/ch-2.py40
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]))