Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write a python code from given picture and choose a 1 problam from group A and from group B.. write the python program ... Problem:
write a python code from given picture and choose a 1 problam from group A and from group B.. write the python program ...
Problem: The purpose of this problem is to help the railroad provide its customers with information about the routes. In particular, you will compute the distance along a certain route, the number of different routes between two towns, and the shortest route between two towns. Input: A directed graph where a node represents a town and an edge represents a route between two towns. The weighting of the edge represents the distance between the two towns. A given route will never appear more than once, and for a given route, the starting and ending town will not be the same town. Output: For test input 1 through 5, if no such route exists, output 'NO SUCH ROUTE'. Otherwise, follow the route as given; do not make any extra stops! For example, the first problem means to start at city A, then travel directly to city B (a distance of 5), then directly to city C (a distance of 4). 1.6. 1 7 . 8 1 10 1. 11 1 12 13 Choose 1 problem from Group A: A1) The distance of the route A-B-C. A2) The distance of the route A-D. A3) The distance of the route A-D-C. A4) The distance of the route A-E-B-C-D. glish (Canada) A5) The distance of the route A-E-D. Choose 1 problem from Group B: B1) The number of trips starting at C and ending at C with a maximum of 3 stops. In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops). B2) The number of trips starting at A and ending at C with exactly 4 stops. In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D, E,B). B3) The length of the shortest route (in terms of distance to travel) from A to C B4) The length of the shortest route (in terms of distance to travel) from B to B. B5) The number of different routes from C to C with a distance of less than 30. In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC. Test Input: For the test input, the towns are named using the first few letters of the alphabet from A to E. A route between two towns (A to B) with a distance of 5 is represented as AB5. Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7 Expected Output: Output #1:9 Output#2:5 Output#3: 13 14 1.1 Output #4:22 Output #5: NO SUCH ROUTE Output #6:2 I Output#7:3 Output #8:9 Output #9:9 Output#10:7 Example Code Snippet (doesn't run, has defects but outlines an approach, provided as a starting point, feel free to modify, improveand create your approach) #!/usr/local/bin/python3 edges = [['A','B',5],['B','C',4],['c','D',8],['D','C',8],['D','E',6],['A', 'D',5],['c','E', 2],['E','B',3],['A' ,'E',7]] I #can also be implemented with dictionaries edges = { 'A':[['B',5],['D',5],['E',7]], 'B':[['C',4]], 'C':[['D',8],['E',2]] 'D':[['C',8],['E',6]], 'E':[['B',3]] } #this assumes that there is a unique route route = ['A','B','C') distance = 0 routestep = 0 fromnode = route[routestep] tonode = route[routestep+1] foradan in 13 14 for edge in edges: if edge[0] is fromnode: if edge[1] is tonode: print("distance:"+str(distance) print("fromnode:"+str(fromnode)) print("tonode:"+str(tonode)) distance += edge[2] routestep += 1 fromnode = route[routestep] tonode = route[routestep+1] if tonode is route[len(route)-1]: breakStep 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