diff options
Diffstat (limited to 'challenge-276/packy-anderson/python')
| -rwxr-xr-x | challenge-276/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/python/ch-2.py | 44 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-276/packy-anderson/python/ch-1.py b/challenge-276/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..54b4b6db1b --- /dev/null +++ b/challenge-276/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def completeDays(hours): + pairs = [] + for i in range(len(hours) - 1): + for j in range(i+1, len(hours)): + if ( hours[i] + hours[j] ) % 24 == 0: + pairs.append([ hours[i], hours[j] ]) + + explain = "" + i = 1 + for p in pairs: + explain += f"\nPair {i}: ({comma_join(p)})" + i += 1 + + return len(pairs), explain + +def solution(hours): + print(f'Input: @hours = ({comma_join(hours)})') + count, explain = completeDays(hours) + print(f'Output: {count}\n{explain}') + +print('Example 1:') +solution([12, 12, 30, 24, 24]) + +print('\nExample 2:') +solution([72, 48, 24, 5]) + +print('\nExample 3:') +solution([12, 18, 24]) diff --git a/challenge-276/packy-anderson/python/ch-2.py b/challenge-276/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..9428fa0118 --- /dev/null +++ b/challenge-276/packy-anderson/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +from collections import Counter + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def conjunction(ints): + if len(ints) < 2: + return(ints) + elif len(ints) == 2: + return(f'{ints[0]} and {ints[1]}') + else: + last = ints.pop(-1) + l = comma_join(ints) + return(f'{l}, and {last}') + +def maxFrequency(ints): + freq = Counter(ints) + maxFreq = max(freq.values()) + atMax = [] + for i in sorted(freq.keys()): + if freq[i] == maxFreq: + atMax.append(i) + numList = conjunction(atMax) + elements = "elements" if len(atMax) > 1 else "element" + have = "have" if len(atMax) > 1 else "has" + explain = ( + f"The maximum frequency is {maxFreq}.\n" + + f"The {elements} {numList} {have} " + + f"the maximum frequency." + ) + return (maxFreq * len(atMax), explain) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + count, explain = maxFrequency(ints) + print(f'Output: {count}\n\n{explain}') + +print('Example 1:') +solution([1, 2, 2, 4, 1, 5]) + +print('\nExample 2:') +solution([1, 2, 3, 4, 5]) |
