CIS-2212 Homework #1 (Processing a CSV File)

Due: Friday, September 10, 2021

Reading from Learning Python, 5th Edition: Chapter 4, Python Object Types. There is more in this chapter than we covered in class, so you might want to skim the latter sections. On the other hand, it's all good stuff, and we will talk about Python dictionaries soon. You may find the reference page on Python strings useful when doing this assignment. It includes a complete listing of all methods that can be applied to string objects (e.g., split).

The file Tokyo-2020-Medals.csv in the data folder of the cis-2212 repository is a comma-separated values (CVS) data file downloaded from Kaggle. It contains a table of data showing how many medals of each type were won by athletes from different countries during the 2020 (actually 2021) Summer Olympics held in Tokyo.

The assignment below asks you to build up a final program in stages. Submit just the overall result. You do not need to submit each stage separately. I suggest working one stage at a time in the order below. Try to get that stage working before starting the next stage. However, you may be able to implement a later stage even if you haven't gotten the previous ones working. Feel free to do that if necessary (the more that works, the better!)

Write a Python program named homework-01.py that does the following:

  1. Reads this file into a list of lines, disregarding the first line (which contains a header and not actual data). If the file does not open successfully, print an appropriate error message (you'll need an exception handler, see the datebook sample).

  2. Create a new list where each element of the list is a list of fields from the corresponding line of the data file (without the commas). For example, if the data file looked like:

              one,two,three
              apple,orange,grapefruit
            

    The list from part #1 above would be: ['one,two,three', 'apple,orange,grapefruit'] and the list from this part would be: [['one', 'two', 'three'], ['apple','orange','grapefruit']]. Here is where you can use the split method in the str class.

  3. The bubble_sort sample shows how to sort a list of text lines using the BubbleSort algorithm. Adapt that code into your program to sort the list of lists by the number of silver medals won. One element of the outer list is "less" than another element if the number of silver medals in that element is lower than the number of silver medals in the other element. You can access the number of silver medals in element 'i' of the outer list using an expression like: data[i][3]. Here the name of the overall (outer) list is 'data'. The value of 3 is the field number of where the silver medals appear (do you see why)?

  4. Print the top 10 elements of the sorted list showing just the name of the country and the number of silver medals won by that country. Print no other fields. Try to arrange the output in an attractive looking table (the details are up to you).

Submit your homework-01.py to Canvas.


Last Revised: 2025-01-09
© Copyright 2025 by Peter Chapin <peter.chapin@vermontstate.edu>