From 59548f33170863d1b540daa9fed90707a6117a01 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 18 May 2025 22:09:15 +1000 Subject: sgreen solutions to challenge 321 --- challenge-321/sgreen/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 challenge-321/sgreen/python/ch-1.py (limited to 'challenge-321/sgreen/python/ch-1.py') 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() -- cgit