CS 61A lect9 tree recusion
CS61A lect9 tree recusion• Each cascade frame is from a different call to cascade.• Until the Return value appears, that call has not completed.• Any statement can appear before or after the recursive call.Write a function that prints an inverse cascade:1121231234123121
12345678910def inverse_cascade(n): grow(n) print(n) shrink(n)def f_then_g(f, g, n): if n: f(n) g(n)grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)
tr ...
CS 61A lect8 recursion
CS61A lect8: recusionThe Anatomy of a Recursive FunctionThe def statement header is similar to other functions• Conditional statements check for base cases• Base cases are evaluated without recursive calls• Recursive cases are evaluated with recursive calls Iteration is a special case of recursion.
CS 61A lect6 code standards
CS61A lect6 code standardsChoosing NamesNames typically don’t matter for correctnessbut they matter a lot for compositionNames should convey the meaning or purpose of the values to which they are bound.The type of value bound to the name is best documented in a function’s docstring.Function names typically convey their effect (print), their behavior (triple), or the value returned (abs)Which Values Deserve a NameRepeated compound expressionsMeaningful parts of complex expressionsMore Naming Tips ...
CS 61A lect5
CS61A lect5Environments for Higher-Order Functions
12345def apply_twice(f, x): return f(f(x))def square(x): return x * xresult = apply_twice(square, 2)
Applying a user-defined function:• Create a new frame• Bind formal parameters (f & x) to arguments• Execute the body: return f(f(x))Nested Def Statements
12345def make_adder(n): def adder(k): return k+n return adderadd_three=make_adder(3)
Every user-defined function has a parent frame (often global)• The parent of a function is t ...
CS 61A lect4
CS61A lect412345678def fib(n): """Compute the nth Fibonacci number, for N >= 1.""" pred, curr = 0, 1 # 0th and 1st Fibonacci numbers k = 1 # curr is the kth Fibonacci number while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
Describing FunctionsA function’s domain is the set of all inputs it might possibly take as arguments.A function’s range is the set of output values it might possibly return.A pure function’s behavior is the relationship i ...
CS 61A lect3
CS61A lect3None Indicates that Nothing is Returned:The special value None represents nothing in PythonA function that does not explicitly return a value will return NoneCareful: None is not displayed by the interpreter as the value of an expressionexample:print(print(1), print(2))12None None
Miscellaneous Python Featuresinterept:python3 -i xx.pymultiple return valuesdoctests:write inputs in the 注释!for example:""">>>q, r=divide_exact(5, 3)>>>q201>>>r3&quo ...
CS 61A lect2
CS61A lect2Anatomy of a Call Expression:operator, operandadd ( 2 , 3 )
Operator Operand Operand
Operators and operands are also expressions
Evaluation procedure for call expressions:
Evaluate the operator and then the operand subexpressions
Apply the function that is the value of the operator
to the arguments that are the values of the operands
Execution rule for assignment statements:
Evaluate all expressions to the right of = from left to right.
Bind all names to the left of = to ...
