Lab10 题解 (UCB CS61A@2021 Fall)
Q2: Over or Under
Define a procedure
over-or-underwhich takes in a numbernum1and a numbernum2and returns the following:
- -1 if
num1is less thannum2- 0 if
num1is equal tonum2- 1 if
num1is greater thannum2Challenge: Implement this in 2 different ways using
ifandcond!Lab09 题解 (UCB CS61A@2021-Fall)
Recursion and Tree Recursion
Q1: Subsequences
A subsequence of a sequence
Sis 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.
Lab08 题解 (UCB CS61A@2021 Fall)
Q2: Convert Link
Write a function
convert_linkthat takes in a linked list and returns the sequence as a Python list. You may assume that the input list is shallow; that is none of the elements is another linked list.Try to find both an iterative and recursive solution for this problem!
迭代的算法很简单, 我们只要创建一个
resultlist 来存储结果, 遍历链表的同时记住访问过的数即可Lab07 题解 (UCB CS61A@2021 Fall)
Accounts
Q2: Retirement
Add a
time_to_retiremethod to theAccountclass. This method takes in anamountand returns how many years the holder would need to wait in order for the currentbalanceto grow to at leastamount, assuming that the bank addsbalancetimes theinterestrate to the total balance at the end of every year.问题描述如下: 每一年你的
balance都会有利息, 问你哪一年你达到了amount可以退休了, 用代码模拟这个过程即可Hw05 题解 (UCB CS61A@2021 Fall)
Q1: Generate Permutations
Given a sequence of unique elements, a permutation of the sequence is a list containing the elements of the sequence in some arbitrary order. For example,
[2, 1, 3],[1, 3, 2], and[3, 2, 1]are some of the permutations of the sequence[1, 2, 3].Implement
gen_perms, a generator function that takes in a sequenceseqand returns a generator that yields all permutations ofseq. For this question, assume thatseqwill not be empty.Lab06 题解 (UCB CS61A@2021 Fall)
Mutability
Write a function which takes in a list
lst, an argumententry, and another argumentelem. This function will check through each item inlstto see if it is equal toentry. Upon finding an item equal toentry, the function should modify the list by placingelemintolstright after the item. At the end of the function, the modified list should be returned.See the doctests for examples on how this function is utilized.