August 2021
Beginner to intermediate
118 pages
1h 29m
English
| Puzzle 19 | TF (Without IDF) |
| 1: | import re |
| - | from collections import defaultdict |
| - | |
| - | |
| 5: | def word_freq(text, freqs=defaultdict(int)): |
| - | """Calculate word frequency in text. freqs are previous frequencies""" |
| - | for word in [w.lower() for w in re.findall(r'\w+', text)]: |
| - | freqs[word] += 1 |
| - | return freqs |
| 10: | |
| - | |
| - | freqs1 = word_freq('Duck season. Duck!') |
| - | freqs2 = word_freq('Rabbit season. Rabbit!') |
| - | print(freqs1) |
| 15: | print(freqs2) |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
This code will print:
| | defaultdict(<class 'int'>, {'duck': 2, 'season': 2, 'rabbit': ... |
Read now
Unlock full access