diff options
| author | Simon Green <mail@simon.green> | 2022-12-11 15:18:07 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2022-12-11 15:18:07 +1100 |
| commit | cd0c1c2f37f184497d8dc4990d2ab50bf04a4052 (patch) | |
| tree | 73b836480e9378cd12e49ae7e5e5bf4b3da7f971 /challenge-194/sgreen/python/ch-2.py | |
| parent | 8d4ad39acceae6916068d7661648e075877837cc (diff) | |
| download | perlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.tar.gz perlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.tar.bz2 perlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.zip | |
Simon's solution to challenge 194
Diffstat (limited to 'challenge-194/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-194/sgreen/python/ch-2.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-194/sgreen/python/ch-2.py b/challenge-194/sgreen/python/ch-2.py new file mode 100755 index 0000000000..dfa570c4cf --- /dev/null +++ b/challenge-194/sgreen/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import sys + + +def main(s): + # Calculate the frequency of each letter + letter_freq = {} + for letter in s: + letter_freq[letter] = letter_freq.get(letter, 0) + 1 + + # Calculate the frequency of frequencies :) + freq = {} + for i in letter_freq.values(): + freq[i] = freq.get(i, 0) + 1 + + solution = 0 + # A solution is only possible if there are two different frequencies ... + if len(freq) == 2: + min_freq = min(freq.keys()) + max_freq = max(freq.keys()) + + # ... and the minimum frequency only occurs once, + if min_freq == 1 and freq[1] == 1: + solution = 1 + + # ... or the difference between them is 1, and the higher frequency + # only occurs once. + elif min_freq == max_freq - 1 and freq[max_freq] == 1: + solution = 1 + elif len(freq) == 1 and 1 in freq: + # ... or the only thing we have is single letters + solution = 1 + + print(solution) + + +if __name__ == '__main__': + main(sys.argv[1]) |
