-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-top_3_words.py
29 lines (24 loc) · 1.27 KB
/
4-top_3_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# https://www.codewars.com/kata/51e056fe544cf36c410000fb/train/python
from collections import Counter
from re import split, sub
def top_3_words(text):
words = map(lambda e: sub("'+", "'", e), split("[^a-z']", text.lower()))
counts = Counter(filter(lambda e: e not in ['', "'"], words))
return [e[0] for e in counts.most_common(3)]
print(top_3_words(
"a a a b c c d d d d e e e e e"), ["e", "d", "a"])
print(top_3_words(
"e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e"), ["e", "ddd", "aa"])
print(top_3_words(
"e' e' e' e' DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e' e' e'"), ["e'", "ddd", "aa"])
print(top_3_words(" //wont won't won't "), ["won't", "wont"])
print(top_3_words(" , e .. "), ["e"])
print(top_3_words(" ... "), [])
print(top_3_words(" ' "), [])
print(top_3_words(" ''' "), [])
print(top_3_words("""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"])