Lab07 题解 (UCB CS61A@2021 Fall)


Add a time_to_retire method to the Account class. This method takes in an amount and returns how many years the holder would need to wait in order for the current balance to grow to at least amount, assuming that the bank adds balance times the interest rate to the total balance at the end of every year.

问题描述如下: 每一年你的 balance 都会有利息, 问你哪一年你达到了 amount 可以退休了, 用代码模拟这个过程即可

Hw05 题解 (UCB CS61A@2021 Fall)

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 sequence seq and returns a generator that yields all permutations of seq. For this question, assume that seq will not be empty.

Lab06 题解 (UCB CS61A@2021 Fall)


Write a function which takes in a list lst, an argument entry, and another argument elem. This function will check through each item in lst to see if it is equal to entry. Upon finding an item equal to entry, the function should modify the list by placing elem into lst right after the item. At the end of the function, the modified list should be returned.

Hw04 题解 (UCB CS61A@2021 Fall)


Implement the planet data abstraction by completing the planet constructor and the size selector so that a planet is represented using a two-element list where the first element is the string 'planet' and the second element is its size.

从问题的描述中我们可以知道什么是 planet. 就是一个长度为 2 的 list, 内容是 ['planet', size]. 可以参考 mobile 函数, 这两个函数的代码是很类似的

Lab05 题解 (UCB CS61A@2021 Fall)


Write factors_list, which takes a number n and returns a list of its factors in ascending order.

我们可以知道这么一个基本的数学事实: 一个数字的因子最大仅可能为它的一半(当这个数字是偶数的时候). 所以我们的 for 循环只要遍历到 n // 2 + 1 即可