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,...

  • 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"} #...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • Python 3.x: Write a function typos(fname) that finds suspected typos in the file fname. To do...

    Python 3.x: Write a function typos(fname) that finds suspected typos in the file fname. To do that we need a dictionary, for which we use the file words.txt which is a reasonably complete list of English words (though you may disagree after running it on some sample texts). Any word in fname that is not in the dictionary should be printed, and we should be told how often it occurs in fname. Below I ran a Mark Train novel to...

  • python Strings Question lyrics = """ You think you've got it, oh, you think you've got...

    python Strings Question lyrics = """ You think you've got it, oh, you think you've got it But “got it” just don't get it 'til there's nothing at all We get together, oh, we get together But separate's always better when there's feelings involved If what they say is “Nothing is forever” Then what makes, then what makes, then what makes Then what makes, what makes, what makes love the exception? So why oh, why oh, why oh, why oh,...

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
ADVERTISEMENT