def do_count_trips_max_stops(self, line): """Counts the number of trips given maximum number of stops ** Use calculate_num_trips_max_stops with parameters: <start> - start_point <finish> - end_point <maximum number of stops> """ parameters = line.split() if line: if len(parameters) != 3: print("Wrong number of arguments") else: start, finish, max_number = parameters start = TownNode(start) finish = TownNode(finish) max_number = int(max_number) if not parameters or len(parameters) != 3: print("Enter start point: ") start = TownNode(input()) print("Enter finish point: ") finish = TownNode(input()) print("Enter max number of stops: ") max_number = int(input()) result = RouteGraph(routes=self.graph).count_trips_max_stops( start, finish, max_number) print(f"Number of trips from {start} to {finish} " f"given {max_number} max stops is {result}")
def setUp(self): """Creates TownNodes and Graphs for each test""" # creating Towns self.a = TownNode("A") self.b = TownNode("B") self.c = TownNode("C") self.d = TownNode("D") self.e = TownNode("E") # creating routes self.graph = { self.a: [ DistanceEdge(self.a, self.b, 5), DistanceEdge(self.a, self.d, 5), DistanceEdge(self.a, self.e, 7) ], self.b: [DistanceEdge(self.b, self.c, 4)], self.c: [DistanceEdge(self.c, self.d, 8), DistanceEdge(self.c, self.e, 2)], self.d: [DistanceEdge(self.d, self.c, 8), DistanceEdge(self.d, self.e, 6)], self.e: [DistanceEdge(self.e, self.b, 3)] }
def do_shortest_route(self, line): """Calculates the shortest route between two points in terms of distance ** Use shortest_route with parameters: <start> - start_point <finish> - end_point """ parameters = line.split() if line: if len(parameters) != 2: print("Wrong number of arguments") else: start, finish = parameters start = TownNode(start) finish = TownNode(finish) if not parameters or len(parameters) != 2: print("Enter start point: ") start = TownNode(input()) print("Enter finish point: ") finish = TownNode(input()) result = RouteGraph(routes=self.graph).shortest_route(start, finish) print(f"The shortest distance from {start} to {finish} is {result}")
def do_calculate_distance(self, route): """Calculates distances between towns. Type desired as such: A-B-C""" if not route: print("Type desired route in format: A-B-C") towns = input().split("-") else: towns = route.split("-") towns = [TownNode(town) for town in towns] result = RouteGraph(routes=self.graph).count_full_distance(towns) print(f"The distance is {result}")
def make_default_graph(): # creating Towns a = TownNode("A") b = TownNode("B") c = TownNode("C") d = TownNode("D") e = TownNode("E") # creating routes graph = { a: [DistanceEdge(a, b, 5), DistanceEdge(a, d, 5), DistanceEdge(a, e, 7)], b: [DistanceEdge(b, c, 4)], c: [DistanceEdge(c, d, 8), DistanceEdge(c, e, 2)], d: [DistanceEdge(d, c, 8), DistanceEdge(d, e, 6)], e: [DistanceEdge(e, b, 3)] } return graph
def do_add_route(self, line): """Add two towns to database and the distance between them""" print("Do you want to use default graph or enter your own?\n" "Type 'default' or 'my'") answer = input() if answer == 'default': self.graph = make_default_graph() elif answer == 'my': print("Enter first town name: ") first_town = TownNode(input()) print(f"First town {first_town} has been saved\n" f"Enter second town name: ") second_town = TownNode(input()) print(f"Second town {second_town} has been saved\n" f"Enter distance between two cities: ") distance = int(input()) self.graph[first_town].append( DistanceEdge(first_town, second_town, weight=distance)) print(f"Thank you, the route {first_town}{second_town}{distance}" f" has been saved.\n Would you like to add more? (y/n)") answer = input() while answer not in ['y', 'n']: print("Please enter 'y' or 'n': \n" "If you want to exit press command+D") answer = input() if answer == 'y': self.do_add_route(line) else: self.do_add_route(line) print("All the towns have been saved to the 'graph' dict")
def do_count_routes_with_distance(self, line): """ Counts the number of different routes between two towns with a given maximum distance ** Use count_routes_with_distance with paramenters: <start> <finish> <maximum distance> """ parameters = line.split() if line: if len(parameters) != 3: print("Wrong number of arguments") else: start, finish, max_distance = parameters start = TownNode(start) finish = TownNode(finish) max_distance = int(max_distance) if not parameters or len(parameters) != 3: print("Enter start point: ") start = TownNode(input()) print("Enter finish point: ") finish = TownNode(input()) print("Enter max number of stops: ") max_distance = int(input()) result = RouteGraph(routes=self.graph).count_routes_given_distance( start, finish, max_distance) print(f"Number of trips from {start} to {finish} " f"given maximum distance of {max_distance} " f"is {result}")