Skip to content

Commit

Permalink
feat(word-rank): implement word counting and ranking of them
Browse files Browse the repository at this point in the history
  • Loading branch information
HuntClauss committed Nov 9, 2023
1 parent 39b83fa commit f2c534b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion python/word-rank/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# input: array with multiple strings
# expected output: rank of the 3 most often repeated words in given set of strings and number of times they occured, case insensitive

sentences = [
sentences: list[str] = [
'Taki mamy klimat',
'Wszędzie dobrze ale w domu najlepiej',
'Wyskoczył jak Filip z konopii',
Expand All @@ -22,6 +22,21 @@
'Nie powinno sprawić żadnego problemu, bo Google jest dozwolony',
]

word_counter: dict[str, int] = {}

words: list[str] = [v.rstrip('?') for v in (' '.join(sentences)).lower().split()]

for word in words:
word_counter.setdefault(word, 0)
word_counter[word] += 1

ranking: list[tuple[str, int]] = sorted(word_counter.items(), key=lambda v: v[1], reverse=True)
TOP_RANKING_COUNT = 3

for idx, v in enumerate(ranking[:TOP_RANKING_COUNT]):
word, encounters = v
print(f'{idx+1}. "{word}" - {encounters}')

# Example result:
# 1. "mam" - 12
# 2. "tak" - 5
Expand Down

0 comments on commit f2c534b

Please sign in to comment.