recursive function
Inside the function, you can call other functions. If a function calls itself internally, and that this function is a recursive function.
For example, we compute the factorial n! = 1 * 2 * 3 * ... * n
, a function fact(n)
expressed, it can be seen:
fact(n) = n! = 1 * 2 * 3 * ... * (n-1) * n = fact(n-1) * n
So, fact(n)
may be expressed as fact(n-1)*n
, if n = 1 only when the need for special handling
So, fact(n)
write recursive way is to use:
def fact(n):
if n == 1:
return 1
return n * fact(n-1)
If we compute fact (5), you can see
--> fact(5)
--> 5 * fact(4)
--> 5 * (4 * fact(3))
--> 5 * (4 * (3 * fact(2)))
--> 5 * (4 * (3 * (2 * fact(1))))
--> 5 * (4 * (3 * (2 * 1)))
--> 5 * (4 * (3 * 2))
--> 5 * (4 * 6)
--> 5 * 24
--> 120
If recursive function calls the function itself constantly, then this recursive function will enter an infinite loop, so we should give a clear recursive function termination condition.
Core recursive: progressive time to achieve a result, more and more small-scale problems (not necessarily true reach); after setting a condition that allows the end of the last function call.
Recursive has two clear stages:
- Recursive: call down layer by layer, into the next level, the problem will be downsizing.
- Back: Recursive encountered after the termination conditions, will back a return to the values from the last level.
Recursive function has three characteristics:
- It calls itself directly or indirectly
- It has a termination condition to prevent recursion spill
- Gradually reduce code size
Recursive function is to define the advantages of simple, clear logic, the disadvantage is too deep can cause a stack overflow calls.
In theory, all recursive functions can be written in a circular fashion, but not as a recursive loop logic is clear.
Recursive function is the function calls itself, and then there is the end condition.
Built-in method
bytes()
Decoding character
res = bytes('你好', encoding='utf8')
print(res)
# b'\xe4\xbd\xa0\xe5\xa5\xbd'
chr () / word ()
CHR () refer to a digital ASCII code table converted into a corresponding character; the ord () converts the character into a corresponding digital.
print(chr(97))
# a
print(ord('a'))
# 97
divmod()
Columns
print(divmod(10, 3))
# (3, 1)
enumerate()
Iteration with indexes
lt = ['a', 'b', 'c']
for i in enumerate(lt):
print(i)
# (0, 'a')
# (1, 'b')
# (2, 'c')
eval()
Translating the strings to data type
lis = '[1, 2, 3]'
lis_eval = eval(lis)
print(lis_eval)
# [1, 2, 3]
hash()
You can hash
print(hash(1))
# 1
Process-oriented programming
Process-oriented programming functional programming is definitely not as simple a process-oriented programming ideas, and programming ideas is not dependent on a specific syntax. That we are not dependent on the function, you can also write programs based on process-oriented thinking.
Process-oriented programming, process steps to resolve the problem, what to do after that is the first
Based on procedure-oriented design, like the design of a pipeline, is a mechanical way of thinking
Pros: complex processes of the problem, and then simplify
Disadvantages: not independent function and the function between, poor scalability, indeed affect the whole body