Lab10 solutions (UCB CS61A@2021 Fall)
Coding Questions
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 solutions (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 solutions (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!
It is easy to solve this problem iteratively. All we have to do is to make a
listto store these nodes we have visited while we iterating this linklist.Lab07 solutions (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.The description tells us that: We will add our
balanceevery year, so when may we retire? Both the math and code are simple.Hw05 solutions (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 solutions (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.
Hw04 solutions (UCB CS61A@2021 Fall)
Mobiles
Q2: Weights
Implement the
planetdata abstraction by completing theplanetconstructor and thesizeselector 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.From the description, we can know what is
planet. It is a['planet', size]. Then we can take a look at themobilefunction, the solution is quite similar.Lab05 solutions (UCB CS61A@2021 Fall)
Lists
Q1: Factors List
Write
factors_list, which takes a numbernand returns a list of its factors in ascending order.We can know such a basic mathematical fact: the factor of a number can only be up to half of it (when the number is even). So we use
for i in range(1, n // 2 + 1)Lab04 solutions (UCB CS61A@2021 Fall)
Intro
I find it really interesting to solve recursive problems. I like this way of solving problems, the code is concise and intuitive, which is why I wrote this blog.
📒 How to solve a recursive problem ?
- What is the base case ?
- How to break down the current problem into simpler ones ?
Later, I will follow this idea to solve the recursion problem in this lab.
Recursion
Q2: Summation
Write a recursive implementation of
summation, which takes a positive integernand a functionterm. It appliestermto every number from1tonincludingnand returns the sum.Hw03 of CS61A of UCB(2021-Fall)
hw03. Recursion, Tree Recursion
Q1: Num eights
Write a recursive function
num_eightsthat takes a positive integerposand returns the number of times the digit 8 appears inpos.Important: Use recursion; the tests will fail if you use any assignment statements. (You can however use function definitions if you so wish.)
It is easy to think of the answer to this question, for we have seen a similar one in lecture. We can split the
posintoall_bust_lastandlast. When we arrive at the base case, we just need to check if it is equals to 8. Because we can not use=in this problem, we need to use function instead.How to Manage Windows Using Hammerspoon
Intro
I found that the windows management built in macOS is difficult to use. As a result, I always using my mouse to move and resize my window, which is less efficient. We should keep our hands on the keyboard as much as possible. After finishing the MIT-Missing-Semester, I came across the hammerspoon tool. I really like this one :)
What is hammerspoon ?
According to the official site’s introduction, hammerspoon is a tool for powerful automation of OS X, which is just a bridge between the operating system and a Lua scripting engine. The windows management is kind of automation.
Hw02 of CS61A of UCB(2021-Fall)
hw02. Higher Order Functions
Q1: Product
The
summation(n, term)function from the higher-order functions lecture adds upterm(1) + ... + term(n). Write a similar function calledproductthat returnsterm(1) * ... * term(n).This problem is quite easy, we just need to use
*instead of+. The logic is similar tosummation(n, term)function in the lecture.Remember to set
ans = 1at first, after all,0* any numbers is always equal to 0 🤗