This lab was written and designed to be completed during lab-time for cs250. This lab is not graded. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.
This lab is designed to get you comfortable using dictionaries and file I/O.
Throughout the problems in this lab, we will be writing several python functions in the same file.
Create a new python file named lab7.py
and open it up in your editor of choice (atom).
Put all of the functions from all problems in this file as you go along.
Write a function named value_count
.
This function will take two arguments.
The first shall be named data
and is a dictionary.
The second is named name
and can be of any type.
This function will return the number of values in the data
dictionary that match name
.
For example:
>>> import lab7
>>> data = { 'Sherlock':'Cumberbatch', 'Superman':'Cavill', 'Dr Strange':'Cumberbatch', 'Batman':'Bale' }
>>> lab7.value_count(data, 'Cumberbatch')
2
>>> lab7.value_count(data, 'Cavill')
1
>>> lab7.value_count(data, 'Bro')
0
Write a python function named sort_all
.
This function will take a single argument named data
which is a dictionary.
This function will return a sorted list which contains all of the keys and all of the values from data
.
For example:
>>> import lab7
>>> lab7.sort_all({'a':'c', 'b':'z', 'f':'d'})
['a', 'b', 'c', 'd', 'f', 'z']
>>> lab7.sort_all({'OKC':'Westbrook', 'GSW':'Curry', 'PHX':'Bledsoe'})
['Bledsoe', 'Curry', 'GSW', 'OKC', 'PHX', 'Westbrook']
Write a python function named keys_matching_values
.
This function will take a single argument named data
which is a dictionary.
For each key in data
, this function will count how many values in data
are equal to the key.
For example:
>>> import lab7
>>>
>>> data = { 'Sherlock':'Cumberbatch', 'Superman':'Cavill', 'Dr Strange':'Cumberbatch', 'Batman':'Bale' }
>>> lab7.keys_matching_values(data)
Key Sherlock matches 0 values
Key Superman matches 0 values
Key Dr Strange matches 0 values
Key Batman matches 0 values
>>>
>>> data = { 5:10, 10:15, 15:20, 20:25, 25:30 }
>>> lab7.keys_matching_values(data)
Key 5 matches 0 values
Key 10 matches 1 values
Key 15 matches 1 values
Key 20 matches 1 values
Key 25 matches 1 values
>>>
>>> data = { 'breakfast':'?', 'lunch':'burrito', 'dinner':'sushi', 'dessert':'?', '?':'?' }
>>> lab7.keys_matching_values(data)
Key breakfast matches 0 values
Key lunch matches 0 values
Key dinner matches 0 values
Key dessert matches 0 values
Key ? matches 3 values
>>>
Write a function named get_top_items
.
This function will take as input a dictionary named sales
that represents the sales numbers for various products in a technology store.
The keys in the dictionary will be the product names, and the values will be the corresponding sales counts.
This function will print out the top-two selling products in-order.
For example:
>>> import lab7
>>>
>>> sales_info = { 'Macbook':135, 'Macbook Pro':75, 'Macbook Air':211, 'Dell XPS':301, 'Lenovo Yoga':90, 'HP Spectre':112 }
>>> lab7.get_top_items(sales_info)
Best selling item: Dell XPS (301 sold)
Second-best selling item: Macbook Air (211 sold)
>>>
>>> sales_info = {'galaxy s7':723, 'iphone 7':657, 'google pixel':300, 'Moto Z':590, 'iphone 7+':900}
>>> lab7.get_top_items(sales_info)
Best selling item: iphone 7+ (900 sold)
Second-best selling item: galaxy s7 (723 sold)
>>>
write a function named upper_file
.
This function will convert the contents of a text file to be all upper-case.
It shall take one argument named file_name
.
For example, say the file jokes.txt
has these contents:
A bear walks into a bar and says to the bartender, "I'll have a pint of beer and a.......... packet of peanuts."
The bartender asks, "Why the big pause?"
A grasshopper walks into a bar, and the bartender says, "Hey, we have a drink named after you!"
The grasshopper looks surprised and asks, "You have a drink named Steve?"
Q: Why did the chewing gum cross the road?
A: He was stuck to the chicken's foot.
After running:
>>> import lab7
>>> lab7.upper_file('jokes.txt')
jokes.txt
will look like:
A BEAR WALKS INTO A BAR AND SAYS TO THE BARTENDER, "I'LL HAVE A PINT OF BEER AND A.......... PACKET OF PEANUTS."
THE BARTENDER ASKS, "WHY THE BIG PAUSE?"
A GRASSHOPPER WALKS INTO A BAR, AND THE BARTENDER SAYS, "HEY, WE HAVE A DRINK NAMED AFTER YOU!"
THE GRASSHOPPER LOOKS SURPRISED AND ASKS, "YOU HAVE A DRINK NAMED STEVE?"
Q: WHY DID THE CHEWING GUM CROSS THE ROAD?
A: HE WAS STUCK TO THE CHICKEN'S FOOT.
Write a function named get_top_items_from_file
.
This problem is similar to problem 4, except it reads the item sales information from a file instead of from a dictionary.
This function will take a single argument named file_name
and the file contains the sales info in it.
The file will be formatted like:
Macbook : 135
Macbook Pro : 75
Macbook Air : 211
Dell XPS : 301
Lenovo Yoga : 90
HP Spectre : 112
The function only needs to report the top selling item.
Assuming that the above file is named sales_info.txt
, here is an example run:
>>> import lab7
>>> lab7.get_top_items_from_file('sales_info.txt')
Best selling item: Dell XPS (301 sold)