Skip to content

Commit 651003a

Browse files
author
Joseph Luce
authored
Create Freq_non-banned_words.md
1 parent 4248a7a commit 651003a

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# QUESTION
2+
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
3+
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
4+
```
5+
Example:
6+
Input:
7+
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
8+
banned = ["hit"]
9+
Output: "ball"
10+
Explanation:
11+
"hit" occurs 3 times, but it is a banned word.
12+
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
13+
Note that words in the paragraph are not case sensitive,
14+
that punctuation is ignored (even if adjacent to words, such as "ball,"),
15+
and that "hit" isn't the answer even though it occurs more because it is banned.
16+
```
17+
18+
# SOLUTION
19+
```
20+
def get_most_freq_word(paragraph, banned_words):
21+
if paragraph is None:
22+
return ''
23+
if banned_words is None:
24+
banned_words = list()
25+
tokens = paragraph.split()
26+
tokens = sanitize(tokens)
27+
banned_words_set = set(banned_words)
28+
word_to_occur_pos_hash = create_occur_pos_hash(tokens, banned_words_set)
29+
return find_most_freq_word_in(word_to_occur_pos_hash)
30+
31+
def sanitize(tokens):
32+
result = list()
33+
for token in tokens:
34+
letters = token.split()
35+
full_word = list()
36+
for letter in token:
37+
letter = letter.lower()
38+
if letter.isalpha():
39+
full_word.append(letter)
40+
if len(full_word) > 0:
41+
full_word = ''.join(full_word)
42+
result.append(full_word)
43+
return result
44+
45+
def create_occur_pos_hash(tokens, banned_words_set):
46+
result = dict()
47+
for pos, word in enumerate(tokens):
48+
if word not in banned_words_set:
49+
if word not in result:
50+
result[word] = (0,0)
51+
occurance = result[word][0]
52+
result[word] = (occurance+1, pos)
53+
return result
54+
55+
def find_most_freq_word_in(wtop_hash):
56+
result, global_occurance, global_position = '', -1, -1
57+
for word, value in wtop_hash.items():
58+
local_occurance = value[0]
59+
local_position = value[1]
60+
if local_occurance >= global_occurance:
61+
if local_position > global_position:
62+
result = word
63+
global_occurance = local_occurance
64+
global_position = local_position
65+
return result
66+
67+
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
68+
banned = ["hit"]
69+
assert(get_most_freq_word(paragraph, banned) == 'ball')
70+
71+
paragraph = " a1 b# 0 $ a b c ( "
72+
banned = ["b"]
73+
assert(get_most_freq_word(paragraph, banned) == 'a')
74+
75+
paragraph = " a1 b# 0 $ a b c ( "
76+
banned = [""]
77+
assert(get_most_freq_word(paragraph, banned) == 'b')
78+
79+
paragraph = ""
80+
banned = ["b"]
81+
assert(get_most_freq_word(paragraph, banned) == '')
82+
83+
paragraph = None
84+
banned = ["b"]
85+
assert(get_most_freq_word(paragraph, banned) == '')
86+
87+
paragraph = "a b a b"
88+
banned = None
89+
assert(get_most_freq_word(paragraph, banned) == 'b')
90+
91+
```

0 commit comments

Comments
 (0)