From 5fded4c63588ee61d55e38c0698abc6bd8a20e4a Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sat, 23 Aug 2025 15:27:04 +1000 Subject: sgreen solutions to challenge 335 --- challenge-335/sgreen/python/ch-1.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 challenge-335/sgreen/python/ch-1.py (limited to 'challenge-335/sgreen/python/ch-1.py') diff --git a/challenge-335/sgreen/python/ch-1.py b/challenge-335/sgreen/python/ch-1.py new file mode 100755 index 0000000000..48a7ce776a --- /dev/null +++ b/challenge-335/sgreen/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import sys +from collections import Counter + + +def common_characters(word_list: list[str]) -> list[str]: + """Find common characters in all words. + + Args: + word_list (list[str]): List of words to compare. + + Returns: + list[str]: List of common characters sorted alphabetically. + """ + solution = [] + + # Turn the words into a frequency dict + freq_list = [Counter(word) for word in word_list] + + # Find the minimum frequency of each character across all words + for letter in sorted(freq_list[0]): + min_freq = min(freq[letter] for freq in freq_list) + if min_freq > 0: + # ... and add it to the solution + solution.extend(letter * min_freq) + + return solution + + +def main(): + result = common_characters(sys.argv[1:]) + print(result) + + +if __name__ == '__main__': + main() -- cgit