MIT 6.0002 Introduction to Computational Thinking and Data Science, Fall 2016 · Problem set 2, Problem 1: Creating the Data Structure Representation — WeightedEdge · Prof. Eric Grimson, Prof. John Guttag and Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Complete the WeightedEdge class, a subclass of Edge that also stores the two costs of traversing the edge: its total distance and its distance spent outdoors.
In this problem set we are dealing with edges that have different weights. Each edge of the MIT campus map has a cost in terms of total distance traveled and a cost in terms of distance spent outdoors. The distance spent outdoors for a single edge is always less than or equal to the total distance it takes to traverse that edge. For example, the cost of going from a to b to e might be a total distance traveled of 22 meters, of which 14 meters are spent outdoors.
The Node and Edge classes are given; they do not store weights. WeightedEdge(src, dest, total_distance, outdoor_distance) must keep both weights, return them from get_total_distance() and get_outdoor_distance(), and implement __str__ as follows. Suppose a WeightedEdge object e contains:
'a''b'Then str(e) should yield a->b (15, 10).
print(str(WeightedEdge(Node('a'), Node('b'), 15, 10))) # a->b (15, 10) print(WeightedEdge(Node('b'), Node('c'), 3, 1).get_outdoor_distance()) # 1