diff options
Diffstat (limited to 'challenge-333/sgreen/python')
| -rwxr-xr-x | challenge-333/sgreen/python/ch-1.py | 42 | ||||
| -rwxr-xr-x | challenge-333/sgreen/python/ch-2.py | 25 | ||||
| -rwxr-xr-x | challenge-333/sgreen/python/test.py | 32 |
3 files changed, 99 insertions, 0 deletions
diff --git a/challenge-333/sgreen/python/ch-1.py b/challenge-333/sgreen/python/ch-1.py new file mode 100755 index 0000000000..fa37695004 --- /dev/null +++ b/challenge-333/sgreen/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import sys + + +def straight_line(points: list[list[int]]) -> bool: + """ + Check if all points lie on a straight line. + + Args: + points (list[list[int]]): A list of points, where each point is represented as a list of two integers [x, y]. + + Returns: + bool: True if all points lie on a straight line, False otherwise. + """ + + # Check for a flat line (to avoid division by zero error) + if all(points[0][1] == points[i][1] for i in range(1, len(points))): + return True + + # Check for only some points being a flat line + if any(points[0][1] == points[i][1] for i in range(1, len(points))): + return False + + degrees = set( + abs((points[0][0] - points[i][0]) / (points[0][1] - points[i][1])) + for i in range(1, len(points)) + ) + + return True if len(degrees) == 1 else False + + +def main(): + # Convert input into integers, and then into pairs of points + array = [int(n) for n in sys.argv[1:]] + points = [[array[i], array[i + 1]] for i in range(0, len(array), 2)] + result = straight_line(points) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-333/sgreen/python/ch-2.py b/challenge-333/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f0662513ed --- /dev/null +++ b/challenge-333/sgreen/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import sys + + +def duplicate_zeros(ints: list) -> list: + solution = [] + for i in ints: + if i == 0: + # Duplicate the zero + solution.append(0) + solution.append(i) + + return solution[:len(ints)] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = duplicate_zeros(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-333/sgreen/python/test.py b/challenge-333/sgreen/python/test.py new file mode 100755 index 0000000000..be4233ee2b --- /dev/null +++ b/challenge-333/sgreen/python/test.py @@ -0,0 +1,32 @@ +#!/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.assertTrue(ch_1.straight_line([[2, 1], [2, 3], [2, 5]])) + self.assertTrue(ch_1.straight_line([[1, 4], [3, 4], [10, 4]])) + self.assertFalse(ch_1.straight_line([[0, 0], [1, 1], [2, 3]])) + self.assertTrue(ch_1.straight_line([[1, 1], [1, 1], [1, 1]])) + self.assertTrue( + ch_1.straight_line( + [[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]] + ) + ) + + def test_ch_2(self): + self.assertEqual( + ch_2.duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0]), + [1, 0, 0, 2, 3, 0, 0, 4] + ) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 3]), [1, 2, 3]) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 3, 0]), [1, 2, 3, 0]) + self.assertEqual(ch_2.duplicate_zeros([0, 0, 1, 2]), [0, 0, 0, 0]) + self.assertEqual(ch_2.duplicate_zeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3]) + + +if __name__ == '__main__': + unittest.main() |
