aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/sgreen/python/ch-1.py
diff options
context:
space:
mode:
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()