aboutsummaryrefslogtreecommitdiff
path: root/challenge-268/sgreen/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-13 00:29:17 +0100
committerGitHub <noreply@github.com>2024-05-13 00:29:17 +0100
commit6bb7b79de03a9327f2850fdbb4298e08b3bef0bc (patch)
tree9b4ef68693be95141f96d0fd5313dbcc8734eb61 /challenge-268/sgreen/python
parentc5fcf2a0860873e8486191af5fa2c9ef8a39001c (diff)
parent9c4b1bbba36ae9f46e2d8d6d1d4cef3e21572e87 (diff)
downloadperlweeklychallenge-club-6bb7b79de03a9327f2850fdbb4298e08b3bef0bc.tar.gz
perlweeklychallenge-club-6bb7b79de03a9327f2850fdbb4298e08b3bef0bc.tar.bz2
perlweeklychallenge-club-6bb7b79de03a9327f2850fdbb4298e08b3bef0bc.zip
Merge pull request #10077 from simongreen-net/master
sgreen solutions to challenge 268
Diffstat (limited to 'challenge-268/sgreen/python')
-rwxr-xr-xchallenge-268/sgreen/python/ch-1.py39
-rwxr-xr-xchallenge-268/sgreen/python/ch-2.py29
-rwxr-xr-xchallenge-268/sgreen/python/test.py24
3 files changed, 92 insertions, 0 deletions
diff --git a/challenge-268/sgreen/python/ch-1.py b/challenge-268/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..5566e1437b
--- /dev/null
+++ b/challenge-268/sgreen/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def magic_number(x: list, y: list) -> int | None:
+ # Check we have equal length lists
+ if len(x) != len(y):
+ raise ValueError('Lists must be of the same length')
+
+ # Sort the lists
+ x = sorted(x)
+ y = sorted(y)
+
+ # Calculate the difference between the first values
+ diff = y[0] - x[0]
+
+ for i in range(len(x)):
+ if y[i] - x[i] != diff:
+ # There is no single magic number
+ return None
+
+ # Return the magic number
+ return diff
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ half = len(array) // 2
+ result = magic_number(array[:half], array[half:])
+ if result:
+ print(result)
+ else:
+ print('No magic number found!')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-268/sgreen/python/ch-2.py b/challenge-268/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d0abc6522c
--- /dev/null
+++ b/challenge-268/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def numbers_game(ints: list) -> list:
+ # Check we have an even number of items
+ if len(ints) % 2 != 0:
+ raise ValueError('Please provide an even number of items!')
+
+ # Sort the list
+ ints = sorted(ints)
+
+ for i in range(0, len(ints), 2):
+ # Switch the pairs of numbers around
+ ints[i], ints[i+1], = ints[i+1], ints[i]
+
+ return ints
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = numbers_game(array)
+ print(tuple(result))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-268/sgreen/python/test.py b/challenge-268/sgreen/python/test.py
new file mode 100755
index 0000000000..681f2f8d21
--- /dev/null
+++ b/challenge-268/sgreen/python/test.py
@@ -0,0 +1,24 @@
+#!/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.magic_number([3, 7, 5], [9, 5, 7]), 2)
+ self.assertEqual(ch_1.magic_number([1, 2, 1], [5, 4, 4]), 3)
+ self.assertEqual(ch_1.magic_number([2], [5]), 3)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.numbers_game([2, 5, 3, 4]), [3, 2, 5, 4])
+ self.assertEqual(
+ ch_2.numbers_game([9, 4, 1, 3, 6, 4, 6, 1]),
+ [1, 1, 4, 3, 6, 4, 9, 6]
+ )
+ self.assertEqual(ch_2.numbers_game([1, 2, 2, 3]), [2, 1, 3, 2])
+
+
+if __name__ == '__main__':
+ unittest.main()