61A Homework 10, Part A

Due by 11:59pm on Friday, 5/2

Homework 10 is in two parts, of which this is the first. The solutions to this part are in Python. Those for 10b are in logic notation.

Submission. See the online submission instructions. Submit the files for hw10a and hw10b together as hw10. We have provided a hw10a starter file for the questions below.

Readings. Sections `4.2 4.3, and 4.4 of Composing Programs.

Q1. (Objects and recursion review) A mobile is a type of hanging sculpture. A simple binary mobile consists of two branches, left and right. Each branch is a rod of a certain length, from which hangs either a weight or another mobile.

Improve the classes for Branch, Weight, and Mobile below in the following ways:

When you are finished, all doctests below should pass:

class Mobile:
    """A simple binary mobile that has branches of weights or other mobiles.

    >>> Mobile(1, 2)
    Traceback (most recent call last):
    TypeError: 1 is not a Branch
    >>> m = Mobile(Branch(1, Weight(2)), Branch(2, Weight(1)))
    >>> m.weight
    >>> m.is_balanced()
    >>> m.left.contents = Mobile(Branch(1, Weight(1)), Branch(2, Weight(1)))
    >>> m.weight
    >>> m.left.contents.is_balanced()
    >>> m.is_balanced() # All submobiles must be balanced for m to be balanced
    >>> m.left.contents.right.contents.weight = 0.5
    >>> m.left.contents.is_balanced()
    >>> m.is_balanced()
    >>> m.right.length = 1.5
    >>> m.is_balanced()

    def __init__(self, left, right):
        "*** YOUR CODE HERE ***"
        self.left = left
        self.right = right

    def weight(self):
        """The total weight of the mobile."""
        "*** YOUR CODE HERE ***"

    def is_balanced(self):
        """True if and only if the mobile is balanced."""
        "*** YOUR CODE HERE ***"

def check_positive(x):
    """Check that x is a positive number, and raise an exception otherwise.

    >>> check_positive('hello')
    Traceback (most recent call last):
    TypeError: hello is not a number
    >>> check_positive('1')
    Traceback (most recent call last):
    TypeError: 1 is not a number
    >>> check_positive(-2)
    Traceback (most recent call last):
    ValueError: -2 <= 0
    "*** YOUR CODE HERE ***"

class Branch:
    """A branch of a simple binary mobile."""

    def __init__(self, length, contents):
        if type(contents) not in (Weight, Mobile):
            raise TypeError(str(contents) + ' is not a Weight or Mobile')
        self.length = length
        self.contents = contents

    def torque(self):
        """The torque on the branch"""
        return self.length * self.contents.weight

class Weight:
    """A weight."""
    def __init__(self, weight):
        self.weight = weight

    def is_balanced(self):
        return True

Q2. (Python evaluation and recursion review) Your partner designed a beautiful balanced Mobile, but forgot to fill in the classes of each part, instead just writing T.


The built-in Python funciton eval takes a string argument, evaluates it as a Python expression, and returns its value.

Complete the definition of interpret_mobile so that it returns a well-formed mobile by guessing the class for each T. The function should exhaustively test all possible combinations of types, then attempt to eval the resulting string when no T remains, handling TypeErrors until a correct series of types is found.

Warning: Interpreting a large mobile is quite slow (can you say why?). You will want to remove the doctest for the large mobile during development:

def interpret_mobile(s):
    """Return a Mobile described by string s by substituting one of the classes
    Branch, Weight, or Mobile for each occurrenct of the letter T.

    >>> simple = 'Mobile(T(2,T(1)), T(1,T(2)))'
    >>> interpret_mobile(simple).weight
    >>> interpret_mobile(simple).is_balanced()
    >>> s = 'T(T(4,T(T(4,T(1)),T(1,T(4)))),T(2,T(10)))'
    >>> m = interpret_mobile(s)
    >>> m.weight
    >>> m.is_balanced()
    next_T = s.find('T')        # The index of the first 'T' in s.
    if next_T == -1:            # The string 'T' was not found in s
            return eval(s)      # Interpret s
        except TypeError as e:
            return None         # Return None if s is not a valid mobile
    for t in ('Branch', 'Weight', 'Mobile'):
        "*** YOUR CODE HERE ***"
    return None

Q3. An enhancement of the Stream class from lecture appears below, along with a function that returns an infinite stream of integers. To extend it, implement an __iter__ method using a yield statement that returns a generator over the elements of the stream. Also add a __getitem__ method to support item selection.

The zip function in the doctest for __iter__ combines the corresponding elements of two iterables into pairs until one iterator raises StopIteration:

class Stream:
    """A lazily computed recursive list."""

    class empty:
        def __repr__(self):
            return 'Stream.empty'
    empty = empty()

    def __init__(self, first, compute_rest=lambda: Stream.empty):
        assert callable(compute_rest), 'compute_rest must be callable.'
        self.first = first
        self._compute_rest = compute_rest

    def rest(self):
        """Return the rest of the stream, computing it if necessary."""
        if self._compute_rest is not None:
            self._rest = self._compute_rest()
            self._compute_rest = None
        return self._rest

    def __repr__(self):
        return 'Stream({0}, <...>)'.format(repr(self.first))

    def __iter__(self):
        """Return an iterator over the elements in the stream.

        >>> list(zip(range(6), ints))
        [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
        "*** YOUR CODE HERE ***"

    def __getitem__(self, k):
        """Return the k-th element of the stream.

        >>> ints[5]
        >>> increment_stream(ints)[7]
        "*** YOUR CODE HERE ***"

def increment_stream(s):
    """Increment all elements of a stream."""
    return Stream(s.first+1, lambda: increment_stream(s.rest))

# The stream of consecutive integers starting at 1.
ints = Stream(1, lambda: increment_stream(ints))

Q4. Implement the function scale_stream, which returns a stream over each element of an input stream, scaled by k:

def scale_stream(s, k):
    """Return a stream of the elements of S scaled by a number K.

    >>> s = scale_stream(ints, 5)
    >>> s.first
    >>> s.rest
    Stream(10, <...>)
    >>> scale_stream(s.rest, 10)[2]
    "*** YOUR CODE HERE ***"

Q5. A famous problem, first raised by Richard Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. These are called regular numbers. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, we can build a stream of such numbers. Let us call the required stream of numbers s and notice the following facts about it.

Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions.

Fill in the definition of merge, then fill in the definition of make_s below:

def merge(s0, s1):
    """Return a stream over the elements of increasing s0 and s1, removing

    >>> twos = scale_stream(ints, 2)
    >>> threes = scale_stream(ints, 3)
    >>> m = merge(twos, threes)
    >>> [m[i] for i in range(10)]
    [2, 3, 4, 6, 8, 9, 10, 12, 14, 15]
    if s0 is Stream.empty:
        return s1
    elif s1 is Stream.empty:
        return s0

    e0, e1 = s0.first, s1.first
    "*** YOUR CODE HERE ***"

def make_s():
    """Return a stream over all positive integers with only factors 2, 3, & 5.

    >>> s = make_s()
    >>> [s[i] for i in range(20)]
    [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36]
    def rest():
        "*** YOUR CODE HERE ***"
    s = Stream(1, rest)
    return s