Question

IN PYTHON 3: Given a sentence, s, produce a dictionary that tells you the number of...

IN PYTHON 3: Given a sentence, s, produce a dictionary that tells you the number of times each word occurs in a string. You may assume each word is lowercase, there are no contractions, and each word is separated by a space. Please note, your function must return the created dictionary.

please test that it works with this test :

wordFrequency("was it a car or cat i saw or was it a kangaroo")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def wordFrequency(s):#definition of function

    #empty dictionary

    dict1={}

    list1=list(s.split(" "))#split the s into spaces and append it to the list1

    i=0#initialize i=0

    while(i<=len(list1)-1):#while loop runs from 0 to len(list1)-1

        count=1#initialize count=1 and ele=first element of list and k=i+1

        ele=list1[i]

        k=i+1

        for j in range(k,len(list1)): #loop runs from k to len(list1)

            if ele in dict1:#if ele in dict1 then breaks the loop

                break

            else:

                if(ele==list1[j]):#else calculate it to check how many times it occured

                    count=count+1# inc count

        if ele not in dict1:#if ele not in dict1

            dict1[ele]=count#appends to ele to the dict1

        i=i+1#Increment i

        if(i==len(list1)):#if i=len(list1)

            print(dict1)#print the dictionary

Add a comment
Know the answer?
Add Answer to:
IN PYTHON 3: Given a sentence, s, produce a dictionary that tells you the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Code Exercise #3 name: count_lead_letter arguments: * s (str): s will all lowercase letter with...

    Python Code Exercise #3 name: count_lead_letter arguments: * s (str): s will all lowercase letter with no puntuation returns: A dictionary keyed by a letter and it's value is a count of the number of times that letter is the first character in a word in the string s note: Each word is seperated by a single space

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for...

    %%%%Python Question%%% Work from the template acrostic.py, which you can find on the ELMS page for this assignment. • In the Generator class, write an __init__() method with two parameters: self and the path to a text file containing one word per line. This method should read the words from the file, strip off leading and trailing whitespace, and store them in a dictionary where each key is a lower-case letter and each corresponding value is a list of words...

  • In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string dat...

    In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string data type Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior. Use character data type for c++. Then you can say something like: char level ='f'; level=toupper(level); You would need a loop...

  • python please 11 def add_item(items, word): 14 15 16 F # check if the word is...

    python please 11 def add_item(items, word): 14 15 16 F # check if the word is in the dictionary (keys of dictionary) if word in items: items [word] = items (word] + 1 # update the count else: # word is not in dictionary items[word] = 1 # add the word with count 1 return items [word] # return the current count of word after updation Create a function named build_dictionary that takes a list of words (as a parameter)...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Create a new program in Mu and save it as ps4.5.2.py and take the code below...

    Create a new program in Mu and save it as ps4.5.2.py and take the code below and fix it as indicated in the comments: # Do not change the line of code below. It's at the top of # the file to ensure that it runs before any of your code. # You will be able to access french_dict from inside your # function. french_dict = {"me": "moi", "hello": "bonjour", "goodbye": "au revoir", "cat": "chat", "dog": "chien", "and": "et"} #...

  • In python and without using Libraries (comment the code and please don't use lambda code): Write...

    In python and without using Libraries (comment the code and please don't use lambda code): Write a function called mostfrequent that takes one string argument containing a list of words separated by commas (e.g., “apple,banana,apple,pear”, note there are no spaces). The function must return the word that occurs most frequently in the input string. You can assume there will be no ties in frequency. Examples: mostfrequent(“apple,apple,banana,apple,peach,banana”)  “apple” mostfrequent(“apple,banana,banana,apple,peach,banana”)  “banana” mostfrequent(“apple”)  “apple”

  • In python write a module named randStr.py that has 3 functions. Each function has an optional...

    In python write a module named randStr.py that has 3 functions. Each function has an optional second parameter to set a particular seed. 1) randWord accepts a string and will return a random word from that string (return value is string). 2) strMixer will randomly change words inside the string and returns a string -- If the string is only one word, then it will mix the letters of the word and return the mixed word as a string 3)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT