Control & Recursive Function
Whether the statement is executed
Control expression
Control expression can skip some errors while one-line-call function will execute all the components first which may occur the errors.
# eg1 :
"""
>>> real_sqrt1(-4)
0
"""
def real_sqrt1(x):
if x>0:
return sqrt(x)
else:
return 0
# eg2
"""
>>> real_sqrt2(-4)
ERROR
"""
def real_sqrt2(x):
return if_(x>0,sqrt(x),0)
def if_(c,f,e):
if c:
f
else:
e
We can see in the above examples . In the eg1 , if the header’s expression of the if statement is not True the suite won’t be executed . Then it will skip over all subsequent clauses in the conditional statement. And next it will execute the else part . So it just returns the 0.
While in the eg2, we call the real_sqrt func ,and it return the if_ part . We must notice that when we call a function ,the python will executed the parameters of the function first before calling this function’s body .So when it comes to the if_(x>0,sqrt(x),0), python will calculate the sqrt(-4) first , which occur the error.
Logical Operators
- If A and B , A is False , B will not be executed .
- If A or B, A is True , B will not be executed .
Short circuiting behavior is a form of control.
Conditional Expression
< Consequet > if < Predicate > else < Alternative >
Evaluation rules:
First evaluate < Predicate > expression , if it is True the whole expression is the value of < Consequent > otherwise is the < Alternative >.
The order of finding a value
local - > parent -> global
When finding a parameter’s value , we will find its local frame first then its parent frame finally its global frame . The first time we find the value ,it will be bound to the parameter which means that is the value we will use.
Local name are not visible to other (non-nested) frame.
Recursion
The difference between iteration and recursion
Iteration: Using the simplest way to explain interation , we can say where there is an iteration, there must exist a loop . And there must be at least one variable that continuously deduces the new value from the old value ,either directly or indirectly. We can call it iterative variable.
We often use for statement or while statement to express the repetition.
Recursion: A function is called recusive when the body of the function calls itself, either directly or indirectly.
A common part can be found in many recursive function. We always use the if - elif - else statement in a recursive funciton. And it will begin with the base case, which is the simplest part of the problem.Also the base case determines when then recursion ends. If it hits the base case, we will get the first value and then track back to get the right answer.
Difference1:
Recursive Decomposition
Example : Partitions
The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order. For example, the number of partitions of 6 using parts up to 4 is 9.
count_partition(n,m) – eg : count_partition(6,4)
- 6 = 4 + 2
- 6 = 4 + 1 +1
- 6 = 3 + 3
- 6 = 3 + 2 +1
- 6 = 3 + 1 + 1 + 1
- 6 = 2 + 2 + 2
- 6 = 2 + 2 + 1 + 1
- 6 = 2 + 1 + 1 + 1 + 1
- 6 = 1 + 1 + 1 + 1 + 1 + 1
- Exploring the 2 possibilities:
- Using at least one 4
- Don’t use any 4
- Solving 2 simpler problems:
- count_partition(6,3) 【step 3-9】
- count_partition(2,4) – [is equivalent to count_partition(2,2)] 【step 1-2 ingoring 4】
- If we use n,m to replace the exact numbers, we will get count_partition(n,m-1) and count_partition(n-m,m) .
- Finding the base case:
- if n is equal to 0, there just exist 1 way to partition n including no parts , so it returns 1
- if n is less than 0 , no way to partition it, so it returns 0
- if m is equal to 0 ,we can not find any positive integer which is less than m so it returns 0
Now we can write the code:
# if we want see the process clearlier ,we can give a trace func
def trace(f):
def g(n,m):
res