Beispiel #1
0
class RailwaySampleDataTest(unittest.TestCase):
    def setUp(self):
        routes = "AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7"
        self.rail = Railway(routes)

    def test_distances(self):
        self.assertEqual(self.rail.get_route_distance('A', 'B', 'C'), 9)
        self.assertEqual(self.rail.get_route_distance('A', 'D'), 5)
        self.assertEqual(self.rail.get_route_distance('A', 'D', 'C'), 13)
        self.assertEqual(self.rail.get_route_distance('A', 'E', 'B', 'C', 'D'),
                         22)
        self.assertEqual(self.rail.get_route_distance('A', 'E', 'D'), None)

    def test_find_routes_by_stops(self):
        self.assertEqual(len(self.rail.find_routes_by_stops('C', 'C', 3)), 2)
        self.assertEqual(
            len(
                self.rail.find_routes_by_stops('A', 'C', 4,
                                               Railway.FindModes.exact)), 3)

    def test_find_shortest_route(self):
        self.assertEqual(self.rail.shortest_route('A', 'C'), 9)
        self.assertEqual(self.rail.shortest_route('B', 'B'), 9)

    def test_find_routes_by_distance(self):
        self.assertEqual(len(self.rail.find_routes_by_distance('C', 'C', 30)),
                         7)
 def test_find_routes_by_stops(self):
     routes = ['AB1', 'AC2', 'CA4']
     rail = Railway(routes)
     self.assertRaises(ValueError, rail.find_routes_by_stops, None, 'A', 5)
     self.assertRaises(ValueError, rail.find_routes_by_stops, 'A', 'B', 'Z')
     self.assertRaises(KeyError, rail.find_routes_by_stops, 'D', 'A', 5)
     self.assertEqual(rail.find_routes_by_stops('A', 'A', 3),
                      [(['A', 'C', 'A'], 6)])
     self.assertEqual(rail.find_routes_by_stops('A', 'A', '3'),
                      [(['A', 'C', 'A'], 6)])
     self.assertEqual(rail.find_routes_by_stops('A', 'C', 3),
                      [(['A', 'C'], 2)])
     self.assertEqual(
         rail.find_routes_by_stops('A', 'C', 4, Railway.FindModes.exact),
         [])
     self.assertEqual(
         rail.find_routes_by_stops('A', 'C', 5, Railway.FindModes.exact),
         [(['A', 'C', 'A', 'C', 'A', 'C'], 14)])
Beispiel #3
0
 print('')
 menu_selection = input('Your selection: ').upper()
 if menu_selection == '1':
     print('Output #1: {}'.format(
         rail.get_route_distance('A', 'B', 'C') or 'NO SUCH ROUTE'))
     print('Output #2: {}'.format(
         rail.get_route_distance('A', 'D') or 'NO SUCH ROUTE'))
     print('Output #3: {}'.format(
         rail.get_route_distance('A', 'D', 'C') or 'NO SUCH ROUTE'))
     print('Output #4: {}'.format(
         rail.get_route_distance('A', 'E', 'B', 'C', 'D')
         or 'NO SUCH ROUTE'))
     print('Output #5: {}'.format(
         rail.get_route_distance('A', 'E', 'D') or 'NO SUCH ROUTE'))
     print('Output #6: {}'.format(
         len(rail.find_routes_by_stops('C', 'C', 3))))
     print('Output #7: {}'.format(
         len(rail.find_routes_by_stops('A', 'C', 4,
                                       Railway.FindModes.exact))))
     print('Output #8: {}'.format(rail.shortest_route('A', 'C')))
     print('Output #9: {}'.format(rail.shortest_route('B', 'B')))
     print('Output #10: {}'.format(
         len(rail.find_routes_by_distance('C', 'C', 30))))
 elif menu_selection == '2':
     origin = input('Please enter the origin station: ')
     next_stop = input('Please enter the destination station: ')
     stops = [next_stop]
     while True:
         next_stop = input(
             'Please enter the next destination station, or ! to stop: ')
         if next_stop == '!':