diff options
Diffstat (limited to 'challenge-211/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-211/sgreen/python/ch-2.py | 31 |
1 files changed, 31 insertions, 0 deletions
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) |
