aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/sgreen/python
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-05-18 22:09:15 +1000
committerSimon Green <mail@simon.green>2025-05-18 22:09:15 +1000
commit59548f33170863d1b540daa9fed90707a6117a01 (patch)
tree4e98e4d84b51a587277befd74e5cc8caae93dd6d /challenge-321/sgreen/python
parent62f1ccaddfc5a65501df9cfdf528d28927fef410 (diff)
downloadperlweeklychallenge-club-59548f33170863d1b540daa9fed90707a6117a01.tar.gz
perlweeklychallenge-club-59548f33170863d1b540daa9fed90707a6117a01.tar.bz2
perlweeklychallenge-club-59548f33170863d1b540daa9fed90707a6117a01.zip
sgreen solutions to challenge 321
Diffstat (limited to 'challenge-321/sgreen/python')
-rwxr-xr-xchallenge-321/sgreen/python/ch-1.py32
-rwxr-xr-xchallenge-321/sgreen/python/ch-2.py44
-rwxr-xr-xchallenge-321/sgreen/python/test.py21
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-321/sgreen/python/ch-1.py b/challenge-321/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..3851260f4e
--- /dev/null
+++ b/challenge-321/sgreen/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+from decimal import Decimal
+import sys
+
+
+def distinct_average(numbers: list) -> int:
+ unique_averages = set()
+
+ # Make sure the list is even
+ if len(numbers) % 2 == 1:
+ raise ValueError("You must provide an even number of numbers")
+
+ # Sort the list, and convert to Decimal type.
+ numbers = sorted(Decimal(i) for i in numbers)
+
+ for idx in range(len(numbers)//2):
+ # Calculate the unique sums for matching pairs
+ unique_averages.add((numbers[idx] + numbers[-1-idx])/2)
+
+ return len(unique_averages)
+
+
+def main():
+ # Convert input into integers
+ array = [Decimal(n) for n in sys.argv[1:]]
+ result = distinct_average(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-321/sgreen/python/ch-2.py b/challenge-321/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..81e0d9ed98
--- /dev/null
+++ b/challenge-321/sgreen/python/ch-2.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+
+import sys
+
+def covert_string(s: str) -> str:
+ """Convert a string by treating '#' as a backspace character
+
+ Args:
+ s (str): The original string
+
+ Returns:
+ str: The converted string
+ """
+
+ new_string = ''
+ for c in s:
+ if c == '#':
+ # This is safe even if new_string is empty
+ new_string = new_string[:-1]
+ else:
+ new_string += c
+
+ return new_string
+
+def backspace_compare(str1: str, str2: str) -> bool:
+ """Compare two strings are the same if '#' is a back space character
+
+ Args:
+ str1 (str): The first string
+ str2 (str): The second string
+
+ Returns:
+ bool: Whether the resulting strings are the same
+ """
+ return covert_string(str1) == covert_string(str2)
+
+
+def main():
+ result = backspace_compare(sys.argv[1], sys.argv[2])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-321/sgreen/python/test.py b/challenge-321/sgreen/python/test.py
new file mode 100755
index 0000000000..2c353d53a2
--- /dev/null
+++ b/challenge-321/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/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.distinct_average([1, 2, 4, 3, 5, 6]), 1)
+ self.assertEqual(ch_1.distinct_average([0, 2, 4, 8, 3, 5]), 2)
+ self.assertEqual(ch_1.distinct_average([7, 3, 1, 0, 5, 9]), 2)
+
+ def test_ch_2(self):
+ self.assertTrue(ch_2.backspace_compare("ab#c", "ad#c"))
+ self.assertTrue(ch_2.backspace_compare("ab##", "a#b#"))
+ self.assertFalse(ch_2.backspace_compare("a#b", "c"))
+
+
+if __name__ == "__main__":
+ unittest.main()