diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-04 12:07:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-04 12:07:53 +0100 |
| commit | 030a22e6dfd65db3b969a769d2981be9fbc6db8e (patch) | |
| tree | 4308a363e31bc502ad7c1de08f912b5928312109 /challenge-276/packy-anderson/python/ch-2.py | |
| parent | 5643586f6b68265209c87dafb3ae700da82f5727 (diff) | |
| parent | 9246c36a181a4a62197859d7bf7367e2295a5c64 (diff) | |
| download | perlweeklychallenge-club-030a22e6dfd65db3b969a769d2981be9fbc6db8e.tar.gz perlweeklychallenge-club-030a22e6dfd65db3b969a769d2981be9fbc6db8e.tar.bz2 perlweeklychallenge-club-030a22e6dfd65db3b969a769d2981be9fbc6db8e.zip | |
Merge pull request #10366 from packy/master
Challenge 276 solutions by Packy Anderson
Diffstat (limited to 'challenge-276/packy-anderson/python/ch-2.py')
| -rwxr-xr-x | challenge-276/packy-anderson/python/ch-2.py | 44 |
1 files changed, 44 insertions, 0 deletions
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]) |
