Lane
homework of cs61a

homework of cs61a

homework of cs61a

lab2

1
2
3
4
>>> b = lambda x: lambda: x  # Lambdas can return other lambdas!
>>> c = b(88)
>>> c
>>> c()
answerFunction 88
1
2
3
4
5
6
7
8
9
>>> higher_order_lambda = lambda f: lambda x: f(x)
>>> g = lambda x: x * x
>>> higer_order_lambda(2)(g)
>>> higer_order_lambda(g)(2)

In [10]: p=print('1')
1

In [11]: p
answerError 'int' object is not callable ; 4 ; Nothing

project2-cats

split and remove_punctuation(used in problem2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s_list=split(remove_punctuation(lower(s)))

(function) def split(s: Any) -> Any
Return a list of words contained in s, which are sequences of characters separated by whitespace (spaces, tabs, etc.).

>>> split("It's a lovely day, don't you think?")
["It's", 'a', 'lovely', 'day,', "don't", 'you', 'think?']

(function) def remove_punctuation(s: Any) -> Any
Return a string with the same contents as s, but with punctuation removed.

>>> remove_punctuation("It's a lovely day, don't you think?")
'Its a lovely day dont you think'
>>> remove_punctuation("Its a lovely day dont you think")
'Its a lovely day dont you think'

切片操作(used in problem6)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def counts(curr_start,curr_goal,cnt):
if cnt > limit:
return limit + 1

if not curr_start and not curr_goal:
return cnt
elif not curr_start or not curr_goal:
return counts(curr_start[1:], curr_goal[1:], cnt + 1)
elif curr_start[0] == curr_goal[0]:
return counts(curr_start[1:], curr_goal[1:], cnt)
else:
return counts(curr_start[1:], curr_goal[1:], cnt + 1)

return counts(typed,reference,0)
#在Python中,curr[1:]这个表达式是在进行切片操作。这个表达式表示从curr这个序列(这个序列可能是列表、元组或字符串等)中,取出下标为1的元素到最后一个元素组成的子序列。Python的切片操作非常强大,基本形式是[start:end],其中start是起始下标,end是结束下标,切片的结果包含start对应的元素,但不包含end对应的元素。
本文作者:Lane
本文链接:https://lakerswillwin.github.io/2024/05/03/homeworkofcs61a/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可