Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is the base code to start with from operator import itemgetter def build_map( in_file1, in_file2 ): in_file1.readline() in_file2.readline() #READ EACH LINE FROM FILE 1
This is the base code to start with
from operator import itemgetter def build_map( in_file1, in_file2 ): in_file1.readline() in_file2.readline() #READ EACH LINE FROM FILE 1 # Split the line into two words (based on :) countries_list = line.strip().split() # Convert to Title case, discard whitespace continent = countries_list[0].strip().title() country = countries_list[1].strip().title() # Ignore empty strings if continent != "": # If current continent not in map, insert it # YOUR CODE # insert country (continent is guaranteed to be in map) #YOUR CODE # If current continent not in map, insert it #READ EACH LINE FROM FILE 2 # Split the line into two words (based on :) cities_list = line.strip().split() # Convert to Title case, discard whitespace country = cities_list[0].strip().title() City = cities_list[1].strip().title() # Ignore empty strings if country != "": # insert city (country is guaranteed to be in map) for continent in data_map: if country in data_map[continent]: # YOUR CODE def display_map( data_map ): #Modify this code to display a sorted nexted dictionary continents_list = [] #sorted list of the continent keys print("{}:".format(continents)) #continents in continents_list countries_list = [] #sorted list of the countries keys in the continents print("{:>10s} --> ".format(countries),end = '') #countries in countries_list cities = [] #sorted list of the cities #when printing add a comma and a spce after the cities names # if it is the last, don't add a comma and a space. print('{}, '.format(city),end = '') # city in cities print('{}'.format(city)) # city in cities def open_file(): try: filename = input("Enter file name: ") in_file = open( filename, "r" ) except IOError: print( " *** unable to open file *** " ) in_file = None return in_file def main(): # YOUR CODE in_file1 = open_file() #Continents with countries file: continents.txt in_file2 = open_file() #Countries with cities file: cities.txt if in_file1 != None and in_file2 != None: data_map = build_map( in_file1, in_file2 ) # data_map is a dictionary display_map( data_map ) in_file1.close() in_file2.close() if __name__ == "__main__": main()Il Sprint 13:58 cse.msu.edu and cities.txt contains city Country Bulgaria China Japan Tunisia Poland Germany Poland Bulgaria Nigeria China Tunisia France Japan Sofia Beijing Tokyo Sousse Warsaw Berlin Poznan Plovdiv Abuja Shanghai Tunis Paris Tokyo the output will be: Africa: NigeriaAbuja Tunisia Sousse, Tunis Asia: China-> Beijing, Shanghai Japan-> Tokyo Europe: Bulgaria ->Plovdiv, Sofia Germany Berlin PolandPoznan, Warsaw Demonstrate your completed program to your TA. On-line students should submit the completed prograrn (named "Lab09a.") for grading via the Mimir system. B. Write a program using Dictionaries of sets Rename your file from Part A to .1ab9b.". Given the same problemin part A, modify the program to replace lists with sets, i.e. use a dictionary of sets instead of dictionary of lists. Requirements: (1) You must use a dictionary of sets. Hints (1) You probably will only have to modify the build map function by replacing list initialization with set initialization and using set methods in place of list methods. (Whether you have to modify the display map function depends on how you implemented it in Part A.) Demonstrate your completed program to your TA. On-line students should submit the completed program (named "lab09b.py") for grading via the Mimir system. 80
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started