diff options
| author | Simon Green <mail@simon.green> | 2024-02-11 17:05:04 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-02-11 17:05:04 +1100 |
| commit | d4525399d41dd57f21bc11a67ceb63bdb16bc146 (patch) | |
| tree | 6ef501f14ad4ccb10ac9d95d9f9d7f2a0809e679 /challenge-255/sgreen/python/ch-1.py | |
| parent | 34fa48cf4b02c81fd6ed544498bde068aca981a2 (diff) | |
| download | perlweeklychallenge-club-d4525399d41dd57f21bc11a67ceb63bdb16bc146.tar.gz perlweeklychallenge-club-d4525399d41dd57f21bc11a67ceb63bdb16bc146.tar.bz2 perlweeklychallenge-club-d4525399d41dd57f21bc11a67ceb63bdb16bc146.zip | |
Simon's solution to challenge 255
Diffstat (limited to 'challenge-255/sgreen/python/ch-1.py')
| -rwxr-xr-x | challenge-255/sgreen/python/ch-1.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-255/sgreen/python/ch-1.py b/challenge-255/sgreen/python/ch-1.py new file mode 100755 index 0000000000..dc81356e84 --- /dev/null +++ b/challenge-255/sgreen/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import sys + + +def frequency_count(s: str) -> dict: + """Counts the frequency of each letter + + Args: + s (str): The string to use + + Returns: + dict: Key is the letter, values is the number of occurrences. + """ + freq = {} + + for c in s: + freq[c] = freq.get(c, 0) + 1 + + return freq + + +def odd_character(s: str, t: str) -> str: + """Finds extra character in second string + + Args: + s (str): The first string + t(str): The second string + + Returns: + str: The extra character + """ + first_freq = frequency_count(s) + second_freq = frequency_count(t) + + for c in second_freq: + if second_freq[c] > first_freq.get(c, 0): + # We have found the extra character + return c + + return 'No extra characters!' + + +def main(): + result = odd_character(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() |
