class logo

CS 122 - Day 11 Exercises

This page lists some problems for today's class. After today, you should be able to solve these problems on your own. Problems with this color are more challenging.

1) Word Frequency

Write a program that asks the user to enter a list of words and counts the number of times each word is entered. Structure it like the guessing program where there is a loop that prompts the user to enter another word or 'q' to quit. solution

2) Phone List

Download the file phoneList.txt. Write a program that reads this file in and creates a dictionary mapping names to phone numbers. Then ask the user which name they want to look up and use the dictionary to look up the phone number for that name and print it out. Note that you will need to use the split() function on each line of the file to separate the name and phone number. solution

3) Functions with Return Values

Using the graphics package, create a window and draw some rectangles on it. Write a function named rectangleContainsPoint that accepts a Rectangle and a Point as parameters and returns True if the rectangle contains the point and false otherwise. Now add an infinite while loop getting mouse clicks from the user. Each time a mouse click is received, call the rectangleContainsPoint function for every rectangle drawn on the screen. If a rectangle contains the point, set its fill color to "Red" otherwise set its fill color to "White". solution

3.a) Quit button

Add a special rectangle colored Yellow. When the user clicks in this rectangle use a break statement to end the loop and quit the program.