aboutsummaryrefslogtreecommitdiff
path: root/challenge-335/sgreen/python/ch-1.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-08-23 15:27:04 +1000
committerSimon Green <mail@simon.green>2025-08-23 15:27:04 +1000
commit5fded4c63588ee61d55e38c0698abc6bd8a20e4a (patch)
tree3ef28d4fb8ebc04cdcd56f18c8ee41b54de6bc58 /challenge-335/sgreen/python/ch-1.py
parent4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff)
downloadperlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.tar.gz
perlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.tar.bz2
perlweeklychallenge-club-5fded4c63588ee61d55e38c0698abc6bd8a20e4a.zip
sgreen solutions to challenge 335
Diffstat (limited to 'challenge-335/sgreen/python/ch-1.py')
-rwxr-xr-xchallenge-335/sgreen/python/ch-1.py37
1 files changed, 37 insertions, 0 deletions
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()