feat: kata returns top 3 most used words test: test cases for kata
This commit is contained in:
parent
1cab2e22fb
commit
20e345a9e9
@ -9,14 +9,19 @@ def top_3_words(text: str) -> list:
|
|||||||
Returns:
|
Returns:
|
||||||
list: [most used word, second most used word, third most used word]
|
list: [most used word, second most used word, third most used word]
|
||||||
"""
|
"""
|
||||||
|
# remove invalid characters and replace with whitespace
|
||||||
text = "".join(
|
text = "".join(
|
||||||
c if c.isalpha() or c.isdigit() or c in ("'", " ") else " " for c in text
|
c if c.isalpha() or c.isdigit() or c in ("'", " ") else " " for c in text
|
||||||
) # remove invalid characters and replace with whitespace
|
)
|
||||||
text = text.split()
|
text = text.split()
|
||||||
|
|
||||||
|
# remove word if it's just made of the character '
|
||||||
|
text = [w for w in text if w.strip("' ") != ""]
|
||||||
|
|
||||||
word_count = {}
|
word_count = {}
|
||||||
|
|
||||||
for word in text:
|
for word in text:
|
||||||
|
word = word.lower()
|
||||||
if word in word_count:
|
if word in word_count:
|
||||||
word_count[word] += 1
|
word_count[word] += 1
|
||||||
else:
|
else:
|
||||||
@ -30,9 +35,18 @@ def top_3_words(text: str) -> list:
|
|||||||
|
|
||||||
for word in word_count:
|
for word in word_count:
|
||||||
if word_count[word] > top_3["first"]["amount"]:
|
if word_count[word] > top_3["first"]["amount"]:
|
||||||
|
top_3["third"]["word"] = top_3["second"]["word"]
|
||||||
|
top_3["third"]["amount"] = top_3["second"]["amount"]
|
||||||
|
|
||||||
|
top_3["second"]["word"] = top_3["first"]["word"]
|
||||||
|
top_3["second"]["amount"] = top_3["first"]["amount"]
|
||||||
|
|
||||||
top_3["first"]["word"] = word
|
top_3["first"]["word"] = word
|
||||||
top_3["first"]["amount"] = word_count[word]
|
top_3["first"]["amount"] = word_count[word]
|
||||||
elif word_count[word] > top_3["second"]["amount"]:
|
elif word_count[word] > top_3["second"]["amount"]:
|
||||||
|
top_3["third"]["word"] = top_3["second"]["word"]
|
||||||
|
top_3["third"]["amount"] = top_3["second"]["amount"]
|
||||||
|
|
||||||
top_3["second"]["word"] = word
|
top_3["second"]["word"] = word
|
||||||
top_3["second"]["amount"] = word_count[word]
|
top_3["second"]["amount"] = word_count[word]
|
||||||
elif word_count[word] > top_3["third"]["amount"]:
|
elif word_count[word] > top_3["third"]["amount"]:
|
||||||
|
|||||||
@ -1,5 +1,27 @@
|
|||||||
from src.codewars.kata_most_words_used import top_3_words
|
from src.codewars.kata_most_words_used import top_3_words
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_top_3_words():
|
@pytest.mark.parametrize(
|
||||||
assert top_3_words() == 0
|
("input", "expected"),
|
||||||
|
[
|
||||||
|
("a a a b c c d d d d e e e e e", ["e", "d", "a"]),
|
||||||
|
("e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e", ["e", "ddd", "aa"]),
|
||||||
|
(" //wont won't won't ", ["won't", "wont"]),
|
||||||
|
(" , e .. ", ["e"]),
|
||||||
|
(" ... ", []),
|
||||||
|
(" ' ", []),
|
||||||
|
(" ''' ", []),
|
||||||
|
(
|
||||||
|
"""In a village of La Mancha, the name of which I have no desire to call to
|
||||||
|
mind, there lived not long since one of those gentlemen that keep a lance
|
||||||
|
in the lance-rack, an old buckler, a lean hack, and a greyhound for
|
||||||
|
coursing. An olla of rather more beef than mutton, a salad on most
|
||||||
|
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
|
||||||
|
on Sundays, made away with three-quarters of his income.""",
|
||||||
|
["a", "of", "on"],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_top_3_words(input, expected):
|
||||||
|
assert top_3_words(input) == expected
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user