Graded Problem Set #3.4
File Problem
Background
These problems should be completed on your own. Unlike the practice problems, where you are allowed, and even encouraged, to talk about the process with your classmates, these should be attempted on your own. If you have difficulty with these, please reach out to me for assistance or guidance.
These problems will be completed using an editor such as Thonny or IDLE and submitted to Autolab for grading.
Introduction
We all love a good Olympic story. I mean, who didn't tear up when they watched the Jamaican bobsled team in Cool Runnings?
We all cheer for the good old U.S. of A. but we also like to cheer for the underdogs. But what exactly does it mean to be an underdog? Sure, a team that doesn't even really know about snow is an underdog. But other than that, a single medal won by a team from a tiny country just might be more impressive than 3 medals won by a much larger country.
In this activity you will look at the ratio of size of a country compared to the # of medals it won to try to figure out which countryreally performed better than we might expect.
Program Guide
For this assignment you should create the following functions in a file called olympics.py. You will make three functions. The first two are smaller, helper functions, that will be used by the third function
For this assignment you will be working with several potential data sets:
- 2012_Olympic_Medals.csv [From the summer Olympics in London, England] The larger file
- 2014_Olympic_Medals.csv [From the winter Olympics in Sochi, Russia] This is the smaller file
- 2018_Olympic_Medals.csv [ From the winter Olympics in Korea]
Create a function called: totalPopulation()
This function should:
- Parameters:
- input file name - (you may use any of the files from above for testing)
- Actions:
- Opens the input file
- "Throws away" the header file
- Calculates the sum of the populations in the file
- Closes the file
- Returns:
- The total population of all of the countries who won medals at that Olympics
- Example Run
Create a function called: totalMedals()
This function should:
- Parameters:
- input file name - (you may use any of the files above for testing)
- Actions:
- Opens the input file
- "Throws away" the header file
- Calculates the sum of ALL of the medals won in the file
- Closes the file
- Returns:
- The total medal count for all of the countries who won medals at that Olympics
- Example Run
Create a function called: medalRatios()
This function should:
- Take Two Parameters:
- input file name - (you may use either of the files above for testing)
- output file name
- Returns:
- Nothing
- Actions:
- Opens the input file
- Opens the output file
- Pulls off the header and adds one more column labeled "Medal Ratio"
- For each country provided in the data file you opened:
- extract population and total medals won just for that country
- calculate the medal ratio of medals to population per billion people as done above
- write the original data PLUS the country's medal ratio (rounded to 2 decimal places)
- Closes both files
- Example Run
- Note, you do not need to include the print statement I did in my example. But you may.
- Example Output
Optional
Extend the previous function: medalRatios_with_category()
This function uses what you did previously but adds one more column to the output file. Once you have medalRatios() working you should copy and paste the function and then change the name to
- Take Two Parameters:
- input file name - (you may use either of the files above for testing)
- output file name
- Returns:
- Nothing
- Actions:
- Uses the previous two functions to calculate the RATIO of how many medals were won for every billion (1,000,000,000) people.
- For example, in 2014 there were:
- 295 / 2,516,757,975 * 1000000000 =117.2 medals awarded for every billion people from a competing country.
- HINT: Notice that I am explicity saying that this should be done first and outside of the loop.
- Opens the input file
- Opens the output file
- Pulls off the header and adds two more columns labeled "Medal Ratio" and "Classification"
- For each country provided in the data file you opened:
- extract population and total medals won just for that country
- calculate the medal ratio of medals to population per billion people as done above
- calculates the "comparison ratio" which is the country's medal ratio divided by the world's medal ratio
- determines where this "comparison ratio" lies compared to the overall average for the Olympics:
-
Comparision Ratio Result < 0.50 [In other words the country wins less than 50% of the world average] WELL Below Average between 0.50 and 0.95 inclusive Below Average between 0.95 and 1.05 exclusive Average between 1.05 and 1.50 inclusive Above Average > 1.50 WELL Above Average
-
- writes the original data PLUS the country's medal ratio (rounded to 2 decimal places) and the classification from the table above
- Closes both files
- Uses the previous two functions to calculate the RATIO of how many medals were won for every billion (1,000,000,000) people.
- Example Run
- Note, you do not need to include the print statement I did in my example. But you may.
- This is mostly there to show you that you only need to calculate this value ONCE and not everytime your loop runs [Related to my previous hint].
- Example Output