aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/spadacciniweb/python/ch-2.py
blob: ebcb2aa60bcba226a3a0929b1c6f850a1f5c6ba6 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Task 2: Most Frequent Word
# Submitted by: Mohammad Sajid Anwar
# 
# You are given a paragraph $p and a banned word $w.
# Write a script to return the most frequent word that is not banned.
# 
# Example 1
# Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
#        $w = "hit"
# Output: "ball"
# 
# The banned word "hit" occurs 3 times.
# The other word "ball" occurs 2 times.
# 
# Example 2
# Input: $p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."
#        $w = "the"
# Output: "Perl"
# 
# The banned word "the" occurs 3 times.
# The other word "Perl" occurs 2 times.

def count(w, dictionary):
    if w in dictionary:
        dictionary[w] += 1
    else:
        dictionary.update({w: 1})

def banned_word(paragraph, banned):
    paragraph = filter(lambda x: x.isalnum() or x.isspace(), paragraph)
    paragraph = "".join(paragraph)

    dictionary = {}
    lst = paragraph.split()

    for w in lst:
        if w != banned:
            count(w, dictionary)

    print(max(dictionary, key=dictionary.get))

if __name__ == "__main__":
    paragraph = "Joe hit a ball, the hit ball flew far after it was hit."
    banned = "hit"
    banned_word(paragraph, banned)

    paragraph = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."
    banned = "the"
    banned_word(paragraph, banned)