aboutsummaryrefslogtreecommitdiff
path: root/challenge-288/sgreen/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-29 19:41:10 +0100
committerGitHub <noreply@github.com>2024-09-29 19:41:10 +0100
commit994174d6863980e2fd017bc613671453c7f9368e (patch)
treefc01a7b6d382f206d16208ca14b7ee5b20e95a1c /challenge-288/sgreen/python
parent9437b5a4b5fa7e256f5afd0e347ccc81149bc136 (diff)
parent84ff106a8fcdd33662eaa42e6f196f390eda7e3b (diff)
downloadperlweeklychallenge-club-994174d6863980e2fd017bc613671453c7f9368e.tar.gz
perlweeklychallenge-club-994174d6863980e2fd017bc613671453c7f9368e.tar.bz2
perlweeklychallenge-club-994174d6863980e2fd017bc613671453c7f9368e.zip
Merge pull request #10918 from simongreen-net/master
sgreen solutions to challenge 288
Diffstat (limited to 'challenge-288/sgreen/python')
-rwxr-xr-xchallenge-288/sgreen/python/ch-1.py31
-rwxr-xr-xchallenge-288/sgreen/python/ch-2.py82
-rwxr-xr-xchallenge-288/sgreen/python/test.py43
3 files changed, 156 insertions, 0 deletions
diff --git a/challenge-288/sgreen/python/ch-1.py b/challenge-288/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..eff97cae4b
--- /dev/null
+++ b/challenge-288/sgreen/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def closest_palindrome(i: int) -> int:
+ # Negative integers always have a closest palindrome of 0
+ if i < 0:
+ return 0
+
+ offset = 1
+ while True:
+ check = i - offset
+ if str(check) == str(check)[::-1]:
+ return check
+
+ check = i + offset
+ if str(check) == str(check)[::-1]:
+ return check
+
+ offset += 1
+
+
+def main():
+ # Convert input into integers
+ result = closest_palindrome(int(sys.argv[1]))
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-288/sgreen/python/ch-2.py b/challenge-288/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..631498fb3f
--- /dev/null
+++ b/challenge-288/sgreen/python/ch-2.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def find_block(matrix, seen, row, col):
+ rows = len(matrix)
+ cols = len(matrix[0])
+
+ # The character that we need to match
+ char = matrix[row][col]
+
+ # The directions we can move: Up, down, left, right
+ directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
+
+ # Mark the we have seen the starting cell
+ seen[row][col] = True
+
+ # Add the starting cell to the stack
+ stack = [[row, col]]
+ count = 1
+
+ while stack:
+ # Whether we found a move from the last value in the stack
+ new_pos = False
+
+ for move in directions:
+ # Move in each direction
+ new_row = stack[-1][0] + move[0]
+ new_col = stack[-1][1] + move[1]
+
+ # Check that the move is still in bounds of the matrix, we haven't
+ # already use that position, and it is the correct character
+ if new_row < 0 or new_row >= rows or new_col < 0 or new_col >= cols or seen[new_row][new_col] or matrix[new_row][new_col] != char:
+ continue
+
+ # It is, add it to the stack, and increment the count
+ stack.append([new_row, new_col])
+ seen[new_row][new_col] = True
+ new_pos = True
+ count += 1
+
+ if not new_pos:
+ # We didn't find a new move, remove it from that stack.
+ stack.pop()
+
+ return count
+
+
+def contiguous_block(matrix: list) -> int:
+ rows = len(matrix)
+ cols = len(matrix[0])
+ max_block = 0
+
+ # Seed the seen table
+ seen = []
+ for i in range(rows):
+ seen.append([0] * cols)
+
+ for row in range(rows):
+ for col in range(cols):
+ if seen[row][col]:
+ # The item at this position has already been used
+ continue
+
+ # Find how many items in this block. If greater than max_block, replace it
+ count = find_block(matrix, seen, row, col)
+ if count > max_block:
+ max_block = count
+
+ return max_block
+
+
+def main():
+ matrix = json.loads(sys.argv[1])
+ result = contiguous_block(matrix)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-288/sgreen/python/test.py b/challenge-288/sgreen/python/test.py
new file mode 100755
index 0000000000..51af278fbe
--- /dev/null
+++ b/challenge-288/sgreen/python/test.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.closest_palindrome(123), 121)
+ self.assertEqual(ch_1.closest_palindrome(2), 1)
+ self.assertEqual(ch_1.closest_palindrome(1400), 1441)
+ self.assertEqual(ch_1.closest_palindrome(1001), 999)
+
+ def test_ch_2(self):
+ matrix_1 = [
+ ['x', 'x', 'x', 'x', 'o'],
+ ['x', 'o', 'o', 'o', 'o'],
+ ['x', 'o', 'o', 'o', 'o'],
+ ['x', 'x', 'x', 'o', 'o'],
+ ]
+
+ matrix_2 = [
+ ['x', 'x', 'x', 'x', 'x'],
+ ['x', 'o', 'o', 'o', 'o'],
+ ['x', 'x', 'x', 'x', 'o'],
+ ['x', 'o', 'o', 'o', 'o'],
+ ]
+
+ matrix_3 = [
+ ['x', 'x', 'x', 'o', 'o'],
+ ['o', 'o', 'o', 'x', 'x'],
+ ['o', 'x', 'x', 'o', 'o'],
+ ['o', 'o', 'o', 'x', 'x'],
+ ]
+
+ self.assertEqual(ch_2.contiguous_block(matrix_1), 11)
+ self.assertEqual(ch_2.contiguous_block(matrix_2), 11)
+ self.assertEqual(ch_2.contiguous_block(matrix_3), 7)
+
+
+if __name__ == '__main__':
+ unittest.main()