From 179e4b69b5060f752454bf14008c4a859345b0fe Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sat, 8 Apr 2023 20:09:13 +1000 Subject: Simon's solution to challenge 211 --- challenge-211/sgreen/python/ch-2.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 challenge-211/sgreen/python/ch-2.py (limited to 'challenge-211/sgreen/python/ch-2.py') diff --git a/challenge-211/sgreen/python/ch-2.py b/challenge-211/sgreen/python/ch-2.py new file mode 100755 index 0000000000..57226ad4bf --- /dev/null +++ b/challenge-211/sgreen/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +from itertools import combinations +import sys + + +def main(n): + # Calculate average + avg = sum(n) / len(n) + + # A single element is always true + if len(n) == 1: + print('true') + return + + # Consider combination of size 1 to half the length + for i in range(1, len(n)//2+1): + # Work through each combination + for c in combinations(n, i): + # If combination average is same, the remaining items also will be + if sum(c) / i == avg: + print('true') + return + + print('false') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) -- cgit