aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/sgreen/python/ch-1.py
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/ch-1.py
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/ch-1.py')
-rwxr-xr-xchallenge-321/sgreen/python/ch-1.py32
1 files changed, 32 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()