CS61A Homework 5
Homework 5: Trees, Linked Lists hw05.zip
Mid-Semester Feedback
Q1: Mid-Semester Feedback
As part of this week’s homework, please fill out the Mid-Semester Feedback form.
This survey is designed to help us make short term adjustments to the course so that it works better for you. We appreciate your feedback. We may not be able to make every change that you request, but we will read all the feedback and consider it.
Confidentiality: Your responses to the survey are confidential, and only the instructor (Pamela) and head TA (Vanshaj) will be able to see this data unanonymized. More specifics on confidentiality can be found on the survey itself.
Once you finish the survey, you will be presented with a passphrase (if you miss it, it should also be at the bottom of the confirmation email you receive). Put this passphrase, as a string, on the line that says passphrase = '*** PASSPHRASE HERE ***'
in the Python file for this assignment.
Use Ok to test your code:
1 | python3 ok -q midsem_survey✂️ |
Parsons Problems
Q2: Chain
For this question, we will define a chain as a path from the root of a tree t
to any leaf such that all nodes on the path share the same label
. Implement the function chain
, which, given a tree t
, returns True
if there exists any chain
in the tree, and False
otherwise.
1 | def chain(t): |
Q3: Flatten Link
Write a function flatten_link
that takes in a linked list lnk
and returns the sequence as a Python list. If lnk
has nested linked lists, flatten_link
should flatten lnk
.
1 | def flatten_link(lnk): |
Code Writing Questions
Q4: Has Path
Write a function has_path
that takes in a Tree t
and a string term
. It returns True
if there is a path that starts from the root where the entries along the path spell out the term
, and False
otherwise. You may assume that every node’s label
is exactly one character.
This data structure is called a trie, and it has a lot of cool applications, such as autocomplete.
1 | def has_path(t, term): |
Use Ok to test your code:
1 | python3 ok -q has_path✂️ |
Q5: Duplicate Link
Write a function duplicate_link
that takes in a linked list lnk
and a value
. duplicate_link
will mutate lnk
such that if there is a linked list node that has a first
equal to value
, that node will be duplicated. Note that you should be mutating the original link list lnk
; you will need to create new Link
s, but you should not be returning a new linked list.
Note: in order to insert a link into a linked list, you need to modify the
.rest
of certain links. We encourage you to draw out a doctest to visualize!
1 | def duplicate_link(lnk, val): |
Use Ok to test your code:
1 | python3 ok -q duplicate_link✂️ |
Q6: Mutable Mapping
Implement deep_map_mut(fn, link)
, which applies a function fn
onto all elements in the given linked list lnk
. If an element is itself a linked list, apply fn
to each of its elements, and so on.
Your implementation should mutate the original linked list. Do not create any new linked lists.
Hint: The built-in
isinstance
function may be useful.
1
2
3
4
5 >>> s = Link(1, Link(2, Link(3, Link(4))))
>>> isinstance(s, Link)
True
>>> isinstance(s, int)
False
Construct Check: The last doctest of this question ensures that you do not create new linked lists. If you are failing this doctest, ensure that you are not creating link lists by calling the constructor, i.e.
1 s = Link(1)
1 | def deep_map_mut(fn, lnk): |
Use Ok to test your code:
1 | python3 ok -q deep_map_mut✂️ |
Submit
Make sure to submit this assignment by running:
1 | python3 ok --submit |
Optional Questions
Homework assignments will also contain prior exam-level questions for you to take a look at. These questions have no submission component; feel free to attempt them if you’d like a challenge!