Python Lab3
Python Lab3
Permitted Resources
You may use the following to complete this lab:
Lecture materials, examples, reference materials, websites, and any other materials directly linked to
from our course website
Your textbook
Utilities, such as Excel or a calculator for checking your results, or translation software (if English is
not your rst language)
You may not use any other resources when completing this lab. Specically, you may not use:
Q&A / homework help sites such as stackoverow.com, coursehero.com, chegg.com, and many others
If you have any doubt as to whether you should be using a particular resource, please ask.
Instructions
Work with your team to complete the following exercises and submit via GitHub. Be sure to name your les
using the convention ex1.py, ex2.py, etc. No le or folder names contain capital letters or spaces. If you
don't follow this convention, GitHub will not evaluate your exercise.
1
Review Your Solutions with Sta for Credit
For each exercise marked Review, after GitHub validates your solution, review the solution with the course
sta to earn credit for the exercise. To earn full points on each exercise, you must review the exercise with
the course sta prior to the due date posted in the course website. If you need more time to complete the
exercises, discuss your plan with the instructor before the published due date/time.
The course sta will look at the following for each exercise:
Output formatting. The instructions for each exercise provide sample output.
I provide additional exercises throughout the lab for practice. You may complete these exercises and submit
to GitHub for validation. We are happy to review your solution to these additional exercises, but there are
no points associated with completing and reviewing these exercises.
Exchange thoughts and ideas with any other students in the class, not just the students on your team
Do not:
Show / distribute your code or solutions to students outside of your team (without instructor permis-
sion)
In [2]: duplicate_names(list_of_names)
Out[2]: ['Alice', 'Jerry']
To pass our review and earn credit for this exercise, your solution should use the histogramming technique
from the Lab 3 Lecture.
ex2.py
The US Department of Agriculture (USDA) categorizes eggs by weight:
Eggs weighing less than 1.5oz do not fall into any category. Write a function named classify_eggs that
accepts a list containing the weight of each egg. Returns a dictionary of egg classications:
In [1]: egg_weights = [1.21, 1.82, 1.9, 1.31, 2.45, 2.2, 1.4, 2.74, 2.99, 2.38]
In [2]: classify_eggs(egg_weights)
Out[2]: {'Extra Large': 2, 'Jumbo': 2, 'Large': 1, 'Medium': 2}
ex3.py
appointed_by function from the previous lab by writing a new function named
(Review, 5 pts) Build on your
justices_appointed_by that accepts two arguments (a lename and a President's name). Returns a list of
all US Supreme Court justices appointed by that President. Like the previous version, your function should
support partial searches:
.5N − n1
M − .167 + × .333
n2
Where:
If n2 = 0, then IM = M . Write a function named interpolated_median that accepts a list of letter grades
and returns the interpolated median. Here is a list you can copy/paste for testing:
grades = [ 'A=' , 'B ' , 'C ' , 'A ' , 'C ' , 'A ' , 'B ' , 'C =' , 'C+ ' , 'B =' ,
'B =' , 'A = ' , 'B ' , 'B+ ' , ' C= ' , 'C+ ' , 'C ' , 'B=' , 'A ' , 'C ' ,
'C ' , ' B= ' , 'A ' , ' C= ' , 'C ' ]
...and here is a sample call to the function using the above list:
In [1]: interpolated_median(grades)
Out[1]: 2.627875
NOTE: In the US, letter grades generally run on a 4.0 scale with A = 4.0, A- = 3.67, B+ = 3.33, B = 3.0,
B- = 2.67, etc. There is no A+ or D- grade at the U of Minnesota, and a grade of F earns 0 grade points.
ex5.py
The le mileage.txt contains the results of a fuel economy study. The researchers drove each model of car
100 miles and recorded the fuel consumption (in gallons). Some models were tested more than once. Write a
function named mpg that accepts one argument (a lename) and returns a dictionary of fuel economy where
the key is the model name and the value is the miles per gallon (mpg) to the nearest tenth:
In [1]: mpg('mileage.txt')
Out[1]:
{'Accord': 23.4,
'Camry': 25.0,
'Mustang': 19.0,
'Prius': 45.5,
'Sebring': 23.8}
In [3]: translate('dictionary.csv', 'My sick friend often has NO hat on.', 'EN', 'DE')
Out[3]: 'Mein krank freund oft hat NEIN hut auf.'
Note:
The rst line of the le contains the ISO codes of each language, which are used as the from and to
arguments of the function. Your function will not be tested with any language codes that do not exist
in the le, so there is no need to spend time on this case.
The sample dictionary le contains 3 dierent languages (English, French, German) and 17 dierent
words. Your solution should not make any assumptions about the number of languages or the number
of words in the le. Modify the le, or make your own, to test dierent scenarios.
You may assume that the phrase or sentence contains no punctuation, or only contains a period (full
stop) at the end. Your function should preserve the punctuation when translating.
Don't make any assumptions about the capitalization of words in the dictionary. Instead, your program
should preserve capitalization in its translation. See the examples above.
Words not in the dictionary should be rendered in their original form. This allows for the preservation
of proper nouns (i.e. Python).
In [1]: b = [[1]]
In [2]: life(b, 0)
Out[2]: [[1]]
In [2]: life(b, 1)
Out[2]:
[[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
In [3]: life(b, 2)
Out[3]:
[[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
Note:
The Game of Life is a common exercise in introductory programming courses. Solutions are widely
available on the Internet. Please remember the rules for permitted resources (listed above), and be
sure the code you submit is uniquely your own. We will be carefully checking your submission for this
exercise.
Specically, avoid use of the copy and deepcopy methods. Most solutions available on the Internet
use these methods. However, these methods unnecessarily complicate the problem and using them
highly suggests copying.
In [1]: state_density_rank(1)
Out[1]: u'New Jersey'
In [2]: state_density_rank(50)
Out[2]: u'Alaska'
HINT: Try loading the JSON data in the interactive prompt rst in order to explore its structure, then write
your program.
NOTE: The u in front of each string here indicates that these are Unicode strings, which is the default when
reading JSON data. You shouldn't need to do anything out of the ordinary to support this.
ex9.py
(Review, 5 pts) The US National Weather Service measures cloud cover in percentage terms (0% = clear,
100% = overcast) The le minneapolis.json contains 48 weather observations of Minneapolis provided by
openweathermap.org. Write a function named avg_temp that will return a tuple containing the average
temperature (in degrees C and F) for all observations in the set that have the percentage cloud cover
determined by the argument. For example, the rst call returns the average temperature (in degrees C and
F) for clear observations and the second call returns the average temperature for observations with 40%
cloud cover:
In [1]: avg_temp(0)
Out[1]: (21.786666666666672, 71.21600000000002)
In [2]: avg_temp(40)
Out[2]: (22.13363636363639, 71.84054545454549)
In [3]: avg_temp(97)
Note that there are no observations with 97% cloud cover, so the function in this case returns the Python
special value of None. openweathermap.org provides a key that you may nd helpful in understanding the
le structure.
Did you know arm-wrestling is a competitive sport? Based in Chicago, the World Armwrestling League is
the largest and fastest growing professional arm-wrestling league in the world.
A contest consists of a 60-second period in which wrestlers A and B attempt to pin each other's arm
to the table.
A wrestler earns one point during the contest for pinning the other wrestler's arm to the table.
Each wrestler can score a maximum of one point during each contest. If both wrestlers score a
point before the 60 seconds expires, then the contest ends.
Like many sports, gambling is a large source of economic activity in professional arm-wrestling. One gam-
blimg scheme looks like this:
1. Each gambler places a bet on one or more single-digit numbers in the range 0 - 9.
2. After each contest, gamblers earn a payout if the last digit of the total score is a digit on which they
placed a bet.
For example, you place a $10 bet on the number 3. After two contests, the score is 2-1 (total of 3) and you
earn a $10 payout. After 10 contests, the score is 10 - 3 (total of 13) and you earn another $10 payout. Now
you've doubled your money! Good job.
Having taken this Python course, you want to take a more analytical approach to placing your arm-wrestling
bets. Write a function named payouts that accepts four arguments:
Figure 1 shows example probabilities for placing a $5 bet with two evenly-matched competitors (pa = pb = .5)
playing a match consisting of 9 contests. We determine the probabilities using a straightforward analysis of
each contest. For example, there are four possible outcomes of the rst contest:
Since two of the four scenarios end in a total score of 1, we see that the number 1 has a higher probability
of winning (p = .5) over the numbers 0 and 2. We obtain subsequent probabilities by multiplying the prior
probabilities. For example, the probability of digit 0 after contest two is equal to the probability of digit 0
after contest 1 times the probability of neither wrestler scoring a point in contest two.
According to our spreadsheet, at the end of this game the friend who bought number 2 should have the
highest payout. Let's check this against the output of our function:
If the probabilities are both 1, each wrestler will always score in each contest. The score after the rst contest
will total 2, after the second contest will total 4, and so on. Whoever bought even numbers will earn money,
while gamblers betting on odd numbers won't win anything:
In our nal scenario, wrestler A is much stronger (.8) than wrestler B (.2), the number 1 earns the highest
payout for a $25 wager in a match consisting of 15 contests: