Recursion and Tree Recursion
Q1: Subsequences
A subsequence of a sequence
S
is a subset of elements fromS
, in the same order they appear inS
. 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.
這一道題要求我們返回一個列表的所有可能子序列, 返回的格式是列表的列表, 每一個都是可能的子序列
題目要求我們首先完成一個函數: 功能是把 item
添加到嵌套列表的每個子列表的開頭, 這個其實用 list comprehension 就可以了.
def insert_into_all(item, nested_list):
"""Return a new list consisting of all the lists in nested_list,
but with item added to the front of each. You can assume that
nested_list is a list of lists.
"""
return [[item] + l for l in nested_list]
這其實是題目給我們的提示, 我們現在思考有了這個函數我們要怎么找到所有可能的子序列呢? 我們可以遞歸分解問題: 當前元素 + 剩下元素的所有可能子序列(假設是 tmp
). 那么我們只要把當前元素加到 tmp
中的每個列表再加上 tmp
即可. 這剛好就用上了題目讓我們實現的 insert_into_all()
函數. 那么最后我們要思考什么對應這個 base case. 顯然如果一個空的列表的子序列為空列表, 如果列表長度為 1, 則存在兩個可能子序列 - 它自己 + 空列表. 最后我們就可以寫出這樣的代碼
def subseqs(s):
"""Return a nested list (a list of lists) of all subsequences of S.
The subsequences can appear in any order. You can assume S is a list.
"""
if len(s) <= 1:
return [[], s] if s !=[] else [[]]
else:
tmp = subseqs(s[1:])
return insert_into_all(s[0], tmp) + tmp
Q2: 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 anitem
and a list of lists and inserts theitem
to the front of each list.
這一道題是在 Q1 的基礎上改編而來的, 相當於提出了一個更高的要求, 我們要求子序列同時是非降序的.
這一題的提示告訴我們要實現一個 subseq_helper
函數, 這其實很好理解, 因為我們現在對子序列的大小順序有要求, 那么我們就要多一個參數用來比較大小, 這樣我們在插入 item 到列表的時候才知道可不可以插入(維持非降序). 比如 [1, 3, 2]
, 當我們在檢查 3
的時候我們發現 3
< 1
, 顯然我們不能把 3
插入到之前生成的子序列列表里. 而其他情況我們則可以選擇把 s[0]
加到子序列列表中或者不加.
def non_decrease_subseqs(s):
"""Assuming that S is a list, return a nested list of all subsequences
of S (a list of lists) for which the elements of the subsequence
are strictly nondecreasing. The subsequences can appear in any order.
"""
def subseq_helper(s, prev):
if not s:
return [[]]
elif s[0] < prev:
return subseq_helper(s[1:], prev)
else:
a = subseq_helper(s[1:], s[0]) # include s[0]
b = subseq_helper(s[1:], prev) # exclude s[0]
return insert_into_all(s[0], a) + b
return subseq_helper(s, 0)
Q3: Number of Trees
A full binary tree is a tree where each node has either 2 branches or 0 branches, but never 1 branch.
Write a function which returns the number of unique full binary tree structures that have exactly n leaves.
For those interested in combinatorics, this problem does have a closed form solution):
題意: 有 n
個葉子結點的完全二叉樹可能有幾種 ? 答案是卡特蘭數, 所以我們要實現的其實是卡特蘭數的遞歸寫法. 至於為什么是卡特蘭數我也想不大明白, 比較能接受的解釋是, 完全二叉樹的左右子樹肯定也是完全二叉樹, 假設左子樹有 1
個葉子結點, 右子樹就有 n - 1
個葉子結點, 那么此時就有 f(1) * f(n - 1)
種可能, 類似的, 如果左子樹有 2
個葉子結點, 那就是 f(2) * f(n - 2)
, 這樣累加起來就是卡特蘭數.
ps: 這里的完全二叉樹不是嚴格意義上的, 確切來說這里指的是所有節點的度只能為 0 或者 2 的樹
def num_trees(n):
"""Returns the number of unique full binary trees with exactly n leaves. E.g.,
"""
if n == 1 or n == 2:
return 1
# catalan number
ans = 0
for i in range(1, n):
ans += num_trees(i) * num_trees(n - i)
return ans
Generators
Q4: Merge
Implement
merge(incr_a, incr_b)
, which takes two iterablesincr_a
andincr_b
whose elements are ordered.merge
yields elements fromincr_a
andincr_b
in sorted order, eliminating repetition. You may assumeincr_a
andincr_b
themselves do not contain repeats, and that none of the elements of either areNone
. You may notassume 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 asnext(incr)
, except that instead of raisingStopIteration
whenincr
runs out of elements, it returnsv
.See the doctest for examples of behavior.
merge
函數的功能是合並兩個有序的可迭代對象, 同時要做去重的工作, 可以假設兩個有序的可迭代對象本身是沒有元素重復的, 而且沒有任何一個元素是 None. 同時不可以假定這兩個可迭代對象是有限序列, 它們可能無序的(這樣你就不能暴力合並為一個有序可迭代對象再去重)
因為兩個可迭代對象本身不包含重復元素, 所以這一道題處理起來比較簡單, 我們只要重復下面的過程:
- 如果兩個可迭代對象都是非空
- 各取一個元素進行比較
- 如果一樣大: 返回一個, 同時兩個 iterator 都要往后移動
- 其中一個比較小: 返回小的這個, 移動小的這個可迭代對象的 iterator, 大的元素的 iterator 不動
- 各取一個元素進行比較
- 如果重復上面的操作導致其中一個已經空了, 那么接下來的問題就比較簡單了, 此時我們只要用
while
循環不斷從某一個可迭代對象中返回元素即可.
代碼如下:
def merge(incr_a, incr_b):
"""Yield the elements of strictly increasing iterables incr_a and incr_b, removing
repeats. Assume that incr_a and incr_b have no repeats. incr_a or incr_b may or may not
be infinite sequences.
"""
iter_a, iter_b = iter(incr_a), iter(incr_b)
next_a, next_b = next(iter_a, None), next(iter_b, None)
# both are non-empty
while next_a is not None and next_b is not None:
val_a, val_b = next_a, next_b
if val_a == val_b:
yield next_a
next_a, next_b = next(iter_a, None), next(iter_b, None)
elif val_a < val_b:
yield next_a
next_a = next(iter_a, None)
else:
yield next_b
next_b = next(iter_b, None)
# incr_a is not empty
while next_a:
yield next_a
next_a = next(iter_a, None)
# incr_b is not empty
while next_b:
yield next_b
next_b = next(iter_b, None)
Objects
Q5: Bank Account
Implement the class
Account
, which acts as a a Bank Account.Account
should allow the account holder to deposit money into the account, withdraw money from the account, and view their transaction history. The Bank Account should also prevents a user from withdrawing more than the current balance.Transaction history should be stored as a list of tuples, where each tuple contains the type of transaction and the transaction amount. For example a withdrawal of 500 should be stored as ('withdraw', 500)
Hint: You can call the
str
function on an integer to get a string representation of the integer. You might find this function useful when implementing the__repr__
and__str__
methods.Hint: You can alternatively use fstrings to implement the
__repr__
and__str__
methods cleanly.
實現一個 Account
類, 要求有以下功能:
- 存款
- 取款, 錢不夠的時候不讓取
- 查看操作歷史. 轉賬歷史是 tuple 的列表, 每個 tuple 包括了操作的類型和轉賬的金額
整體上而言這題不難, 看 __repr__
我們可以知道要求返回存款和取款的次數, 這里可以用兩個變量來記住.
class Account:
"""A bank account that allows deposits and withdrawals.
It tracks the current account balance and a transaction
history of deposits and withdrawals.
"""
interest = 0.02
def __init__(self, account_holder):
self.balance = 0
self.holder = account_holder
self.transactions = []
self.withdraw_cnt = 0
self.deposit_cnt = 0
def deposit(self, amount):
"""Increase the account balance by amount, add the deposit
to the transaction history, and return the new balance.
"""
self.balance += amount
self.transactions.append(('deposit', amount))
self.deposit_cnt += 1
return self.balance
def withdraw(self, amount):
"""Decrease the account balance by amount, add the withdraw
to the transaction history, and return the new balance.
"""
if self.balance > amount:
self.balance -= amount
self.transactions.append(('withdraw', amount))
self.withdraw_cnt += 1
return self.balance
# prevent illegal withdraw
return self.balance
def __str__(self):
return f"{self.holder}'s Balance: ${self.balance}"
def __repr__(self):
return f"Accountholder: {self.holder}, Deposits: {self.deposit_cnt}, Withdraws: {self.withdraw_cnt}"
Mutable Lists
Q6: Trade
In the integer market, each participant has a list of positive integers to trade. When two participants meet, they trade the smallest non-empty prefix of their list of integers. A prefix is a slice that starts at index 0.
Write a function
trade
that exchanges the firstm
elements of listfirst
with the firstn
elements of listsecond
, such that the sums of those elements are equal, and the sum is as small as possible. If no such prefix exists, return the string'No deal!'
and do not change either list. Otherwise change both lists and return'Deal!'
. A partial implementation is provided.Hint: You can mutate a slice of a list using slice assignment. To do so, specify a slice of the list
[i:j]
on the left-hand side of an assignment statement and another list on the right-hand side of the assignment statement. The operation will replace the entire given slice of the list fromi
inclusive toj
exclusive with the elements from the given list. The slice and the given list need not be the same length.>>> a = [1, 2, 3, 4, 5, 6] >>> b = a >>> a[2:5] = [10, 11, 12, 13] >>> a [1, 2, 10, 11, 12, 13, 6] >>> b [1, 2, 10, 11, 12, 13, 6]
Additionally, recall that the starting and ending indices for a slice can be left out and Python will use a default value.
lst[i:]
is the same aslst[i:len(lst)]
, andlst[:j]
is the same aslst[0:j]
.
題意: 交換兩個列表的開頭幾個元素(m
和 n
可以不等長), 使得兩邊被用來交換的子列表的和(前綴和)是一樣的, 而且這個和要越小越好.
在代碼里已經為我們提供了交換元素的函數, 我們要做的就是讓 m
和 n
停在正確的位置(他們的和一樣), 這里用 while
循環來實現, 只要兩個的索引是有效的(不然他們會一直增加, while
循環就會變為死循環)而且前綴和不想等, 我們移動 m
或者 n
指針.
def trade(first, second):
"""Exchange the smallest prefixes of first and second that have equal sum.
"""
m, n = 1, 1
equal_prefix = lambda: sum(first[:m]) == sum(second[:n])
while m <= len(first) and n <= len(second) and not equal_prefix():
if sum(first[:m]) < sum(second[:n]):
m += 1
else:
n += 1
if equal_prefix():
first[:m], second[:n] = second[:n], first[:m]
return 'Deal!'
else:
return 'No deal!'
Q7: 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
ands1
is to create a new sequence such that the new sequence contains (in this order) the first element ofs0
, the first element ofs1
, the second element ofs0
, the second element ofs1
, 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.
這一道題就是要我們完成洗牌的功能, 洗牌的意思是前一半和后一半的元素交替出現, 舉例來說:[0, 1, 2, 3, 4, 5] = [0, 3, 1, 4, 2, 5]
. 你可以看到奇數索引的是后一半的元素, 偶數索引的是前一半元素.
這一道題的關鍵在於弄清楚洗牌之后的索引和原來的索引對應的關系, 總結來來說:[0, 1, ..., len(cards) // 2, len(cards) // 2 + 1, ...]
. 你可以發現前一半和后一半對應位置的元素的索引相差 len(cards) // 2
def shuffle(cards):
"""Return a shuffled list that interleaves the two halves of cards.
"""
assert len(cards) % 2 == 0, 'len(cards) must be even'
half = len(cards) // 2
shuffled = []
for i in range(half):
shuffled.append(cards[i])
shuffled.append(cards[i + half])
return shuffled
Linked Lists
Q8: Insert
Implement a function
insert
that takes aLink
, avalue
, and anindex
, and inserts thevalue
into theLink
at the givenindex
. You can assume the linked list already has at least one element. Do not return anything --insert
should mutate the linked list.Note: If the index is out of bounds, you should raise an
IndexError
with:raise IndexError('Out of bounds!')
根據指定的索引 index
在鏈表中插入元素, 如果索引非法, 拋出錯誤
這一題有點奇怪的地方在於, 它要求我們在原來的鏈表上進行修改, 但是如果我們要在鏈表的開頭進行插入一個新節點, 會無法通過它的 link is other_link
的判斷(因為插入后鏈表頭是一個新的節點), 所以我這里想的辦法是每次在插入前我們拷貝當前結點, 然后修改當前結點的值為想要插入的值, 這樣等效於我們做了插入
def insert(link, value, index):
"""Insert a value into a Link at the given index.
"""
pos = link
current_index = 0
while pos is not Link.empty:
if current_index == index:
# make a copy of current node, and modify the current node's value \
# which is equal to insert a new node :)
current_copy = Link(pos.first, pos.rest)
origin_next = pos.rest
pos.first = value
pos.rest = current_copy
#print(f"link: {link.first}")
return
pos = pos.rest
current_index += 1
raise IndexError('Out of bounds!')
Q9: 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.
Deep Linked List Length 其實就是一個可能包含鏈表為結點的嵌套鏈表結構. 這一道題要求我們算這種嵌套列表一共有多少個元素. 其實就是之前做的攤平鏈表的那種題目. 顯然, 這是符合遞歸的嵌套結構, 所以我們可以用遞歸的辦法解決.
base case 就是空鏈表或者它是一個元素而不是鏈表. 其他情況我們就遞歸處理鏈表的第一個節點和除了第一個結點以外的子鏈表 🤗
def deep_len(lnk):
""" Returns the deep length of a possibly deep linked list.
"""
# base case 1. an empty node
if lnk is Link.empty:
return 0
# base case 2. an integer
elif isinstance(lnk, int):
return 1
else:
return deep_len(lnk.first) + deep_len(lnk.rest)
Q10: Linked Lists as Strings
Kevin and Jerry like different ways of displaying the linked list structure in Python. While Kevin likes box and pointer diagrams, Jerry prefers a more futuristic way. Write a function
make_to_string
that returns a function that converts the linked list to a string in their preferred style.Hint: You can convert numbers to strings using the
str
function, and you can combine strings together using+
.>>> str(4) '4' >>> 'cs ' + str(61) + 'a' 'cs 61a'
簡單來說就是想要根據不同人的需求來打印鏈表, 具體格式就是 front + 當前結點的值 + mid + 子鏈表的 + back
這樣
def make_to_string(front, mid, back, empty_repr):
""" Returns a function that turns linked lists to strings.
"""
def printer(lnk):
if lnk is Link.empty:
return empty_repr
else:
return front + str(lnk.first) + mid + printer(lnk.rest) + back
return printer
Trees
Q11: Long Paths
Implement
long_paths
, which returns a list of all paths in a tree with length at leastn
. A path in a tree is a list of node labels that starts with the root and ends at a leaf. Each subsequent element must be from a label of a branch 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 ordered in the output list from left to right in the tree. See the doctests for some examples.
返回一個嵌套列表, 每個子列表表示長度至少為 n
的路徑. 這里說的路徑一定是葉子結點的路徑 ! 路徑的長度可以理解為從根結點出發到達葉子結點經過的邊數.
這一道題其實是經典的遞歸與回溯問題, 我們要為其寫一個 helper
函數, 要記住我們當前經過的點的路徑, 以及路徑的長度. 遞歸與回溯的模板大概如下:
def function_name(p):
# base case
...
dothing thing
... # recursively solve this problem
recall what you have done
放到我們這里就是我們要在往更深層遞歸的時候加上當前節點的 label, 當我們回溯的時候撤銷我們對之前的添加. 代碼如下:
def long_paths(t, n):
"""Return a list of all paths in t with length at least n.
"""
path_list = []
def helper(t, current_path, length):
nonlocal path_list
if t.is_leaf():
current_path.append(t.label)
if length >= n:
# warning: we need to pass a copy instead fo a ref
path_list.append(current_path[:])
current_path.pop()
return
current_path.append(t.label)
for b in t.branches:
helper(b, current_path, length + 1)
current_path.pop()
helper(t, [], 0)
return path_list