CS 115 Lab 13, Part B: Sort City objects

[Back to lab instructions]


Instructions

Overview

In this part of the lab, we will reimplement the sorting lab to use objects of your new City class instead of just strings.

Instructions

  1. Make a new copy of your lab13a.py file. Name it lab13b.py.
  2. Add this definition of the print_list function to your code:
    def print_list(list_to_print):
        '''
        Print the contents of a list.
    
        Parameter: the list to print
        Returns: nothing
        '''
    
        for i, item in enumerate(list_to_print):
            print(i, ': ', item, sep="")
  3. At the bottom of the definition of main, add the following code:
    testlist = [tokyo, mexico_city]
    Then add one more line that calls the print_list function to print testlist. Comment out all of the other print statements, and run your program. You should see:
    0: Tokyo (pop. 13189000)
    1: Mexico City (pop. 8857188)
  4. Copy your selection_sort and find_index_of_min function definitions from your Lab 11 code.
  5. At the end of main, call selection_sort on your list, and then call print_list to print it again. Comment out the lines (in selection_sort) that print which elements were swapped. After adding some blank lines and additional print statements, you should see the following output:
    The original list of cities is:
    0: Tokyo (pop. 13189000)
    1: Mexico City (pop. 8857188)
    
    The new list of cities is:
    0: Mexico City (pop. 8857188)
    1: Tokyo (pop. 13189000)
  6. Answer Question 3 in your writeup.
  7. The next challenge is to read a list of cities from a file. Copy the definition of readfile from a previous lab. Use a version that returns a list of strings, where each string is one line of the input file.
  8. Comment out all of the code within main.
  9. In main, add code that asks the user for the name of an input file and calls readfile to read its contents into a list.
  10. Call print_list on this list, and answer Question 4 in your writeup.
  11. Inside main, and before the call to print_list, you will insert code to create a list of City objects based on the lines of the input file.

    First, create a new, empty list for your list of cities.
  12. Next, you will loop to populate your new list of cities. Answer Question 5 in your writeup.
  13. It is possible to write a loop that increments the loop counter by 2 instead of 1 each time you go through the loop. Type the following code into the the online Python 3 tutor:
    for i in range(0, 5, 2):
        print(i)
  14. Write a loop that will go over the lines of text read from the input file, incrementing i by 2 instead of 1.
  15. Within that loop, create a new City object each iteration. Element i of your list is the name of the city, and element i + 1 is its population. Don't forget to convert the population to an integer.
  16. Append the City object you created to the list of cities.
  17. Modify the call to print_list so that it prints your list of cities, not the list of lines in the input file. Sample output:
    Name of input file: cities-small-pop.txt
    0: Santa Rosa (pop. 167815)
    1: Petaluma (pop. 57941)
    2: Rohnert Park (pop. 40971)
    3: Windsor (pop. 26801)
    4: Healdsburg (pop. 11254)
  18. Add code to selection-sort the list of cities and print the old and new lists.
  19. Sample output:
    Name of input file: cities-small-pop.txt
    
    The original list of cities is:
    0: Santa Rosa (pop. 167815)
    1: Petaluma (pop. 57941)
    2: Rohnert Park (pop. 40971)
    3: Windsor (pop. 26801)
    4: Healdsburg (pop. 11254)
    
    The new list of cities is:
    0: Healdsburg (pop. 11254)
    1: Windsor (pop. 26801)
    2: Rohnert Park (pop. 40971)
    3: Petaluma (pop. 57941)
    4: Santa Rosa (pop. 167815)
  20. When your code matches the sample output, call an instructor to demo it.
  21. Continue to Part C to submit your code.