diff options
Diffstat (limited to 'challenge-199/sgreen/python')
| -rwxr-xr-x | challenge-199/sgreen/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-199/sgreen/python/ch-2.py | 30 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-199/sgreen/python/ch-1.py b/challenge-199/sgreen/python/ch-1.py new file mode 100755 index 0000000000..aebf116a45 --- /dev/null +++ b/challenge-199/sgreen/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # Calculate the frequency of each 'integer' + freq = {} + for i in n: + freq[i] = freq.get(i, 0)+1 + + solution = 0 + for f in freq.values(): + # If a value appears more than once, calculate the number of + # combinations. This is the sum of 1 + ... + f-1. + if f > 1: + solution += f * (f-1)//2 + + # Display the output + print(solution) + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/challenge-199/sgreen/python/ch-2.py b/challenge-199/sgreen/python/ch-2.py new file mode 100755 index 0000000000..9536ef0e8b --- /dev/null +++ b/challenge-199/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys +from itertools import combinations + + +def main(array): + # Get the x, y, and z values from the input + *array, x, y, z = array + + # The solution is the number of good triplets. + count = 0 + + # Work through all combinations of positions + for c in combinations(range(len(array)), 3): + i, j, k = sorted(c) + # If we match the criteria, add one to the count + if abs(array[i] - array[j]) <= x and \ + abs(array[j] - array[k]) <= y and \ + abs(array[i] - array[k]) <= z: + count += 1 + + # Display the output + print(count) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) |
