CS计算机代考程序代写 CS 61A Structure and Interpretation of Computer Programs

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 ( )

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
>>> is_palindrome(��)
True
>>> is_palindrome(�a�)
True
>>> is_palindrome(�ab�)
False
���
if _______________________________________________________________________________:

return True

return ___________________________________________________________________________

SCI lancs B

lerG c I

s fo SEA and is palindromeSU B
i

BASECn.se
Sleen63B

Whenareyouloo it’sapalindromewithouthavingtodoanymorework

3

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�
>>> greatest_pal(��)
��
���
def helper(a, b, c):

if ________________ > ________________________________________________________:

return ___________________________________________________________________

elif ________________________ > _____________________________________________:

return ___________________________________________________________________

elif ___________________ and _________________________________________________:

__________________________________________________________________________

return _______________________________________________________________________

return helper(1, 0, ��)

a startsat E

bi starts
at 0

string startsat

while length hen s
while start lenk length gardens

jengthstart
index b bta

c whatoff’Ygesffenpaundrone sofar braLen
a eenG

c

b t a denG
helper att O c

ispalindromeGlebbd A Len c
c s b b ta
helpeda btl c

w w w
w

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):

if ___________________________________________________________________________:

return ___________________________________________________________________

elif _________________________________________________________________________:

return ___________________________________________________________________

elif _________________________________________________________________________:

return ___________________________________________________________________

return _______________________________________________________________________

return ___________________________________________________________________________

a on
cck

atb seenG
helperCa l O

is palindrome s b b ta

S b bta
helperCab tD
helperClerks O

5

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 ____________________________________________________________________:

return ____________________________________________________________

for __________ in _____________________________________________________:

if ________________________________________________________________:

return ________________________________________________________

return s

she if

Len L I
5

i rangetents112
Sli _s eenCs i l

Max greatestpaltwoSGD greatestpaltwoSL B keyLen

a max 2 1
LaxC 2,1 key lambda x x 2
2

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�)

7

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 ________________________________________________________________________