Due by 11:59pm on Friday, 4/15


Download hw07.zip. Inside the archive, you will find a file called hw07.py, along with a copy of the OK autograder.

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored. See Lab 0 for instructions on submitting assignments.

Using OK: If you have any questions about using OK, please refer to this guide.

Readings: You might find the following references useful:


Question 1: Generate permutations

Given a list of unique elements, a permutation of the list is a reordering of the elements. For example, [2, 1, 3], [1, 3, 2], and [3, 2, 1] are all permutations of the list [1, 2, 3].

Implement permutations, a generator function that takes in a lst and outputs all permutations of lst, each as a list (see doctest for an example).

def permutations(lst):
    """Generates all permutations of sequence LST.  Each permutation is a
    list of the elements in LST in a different order.

    >>> sorted(permutations([1, 2, 3]))
    [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
    >>> type(permutations([1, 2, 3]))
    <class 'generator'>
    >>> sorted(permutations((10, 20, 30)))
    [[10, 20, 30], [10, 30, 20], [20, 10, 30], [20, 30, 10], [30, 10, 20], [30, 20, 10]]
    >>> sorted(permutations("ab"))
    [['a', 'b'], ['b', 'a']]
    if not lst:
        yield []
    "*** YOUR CODE HERE ***"

The order in which you generate permutations is irrelevant. Hint: This problem yields to recursion. If you had the permutations of a subsequence of a that was missing one element, how could generate from it the permutations of the full sequence?

Use OK to test your code:

python3 ok -q permutations


The Stream class defines a lazy sequence, a lazily evaluated linked list. In other words, a Stream's elements (except for the first element) are only evaluated as those values are needed.

class Stream:
    """A lazily computed linked 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))

Instead of specifying all of the elements in __init__, we provide a function, compute_rest, that encapsulates the algorithm used to calculate the remaining elements of the stream. The code in the function body is not evaluated until it is called, which lets us implement the desired evaluation behavior.

This implementation of streams also uses memoization. The first time a program asks a Stream for its rest field, the Stream code computes the required value using compute_rest, saves the resulting value, and then returns it. After that, every time the rest field is referenced, the stored value is simply returned and it is not computed again.

Here is an example (which you may use in solutions):

def make_integer_stream(first=1):
    def compute_rest():
        return make_integer_stream(first+1)
    return Stream(first, compute_rest)

We start out with a stream whose first element is 1, and whose compute_rest function creates another stream. So when we do compute the rest, we get another stream whose first element is one greater than the previous element, and whose compute_rest creates another stream. Hence, we effectively get an infinite stream of integers, computed one at a time. This is almost like an infinite recursion, but one which can be viewed one step at a time, and so does not crash.

Another example:

def map_stream(fn, s):
    if s is Stream.empty:
        return s
    return Stream(fn(s.first), lambda: map_stream(fn, s.rest))

Question 2: Scale Stream

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.

    >>> ints = make_integer_stream(1)
    >>> s = scale_stream(ints, 5)
    >>> stream_to_list(s, 5)
    [5, 10, 15, 20, 25]
    >>> s = scale_stream(Stream("x", lambda: Stream("y")), 3)
    >>> stream_to_list(s)
    ['xxx', 'yyy']
    >>> stream_to_list(scale_stream(Stream.empty, 10))
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q scale_stream

Question 3: Efficient Map

The map-stream function:

def map_stream(fn, s):
    if s is Stream.empty:
        return s
    return Stream(fn(s.first), lambda: map_stream(fn, s.rest))

creates a new lambda function each time the tail of the list is expanded via the rest attribute, because it calls map_stream, which then executes lambda: map_stream(fn, s.rest). The value of a lambda expression is a new Python object, requiring space. But each function that is given as a second argument to Stream is only called once, and then thrown away (self._compute_rest = None). See if you can find a way to avoid creating a new function, and instead re-use the same function for each _compute_rest value. Obviously, the crux of the problem is to devise a way to get this (parameterless) function to return a different value each time.

def lst_to_stream(lst):
    """A utility for converting iterator or iterable LST into a corresponding
    Stream value.  (Use for testing only)"""
    if not lst:
        return Stream.empty
    return Stream(lst[0], lambda: lst_to_stream(lst[1:]))

def efficient_map_stream(fn, s):
    """Compute the Stream resulting from applying FN to each value of Stream
    S in turn.  [This implementation should only create a finite number of
    functions in total, regardless of the length of the stream S.]
    >>> str = lst_to_stream([7, -9, -3, 0])
    >>> ab = efficient_map_stream(abs, str)
    >>> stream_to_list(ab)
    [7, 9, 3, 0]
    >>> stream_to_list(efficient_map_stream(abs, Stream.empty))
    >>> stream_to_list(efficient_map_stream(abs, Stream(-3)))
    >>> stream_to_list(efficient_map_stream(lambda x: x*x, make_integer_stream(1)))
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q efficient_map_stream

Question 4: Stream of Streams

Write the function make_stream_of_streams, which returns an infinite stream, where the element at position i, counting from 1, is an infinite stream of integers that start from i. Your solution should have the form
result = Stream(..., lambda: ...)
return result

and should not require any additional helper functions (i.e., just use recursively defined streams, and any additional functions supplied in your starter file).

def make_stream_of_streams():
    >>> stream_of_streams = make_stream_of_streams()
    >>> stream_of_streams
    Stream(Stream(1, <...>), <...>)
    >>> stream_to_list(stream_of_streams, 3)
    [Stream(1, <...>), Stream(2, <...>), Stream(3, <...>)]
    >>> stream_to_list(stream_of_streams, 4)
    [Stream(1, <...>), Stream(2, <...>), Stream(3, <...>), Stream(4, <...>)]
    "*** YOUR CODE HERE ***"

Use OK to test your code:

python3 ok -q make_stream_of_streams

Question 5: Regular Numbers

Acknowledgements. This exercise is taken from Structure and Interpretation of Computer Programs, Section 3.5.2.

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.

  • s begins with 1 .
  • The elements of scale_stream(s, 2) are also elements of s.
  • The same is true for scale_stream(s, 3) and scale_stream(s, 5).
  • These are all of the elements of s.

Now all we have to do is combine elements from these sources. For this we define a merge function 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 strictly increasing s0 and s1,
    removing repeats. Assume that s0 and s1 have no repeats.

    >>> ints = make_integer_stream(1)
    >>> twos = scale_stream(ints, 2)
    >>> threes = scale_stream(ints, 3)
    >>> m = merge(twos, threes)
    >>> stream_to_list(m, 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()
    >>> stream_to_list(s, 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

Use OK to test your code:

python3 ok -q merge

Use OK to test your code:

python3 ok -q make_s