In this part of the lab, we will reimplement the sorting lab to use objects of your new City class
instead of just strings.
- Make a new copy of your lab13a.py file. Name it lab13b.py.
- 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="")
- 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)
- Copy your selection_sort and find_index_of_min function definitions from your Lab 11 code.
- At the end of main, call selection_sort on your list, and then call print_list to print it again.
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)
- Answer Question 3 in your writeup.
- 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.
- Comment out all of the code within main.
- 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.
- Call print_list on this list, and answer Question 4 in your writeup.
- 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.
- Next, you will loop to populate your new list of cities. Answer Question 5 in your writeup.
- 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 and answer Question 6:
for i in range(0, 5, 2):
print(i)
- Write a loop that will go over the lines of text read from the input file, incrementing i by 2 instead of 1.
- 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.
- Append the City object you created to the list of cities.
- 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)
- Add code to selection-sort the list of cities and print the old and new lists. Comment out the lines
that print which elements were swapped.
- 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)
- When your code matches the sample output, call an instructor to demo it.
- Continue to Part C to submit your code.