Question: python Q3: City Visits - 20 pts The file associated with this question is CityVisits.py. Part A (10 points): Complete the function build_dict(num_visits) in the
python
Q3: City Visits - 20 pts The file associated with this question is CityVisits.py. Part A (10 points): Complete the function build_dict(num_visits) in the given file. This part asks you to build a dictionary that will store some information about how many nights on average travellers stay in a city and which city they go to next. You are given each trip information with a file, placed under the city_visits folder as visit{i}.txt where i is between 0 and 49 (i.e. 50 trips in total). Each line of a visit file has the city and the duration of stay as nights. The following line corresponds to the next city and duration of stay etc. For example the visit1.txt is as follows: Hong Kong, 4 Palma de Mallorca, 9 Barcelona, 3 Ankara, 3 This file describes a trip that started in Hong Kong, lasted 4 nights. Then the traveller went to Palma de Mallorca and stayed 9 nights, followed by Barcelona and 3 nights. Finally they finished their trip in Ankara with 3 nights. The dictionary that you are asked to build should have cities as its keys. The values will be a list. The first entry of the list is the average number of nights per visit. The second entry will be another list which will include the city/cities visited next. This inner list should have unique entries (i.e. do not add a city twice). It can also be empty if there wasn't any other next visited city. Note that these will be updated as you read more files. Hint: It is okay to update the average number of nights after first building the dictionary! After going over only the above file, the graph should be like this: G = { 'Hong Kong': [4, ['Palma de Mallorca']], 'Palma de Mallorca': [9, ['Barcelona']], 'Barcelona': [3, ['Ankara']], 'Ankara': [3, [1]} There are more examples in the file. If you have any concerns, look at the first three trip files (visito.txt, visit1.txt and visit2.txt) and the expected output. Notice how Istanbul has 8.5 average nights (due to a 7 and a 10 night stay). There is starter code for you to help with reading the files. Feel free to modify it to your liking. In fact, you need to add your own code within the loops. Q3: City Visits - 20 pts The file associated with this question is CityVisits.py. Part A (10 points): Complete the function build_dict(num_visits) in the given file. This part asks you to build a dictionary that will store some information about how many nights on average travellers stay in a city and which city they go to next. You are given each trip information with a file, placed under the city_visits folder as visit{i}.txt where i is between 0 and 49 (i.e. 50 trips in total). Each line of a visit file has the city and the duration of stay as nights. The following line corresponds to the next city and duration of stay etc. For example the visit1.txt is as follows: Hong Kong, 4 Palma de Mallorca, 9 Barcelona, 3 Ankara, 3 This file describes a trip that started in Hong Kong, lasted 4 nights. Then the traveller went to Palma de Mallorca and stayed 9 nights, followed by Barcelona and 3 nights. Finally they finished their trip in Ankara with 3 nights. The dictionary that you are asked to build should have cities as its keys. The values will be a list. The first entry of the list is the average number of nights per visit. The second entry will be another list which will include the city/cities visited next. This inner list should have unique entries (i.e. do not add a city twice). It can also be empty if there wasn't any other next visited city. Note that these will be updated as you read more files. Hint: It is okay to update the average number of nights after first building the dictionary! After going over only the above file, the graph should be like this: G = { 'Hong Kong': [4, ['Palma de Mallorca']], 'Palma de Mallorca': [9, ['Barcelona']], 'Barcelona': [3, ['Ankara']], 'Ankara': [3, [1]} There are more examples in the file. If you have any concerns, look at the first three trip files (visito.txt, visit1.txt and visit2.txt) and the expected output. Notice how Istanbul has 8.5 average nights (due to a 7 and a 10 night stay). There is starter code for you to help with reading the files. Feel free to modify it to your liking. In fact, you need to add your own code within the loops
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
