Description
Problem A. (5 points) Cookie Ordering
You are asked to help write a program that will help users purchase cookies. A user will input types of cookies and the number of cookies that they want, and your program will suggest how many boxes that they should order. There are 30 cookies per box, so your goal is to make sure that the user will buy enough boxes so that they will get the cookies that they want.
Write a function cookie_order that takes a dictionary as a parameter containing cookie names as keys and numbers of those cookies as values. Have this function return a new dictionary that has cookie names and the number of boxes that should be purchased of each type.
Hint:
- This means if someone wants 31 cookies, they need to buy 2 boxes of cookies of that type.
- You may want to consider using math.ceil for this problem.
Examples:
>>> cookie_order({‘thin mints’: 97})
{‘thin mints’: 4}
>>> cookie_order({‘thin mints’:34, ‘shortbread’: 60, ‘caramel delites’: 71, ‘peanut butter sandwich’: 0})
{‘thin mints’: 2, ‘shortbread’: 2, ‘caramel delites’: 3, ‘peanut butter sandwich’: 0}
>>> cookie_order({})
{}
Problem B. (10 points) Following Word
In the next few problems, we’re going to be using knowledge about what words most commonly follow other words within a block of text in order to automatically generate sentences.
Suppose we have a list of words that occur within some text, in the order they occur. Write a function follow_words(text) that takes in a string, and returns a dictionary.
In particular, each key in the output dictionary will be a word from string text, and each value will be a list of all of the words that directly follow that word.
For example, consider the word ‘fish’ in the following example:
>>> follow_words(‘one fish two fish red fish blue fish blue’)
{‘one’: [‘fish’], ‘fish’: [‘two’, ‘red’, ‘blue’, ‘blue’], ‘two’: [‘fish’], ‘red’: [‘fish’], ‘blue’: [‘fish’]}
The word ‘fish’ appears four times in the string. The first time it appears the word immediately after it is ‘two’, the second time the word immediately after it is ‘red’, and the other two times it’s followed by ‘blue’. So the value associated with the key ‘fish’ would be [‘two’, ‘red’, ‘blue’, ‘blue’].
See the examples below if you’re still unsure of the format of the output dictionary.
Note that the last word in text will not necessarily appear in the output dictionary (unless it occurs elsewhere in the list as well), since there is no word that follows it.
Hints:
- The .split() method should be used to separate the words in the string text
- You need to get each word and the word that follows it. This will require knowing the index of that word, so you should loop through the indexes, not the elements of your word list.
- For each pair of consecutive words, you need to handle both of these possibilities:
- The first word is already present as a key in the dictionary: if this is the case, then append the second word to the list that is the value associated with that key.
- The first word is not a key in the dictionary, since this is the first time you’re seeing it: if this is the case, then it must be added as a key in the dictionary, with the associated value being a list containing the second word.
Examples (Don’t forget that ordering is not guaranteed in a dictionary. )
>>> follow_words(‘what is that there’)
{‘what’: [‘is’], ‘is’: [‘that’], ‘that’: [‘there’]}
Problem D. (10 points) Random Sentence Generation
Create a function random_sent(fname, max_length) that takes two parameters: a string containing a file name, and an int containing a sentence length.
This function should read in that file and then use functions created earlier in this assignment to create a random sentence of the given length (in terms of number of words), whose data is drawn from the given text file. To do this, you need to do the following:
- Read in the data from the file into one long string
- Pass that string to your Problem B function to get a dictionary of what words can follow each word
- Pick a random starting word from among words in the file (You can use random.choice to accomplish this.) Call this your “current” word.
- Call your Problem C function with your current word to get a list of potential options for the next word. Pick one at random using random.choice and add it to your sentence. Your newly generated next word is now your “current” word.
- Repeat this step until you have generated a sentence with the given length, or your generated “next” word ends with a period, exclamation point, or question mark.
- This means that your sentence should end if a word is chosen that ends with a period, exclamation point, or question mark, even if you haven’t reached the required sentence length yet.
You can assume for this problem that the file exists: no need for a try-except block.
Your inclination may be to remove the punctuation from the text, but that isn’t necessary. In fact, the random sentences work better if punctuation is left in. Similarly, your inclination might be to convert the text to lowercase, but that is counterproductive as well. So, use the text exactly as it is read from the file.
For sample texts to load, I recommend checking out Project Gutenberg, where you can download public-domain works as text files. (E.g. Hamlet, Life on the Mississippi, etc.). We’ve also included some sample texts in hw09files.zip, including three relatively short ones to initially test your function on before you try one of the full texts.
You shouldn’t expect this technique to produce convincing sentences – it’s a little too simple and too random for that, but the nonsense it does produce should at least somewhat resemble English.
Examples (random, so won’t necessarily match, but make sure that each word is one that is allowed to follow the previous one):
>>> random_sent(‘short3.txt’, 10)
‘for second-guessing Too late to try defying gravity I guess’
>>> random_sent(‘hamlet.txt’, 20)
“pleasure of fear and dungeons, Denmark to those effects for look like a puff’d and we are the bloat king”