# CS61A Lab 8

# Lab 8: Midterm Review lab08.zip

*Due by 11:59pm on Wednesday, March 16.*

## Starter Files

Download lab08.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the Ok autograder.

# Topics

Consult this section if you need a refresher on the material for this lab. It’s okay to skip directly to the questions and refer back here should you get stuck.

Iterators

Generators

# Required Questions

## Iterators & Generators

### Q1: Repeated

Implement `repeated`

, which takes in an iterator `t`

and returns the first value in `t`

that appears `k`

times in a row.

Note:You can assume that the iterator`t`

will have a value that appears at least`k`

times in a row. If you are receiving a`StopIteration`

, your`repeated`

function is likely not identifying the correct value.

Your implementation should iterate through the items in a way such that if the same iterator is passed into `repeated`

twice, it should continue in the second call at the point it left off in the first. An example of this behavior is in the doctests.

1 | def repeated(t, k): |

Use Ok to test your code:

1 | python3 ok -q repeated✂️ |

### Q2: Merge

Implement `merge(incr_a, incr_b)`

, which takes two iterables `incr_a`

and `incr_b`

whose elements are ordered. `merge`

yields elements from `incr_a`

and `incr_b`

in sorted order, eliminating repetition. You may assume `incr_a`

and `incr_b`

themselves do not contain repeats, and that none of the elements of either are `None`

. You may **not** assume that the iterables are finite; either may produce an infinite stream of results.

You will probably find it helpful to use the two-argument version of the built-in `next`

function: `next(incr, v)`

is the same as `next(incr)`

, except that instead of raising `StopIteration`

when `incr`

runs out of elements, it returns `v`

.

See the doctest for examples of behavior.

1 | def merge(incr_a, incr_b): |

Use Ok to test your code:

1 | python3 ok -q merge✂️ |

## Linked Lists & Trees

### Q3: Deep Linked List Length

A linked list that contains one or more linked lists as elements is called a *deep* linked list. Write a function `deep_len`

that takes in a (possibly deep) linked list and returns the *deep length* of that linked list. The deep length of a linked list is the total number of non-link elements in the list, as well as the total number of elements contained in all contained lists. See the function’s doctests for examples of the deep length of linked lists.

Hint:Use`isinstance`

to check if something is an instance of an object.

1 | def deep_len(lnk): |

Use Ok to test your code:

1 | python3 ok -q deep_len✂️ |

### Q4: Add Leaves

Implement `add_d_leaves`

, a function that takes in a `Tree`

instance `t`

and a number `v`

.

We define the depth of a node in `t`

to be the number of edges from the root to that node. The depth of root is therefore 0.

For each node in the tree, you should add `d`

leaves to it, where `d`

is the depth of the node. Every added leaf should have a label of `v`

. If the node at this depth has existing branches, you should add these leaves to the end of that list of branches.

For example, you should be adding 1 leaf with label `v`

to each node at depth 1, 2 leaves to each node at depth 2, and so on.

Here is an example of a tree `t`

(shown on the left) and the result after `add_d_leaves`

is applied with `v`

as 5.

Try drawing out the second doctest to visualize how the function is mutating

`t3`

.

Hint:Use a helper function to keep track of the depth!

1 | def add_d_leaves(t, v): |

Use Ok to test your code:

1 | python3 ok -q add_d_leaves✂️ |

## Efficiency

### Q5: Efficiency Practice

Choose the term that fills in the blank for the functions defined below: `<function>`

runs in `____`

time in the length of its input.

- Constant
- Logarithmic
- Linear
- Quadratic
- Exponential
- None of these

Assume that `len`

runs in constant time and `all`

runs in linear time in the length of its input. Selecting an element of a list by its index requires constant time. Constructing a range requires constant time.

1 | def count_partitions(n, m): |

The

`is_palindrome`

question was reformatted from question 6(d) on fall 2019’s final.

Use Ok to test your understanding:

1 | python3 ok -q efficiency_practice -u |

## Submit

Make sure to submit this assignment by running:

1 | python3 ok --submit |

# Extra Practice

## Recursion and Tree Recursion

### Q6: Subsequences

A subsequence of a sequence `S`

is a subset of elements from `S`

, in the same order they appear in `S`

. Consider the list `[1, 2, 3]`

. Here are a few of it’s subsequences `[]`

, `[1, 3]`

, `[2]`

, and `[1, 2, 3]`

.

Write a function that takes in a list and returns all possible subsequences of that list. The subsequences should be returned as a list of lists, where each nested list is a subsequence of the original input.

In order to accomplish this, you might first want to write a function `insert_into_all`

that takes an item and a list of lists, adds the item to the beginning of each nested list, and returns the resulting list.

1 | def insert_into_all(item, nested_list): |

Use Ok to test your code:

1 | python3 ok -q subseqs✂️ |

### Q7: Non-Decreasing Subsequences

Just like the last question, we want to write a function that takes a list and returns a list of lists, where each individual list is a subsequence of the original input.

This time we have another condition: we only want the subsequences for which consecutive elements are *nondecreasing*. For example, `[1, 3, 2]`

is a subsequence of `[1, 3, 2, 4]`

, but since 2 < 3, this subsequence would *not* be included in our result.

**Fill in the blanks** to complete the implementation of the `non_decrease_subseqs`

function. You may assume that the input list contains no negative elements.

You may use the provided helper function `insert_into_all`

, which takes in an `item`

and a list of lists and inserts the `item`

to the front of each list.

1 | def non_decrease_subseqs(s): |

Use Ok to test your code:

1 | python3 ok -q non_decrease_subseqs✂️ |

## Mutable Lists

### Q8: Shuffle

Define a function `shuffle`

that takes a sequence with an even number of elements (cards) and creates a new list that interleaves the elements of the first half with the elements of the second half.

To interleave two sequences `s0`

and `s1`

is to create a new sequence such that the new sequence contains (in this order) the first element of `s0`

, the first element of `s1`

, the second element of `s0`

, the second element of `s1`

, and so on. If the two lists are not the same length, then the leftover elements of the longer list should still appear at the end.

Note:If you’re running into an issue where the special heart / diamond / spades / clubs symbols are erroring in the doctests, feel free to copy paste the below doctests into your file as these don’t use the special characters and should not give an “illegal multibyte sequence” error.

1 | def card(n): |

Use Ok to test your code:

1 | python3 ok -q shuffle✂️ |

## Generators & Iterators

### Q9: Pairs (generator)

Write a generator function `pairs`

that takes a list and yields all the possible pairs of elements from that list.

1 | def pairs(lst): |

Use Ok to test your code:

1 | python3 ok -q pairs✂️ |

### Q10: Pairs (iterator)

Important note (March 14):This question isnotin scope for this semester (Spring 2022), as it involves`__iter__`

and`__next__`

.

Now write an iterator that does the same thing. You are only allowed to use a linear amount of space - so computing a list of all of the possible pairs is not a valid answer. Notice how much harder it is - this is why generators are useful.

1 | class PairsIterator: |

Use Ok to test your code:

1 | python3 ok -q PairsIterator✂️ |

## Trees

### Q11: Long Paths

Implement `long_paths`

, which returns a list of all *paths* in a tree with length at least `n`

. A path in a tree is a linked list of node values that starts with the root and ends at a leaf. Each subsequent element must be from a child of the previous value’s node. The *length* of a path is the number of edges in the path (i.e. one less than the number of nodes in the path). Paths are listed in order from left to right. See the doctests for some examples.

1 | def long_paths(tree, n): |

Use Ok to test your code:

1 | python3 ok -q long_paths✂️ |

## Linked Lists

### Q12: Flip Two

Write a recursive function `flip_two`

that takes as input a linked list `s`

and mutates `s`

so that every pair of values in the linked list is flipped.

1 | def flip_two(s): |

Use Ok to test your code:

1 | python3 ok -q flip_two✂️ |