Problem 1.1 Weights

Implement the planet data abstraction by completing the planet constructor and the size selector so that a planet is represented using a two-element list where the first element is the string 'planet' and the second element is its size. The total_weight example is provided to demonstrate use of the mobile, arm, and planet abstractions.

# The constructor and selectors of the planet def planet(size): """Construct a planet of some size. >>> planet(5) ['planet', 5] """ assert size > 0 "*** YOUR CODE HERE ***" def size(w): """Select the size of a planet. >>> p = planet(5) >>> size(p) 5 """ assert is_planet(w), 'must call size on a planet' "*** YOUR CODE HERE ***" def is_planet(w): """Whether w is a planet.""" return type(w) == list and len(w) == 2 and w[0] == 'planet'
# examples and usage def examples(): t = mobile(arm(1, planet(2)), arm(2, planet(1))) u = mobile(arm(5, planet(1)), arm(1, mobile(arm(2, planet(3)), arm(3, planet(2))))) v = mobile(arm(4, t), arm(2, u)) return (t, u, v) def total_weight(m): """Return the total weight of m, a planet or mobile. >>> t, u, v = examples() >>> total_weight(t) 3 >>> total_weight(u) 6 >>> total_weight(v) 9 """ if is_planet(m): return size(m) else: assert is_mobile(m), "must get total weight of a mobile or a planet" return total_weight(end(left(m))) + total_weight(end(right(m)))

Note: the code listed above is the part you should fill in for the lab, see lab04.py for the complete codes.