167k views
1 vote
ASSIGNMENT 9 FLIGHT TRACKER (project stem)

Create and initialize a 2D list as shown below.

Miami Atlanta Dallas Los Angeles
New York Chicago Portland Sacramento

Imagine you are coordinating the flights for two planes that hit various cities across the US, East to West. Moreover, these planes hit the same cities on the way back West to East. Write a function that prints out the specified cities East to West and then flips them West to East to illustrate the flight back.

First, write a printList() function that prints out the list. Use a single parameter in the definition of the function, and pass the list you created above as the parameter. Call the function so it prints the original list.

Then, write a flipOrder() function that reverses the order of each sublist containing cities in order to illustrate the stops made on the way back from the trip. Again, this function should pass the original list as the parameter. This function should also use the printList() function at the end to print the reversed list.

Sample Output:
Miami Atlanta Dallas Los Angeles
New York Chicago Portland Sacramento

Los Angeles Dallas Atlanta Miami
Sacramento Portland Chicago New York

1 Answer

3 votes

Final answer:

To solve this problem, you need to create a 2D list with the given cities. First, write a printList() function that takes the list as a parameter and prints it. Then, create a flipOrder() function that reverses the order of each sublist in the original list.

Step-by-step explanation:

To solve this problem, you need to create a 2D list with the given cities. First, write a printList() function that takes the list as a parameter and prints it. Then, create a flipOrder() function that reverses the order of each sublist in the original list. At the end of this function, call printList() again to print the reversed list. This will give you the desired output of the cities in the original order and the reversed order.

Example:

def printList(city_list):
for sublist in city_list:
for city in sublist:
print(city, end=' ')
print()

def flipOrder(city_list):
for sublist in city_list:
sublist.reverse()
printList(city_list)

# Create the 2D list
list_of_cities = [['Miami', 'Atlanta', 'Dallas', 'Los Angeles'], ['New York', 'Chicago', 'Portland', 'Sacramento']]

# Call printList() to print the original list
printList(list_of_cities)

# Call flipOrder() to print the reversed list
flipOrder(list_of_cities)

User JacopKane
by
7.8k points