Dict Practice 2


Submit solution

Points: 3
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C, C++, Python3

This is the more advanced version of csci120p001.

Mr Cleese wants to know how many times a word appears in a sentence. Your task is to help him with it. In addition, the words may not be separated from the punctuation marks. Here's a list of punctuation marks you might encounter: ", ', ., ,, :, ;, !, ?

These symbols can occur anywhere in the text.

(Hint: use str.replace(key, " ") to get replace these with whitespace in the text)

In addition, because the sentences might be written by silly people, expect irregular casings such as bellow:

WhAt Is Up WiTh ThAt WeIrD, NeW SpOnGeBoB MeMe?

(Hint: use str.lower() to get everything in lowercase.

Input Specification

The first line will contain a single English sentence. All words will be lower case and separated by a single space.

The second line will contain a single lowercased word.

Output Specification

The output should contain only integer, the number of times the inquired word has appeared in the sentence.

Sample Input 1

    Canners, cAn Can? "whAt ThEy CAN caN".
    can

Sample Output 1

    4

Comments


  • 0
    Mais  commented on March 23, 2021, 8:32 a.m.

    Enter the sentence

    input_str = input()

    convert in lower case

    input_str = input_str.lower()

    input_str = input_str.replace("."," ") input_str = input_str.replace(","," ") input_str = input_str.replace("!"," ") input_str = input_str.replace(":"," ") input_str = input_str.replace(";"," ") input_str = input_str.replace("?"," ") input_str = input_str.replace("'"," ")

    print(input_str)

    Enter the word you want to count

    input_word = input()

    convert the sentence in list of words

    items = input_str.split()

    initialize the count variable

    count = 0 for i in range(len(items)): if input_word == items[i]: count+=1

    print the count

    print(count)


  • 0
    Mais  commented on March 21, 2021, 8:50 p.m.

    input_str = input("Enter the sentence")

    convert in lower case

    input_str = input_str.lower()

    input_str = input_str.replace("."," ") input_str = input_str.replace(","," ") input_str = input_str.replace("!"," ") input_str = input_str.replace(":"," ") input_str = input_str.replace(";"," ") input_str = input_str.replace("?"," ") input_str = input_str.replace("'"," ")

    print(input_str)

    input_word = input("Enter the word you want to count")

    convert the sentence in list of words

    items = input_str.split()

    initialize the count variable

    count = 0 for i in range(len(items)): if input_word == items[i]: count+=1

    print the count

    print(count)


  • 0
    Mais  commented on March 21, 2021, 8:49 p.m.

    input_str = input("Enter the sentence")

    convert in lower case

    input_str = input_str.lower()

    input_str = input_str.replace("."," ") input_str = input_str.replace(","," ") input_str = input_str.replace("!"," ") input_str = input_str.replace(":"," ") input_str = input_str.replace(";"," ") input_str = input_str.replace("?"," ") input_str = input_str.replace("'"," ")

    print(input_str)

    input_word = input("Enter the word you want to count")

    convert the sentence in list of words

    items = input_str.split()

    initialize the count variable

    count = 0 for i in range(len(items)): if input_word == items[i]: count+=1

    print the count

    print(count)


  • 0
    Mais  commented on March 21, 2021, 8:48 p.m.

    input_str = input("Enter the sentence") input_word = input("Enter the word you want to count")

    convert the sentence in list of words

    items = input_str.split()

    initialize the count variable

    count = 0 for i in range(len(items)): if input_word == items[i]: count+=1

    print the count

    print(count)