程序代写代做代考 C CS 61A Structure and Interpretation of Computer Programs Fall 2020 Quiz 3

CS 61A Structure and Interpretation of Computer Programs Fall 2020 Quiz 3
INSTRUCTIONS
• Please review this worksheet before the exam prep session. Coming prepared will help greatly, as the TA will be live solving without allocating much time for individual work.
• Either Sean or Derek will be on video live solving these questions. The other TA will be answering questions in the chat. It is in your best interest to come prepared with specific questions.
• This is not graded, and you do not need to turn this in to anyone.
• Fall 2020 students: the boxes below are an artifact from more typical semesters to simulate exam environments. Obviously this doesn’t apply to this semester’s exams, but we just kept the fields to keep our materials looking professional 🙂 Feel free to ignore them.
• For multiple choice questions, fill in each option or choice completely.
– 2 means mark all options that apply – # means mark a single choice
Last name
First name
Student ID number
CalCentral email (_@berkeley.edu)
Discussion Section
All the work on this exam is my own.
(please sign)

2
1. Warm-Up
(a) A palindrome is a string that remains identical when reversed. Given a string s, return whether or not it is a palindrome.
def is_palindrome(s):

>>> is_palindrome(tenet)
True
>>> is_palindrome(tenets)
False
B
SCI lancs
if _______________________________________________________________________________:
>>> is_palindrome()
True
>>> is_palindrome(a)
True
>>> is_palindrome(ab)
False
lerG
cI return True
sfo SEAandis i 63B
When without
areyouloo it’sapalindrome havingtodoanymorework
SU B
BASECn.se
Sleen
return __________________
__
___
______________p__a_l_in__d_r_o_m__e___________________________

2. Great Pals
(a) A substring of s is a sequence of consecutive letters within s. Given a string s, return the longest palindromic substring of s. If there are multiple palindromic substrings of greatest length, then return the leftmost one. You may use is_palindrome.
def greatest_pal(s):

>>> greatest_pal(tenet)
tenet
>>> greatest_pal(tenets)
tenet
>>> greatest_pal(stennet)
tennet
>>> greatest_pal(abc)
a
E a startsat 0
bi startsat
>>> greatest_pal()
jengthstart
whilelength while start
lenk length b
gardens bta
index
sofar
if ________________ > ________________________________________________________:
whaotff’Ygesffenpaundrone c
braLen
return ___________________________________________________________________
def helper(a, b, c):
eenG
elif ________________________ > _____________________________________________:
a
c
bta denG
return ____________a__t_t____________________________________________________
helper O c
i s p a l i n d r o m Ge l e b b d A L e n c
elif ___________________ and _________________________________________________:
__________________________________________________________________________
s bbta
return _______________________________________________________________________
w
c
www
helpeda btl return helper(1, 0, )
c
startsat hen s
string
3

4
3. Take Two
(a) A substring of s is a sequence of consecutive letters within s. Given a string s, return the longest palindromic substring of s. If there are multiple palindromic substrings of greatest length, then return the leftmost one. You may use is_palindrome.
def greatest_pal_simpler(s):

>>> greatest_pal_simpler(tenet)
tenet
>>> greatest_pal_simpler(tenets)
tenet
>>> greatest_pal_simpler(stennet)
tennet
>>> greatest_pal_simpler(abc)
a
>>> greatest_pal_simpler()


def helper(a, b):
cck
on
if ___________________________________________________________________________:
a
return ___________________________________________________________________
elif _________________________________________________________________________:
atbseenG
Cal O helper
return ___________________________________________________________________
elif _____________________________b____________________________________________:
is palindrome s
b ta
S bbta
return ___________________________________________________________________
CabtD helper
return _______________________________________________________________________
return ___________________________O________________________________________________ Clerks
helper

4. Wait, it’s all palindromes?
(a) Given a string s, return the longest palindromic substring of s. If there are multiple palindromes of
greatest length, then return the leftmost one. You may not use is_palindrome.
def greatest_pal_two(s):

>>> greatest_pal_two(tenet)
tenet
>>> greatest_pal_two(tenets)
tenet
>>> greatest_pal_two(stennet)
tennet
>>> greatest_pal_two(abc)
a
>>> greatest_pal_two()

if _____________L_______________________________________________________: Len I
i
tents112
5
return ____________________________________________________________
for __________ in _____________________________________________________:
range
if ____________________C__s__________________________________________:
Sli_seen il
return ________________________________________________________
Len
return s
a max
1
key
2
she
if
Max greatespt altwoSGD greatespt altwoSL B
2
LaxC 2,1 key 2
lambda x x
5

6
5. All-Ys Has Been
(a) Given mystery function Y, complete fib and is_pal so that the given doctests work correctly. When Y is called on fib, it should return a function which takes a positive integer n and returns the nth Fibonacci number. is_pal should take a string and return whether it is a palindrome.
Hint: you may use the ternary operator if else , which evaluates to if is true and evaluates to if is false.
Y = lambda f: (lambda x: x(x))(lambda x: f(lambda z: x(x)(z)))
fib_maker = lambda f: lambda r: _______________________________________________________
is_pal_maker = lambda f: lambda r: ___________________________________________________
fib = Y(fib_maker)
is_pal = Y(is_pal_maker)
assert fib(0) == 0
assert fib(1) == 1
assert fib(2) == 1
assert fib(3) == 2
assert fib(4) == 3
assert fib(5) == 5
assert is_pal(tenet)
assert not is_pal(tenets)
assert not is_pal(ab)
assert is_pal()
assert is_pal(a)

6. Longest Palindromic Subsequence
(a) A subsequence of a string is another string which contains possibly non-consecutive characters of the string in the same order. For example, “abcd”, “acd” and “bd” are all subsequences of “abcd” (although there are others).
Given a string s, return the longest palindromic subsequence of s. If there are multiple options for palindromic subsequences, you may return any; the doctests follow the rule that all chosen letters are as far left as possible. You may not use functions from any other problem.
def gp_subseq(s):

>>> gp_subseq()

>>> gp_subseq(abc)
a
>>> gp_subseq(admrdak) # adrda is also acceptable
admda

if ___________________________________________________________________________:
return ___________________________________________________________________
elif _________________________________________________________________________:
return ___________________________________________________________________:
return ________________________________________________________________________
7