CS61A
Function
The principle of designing a function:
-
Give each function exactly a job
-
Don’t repeat yourself. Implement a process just once, but execute it many times.
-
Define functions generally
Higher-order functions(A function that takes a function as an argument value or returns a function as a return vales):
-
express general methods of computation.
-
remove repetition from programs
-
separate concerns among functions
-
the inner function need to be defined as a real function(f) rather than a value evaluated by the function such as f(x) in a higher-order function. It actually act as a function that its output is some functions.
True/False:
- ‘and’ will return the first False value it meets or it will return the last True value
- ‘or’ will return the first True value
Recursion
Skipping questions:
- Call expressions don’t allow you to skip evaluating part of the call expressions
every parts of the statement will be executed
-
Control expressions(and/or) allow you to skip evaluating some sub-expressions
e.g. return if -100 > 0 and sqrt(-100) > 10 will not lead to a error
difference between iteration and recursion
sometimes recursion can simplify the code
Abstraction
|
|
Is fact implemented correctly?
- Verify the base case.
- Treat fact as a functional abstraction!(focus on what’s it function)
- Assume that fact(n-1) is correct.
- Verify that fact(n) is correct, assuming that fact(n-1) correct.
Containers
Strings are Sequences
|
|
In list you can only search for one word each time
When working with strings, we usually care about whole words more than letters.
Comprehensions
Both list and dictionary can use comprehension to define them
|
|
The parent frame of the comprehensions is the current frame
Data abstraction
Abstraction Barriers
The higher the more abstract. Taking computing fraction as example
This example violates the abstraction barriers. Here’s how
-
Does not use constructors: instead of using
list
‘[1, 2]’, it should userational
-
No selectors: instead of using
list element
to imply numer and denom, it should usenumer
anddenom
-
No constructor: use
add_rational
, this will make your code more readable!
All in all, the data abstraction should make a data/func’s detail the same as its name!
An elegant way to obtain the minimum number form
cat
project
|
|
Syntax
Useful string methods for processing the contents of a file:
-
.strip():return a string without whitespace (spaces, tabs, etc.)
1 2
>>>' hello '.strip() 'hello'
-
.split():returns a list of strings that were separated by whitespace
1 2
>>>'hi there'.split() ['hi', 'there']
-
.replace(a. b): returns a string with all instances of string a replaced by string b
1 2
>>>'2+2'.replace('+', ' + ') '2 + 2'
-
list: string can be seen as a string, processing it by slicing
1 2
>>>'apples'[:-1] 'apple'
-
.join(): get the elements in argument separated by the thing before it
1 2 3
>>>s = ['a', 'little', 'bug'] >>>' '.join(s) 'a little bug'
Lab 05 Mutability, Iterators
There are many other list mutation methods:
-
append(elem)
: Addelem
to the end of the list. ReturnNone
. -
extend(s)
: Add all elements of iterables
to the end of the list. ReturnNone
. -
insert(i, elem)
: Insertelem
at indexi
. Ifi
is greater than or equal to the length of the list, thenelem
is inserted at the end. This does not replace any existing elements, but only adds the new elementelem
. ReturnNone
. -
remove(elem)
: Remove the first occurrence ofelem
in list. ReturnNone
. Errors ifelem
is not in the list. -
pop(i)
: Remove and return the element at indexi
. -
pop()
: Remove and return the last element.1 2 3
>>>s = [1, 2, 3] >>>print(s.pop(1)) 2
The difference of Where iteration and For iteration:
-
Where iteration can be very useful when the range is variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
def insert_items(s, before, after): """Insert after into s after each occurrence of before and then return s. >>> test_s = [1, 5, 8, 5, 2, 3] >>> new_s = insert_items(test_s, 5, 7) >>> new_s [1, 5, 7, 8, 5, 7, 2, 3] >>> test_s [1, 5, 7, 8, 5, 7, 2, 3] >>> new_s is test_s True >>> double_s = [1, 2, 1, 2, 3, 3] >>> double_s = insert_items(double_s, 3, 4) >>> double_s [1, 2, 1, 2, 3, 4, 3, 4] >>> large_s = [1, 4, 8] >>> large_s2 = insert_items(large_s, 4, 4) >>> large_s2 [1, 4, 4, 8] >>> large_s3 = insert_items(large_s2, 4, 6) >>> large_s3 [1, 4, 6, 4, 6, 8] >>> large_s3 is large_s True """ "*** YOUR CODE HERE ***" i = 0 while i < len(s): if s[i] == before: s.insert(i+1, after) i += 1 i += 1 return s # if you ues for iteration, the changing length of list s is quite a big problem
HW 05
the yield, yield from, iter function, and the algorithms here are so hard!
I can feel I’m extremely weak at this part