aboutsummaryrefslogtreecommitdiff
path: root/challenge-216/sgreen/python/ch-1.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2023-05-14 17:53:28 +1000
committerSimon Green <mail@simon.green>2023-05-14 17:53:28 +1000
commitfd41118bac9ea3d412a4490a9676391122a2cc6f (patch)
tree5f8d3c5f19df0ea745064e3fcc1643e676e5bb42 /challenge-216/sgreen/python/ch-1.py
parent126e144efbf6665a4c51f6f7492935d1f1f6a080 (diff)
downloadperlweeklychallenge-club-fd41118bac9ea3d412a4490a9676391122a2cc6f.tar.gz
perlweeklychallenge-club-fd41118bac9ea3d412a4490a9676391122a2cc6f.tar.bz2
perlweeklychallenge-club-fd41118bac9ea3d412a4490a9676391122a2cc6f.zip
Simon's solution to challenge 216
Diffstat (limited to 'challenge-216/sgreen/python/ch-1.py')
-rwxr-xr-xchallenge-216/sgreen/python/ch-1.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-216/sgreen/python/ch-1.py b/challenge-216/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..649ebed4be
--- /dev/null
+++ b/challenge-216/sgreen/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def get_frequency(word):
+ '''Return a dict with the frequency of each letter'''
+ freq = {}
+ for letter in word.upper():
+ if 'A' <= letter <= 'Z':
+ freq[letter] = freq.get(letter, 0) + 1
+
+ return freq
+
+
+def main(words):
+ # The last value is the plate we are matching
+ plate = words.pop()
+ plate_freq = get_frequency(plate)
+ solution = []
+
+ for word in words:
+ # Calculate the frequency of letters in the current word
+ word_freq = get_frequency(word)
+ for letter, cnt in plate_freq.items():
+ # Check that letters in the plate appear in the word
+ if letter not in word_freq or cnt > word_freq[letter]:
+ break
+ else:
+ solution.append(word)
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])