From d4525399d41dd57f21bc11a67ceb63bdb16bc146 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 11 Feb 2024 17:05:04 +1100 Subject: Simon's solution to challenge 255 --- challenge-255/sgreen/python/ch-1.py | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 challenge-255/sgreen/python/ch-1.py (limited to 'challenge-255/sgreen/python/ch-1.py') 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() -- cgit