Alterna · учебный материал
Programme recursions and trajectory
41.2: Programme recursion and trajectory: Short theory, one example and Python step-by-step.
Что в этом материале
- How it works.
- One example in execution
- Remember
Содержание и основные понятия
In EGE No. 23, the team translates the number between states. The number of programs to state can be considered to be a memo or dynamic recursion.
substance and practice
Grade 11
OGE No. 6/16
EEG No. 16/17/23 - 27
01
How it works.
The task on the trajectory describes the state and command of the transition. The number of ways to reach the state is equal to the sum of the ways to get into the permissible predecessors.
Status.
Number or other sufficient characteristic of the current entry.
Base.
There's one empty way to start.
Transition.
Each team adds to the path from the same predecessor.
A ban.
Unacceptable condition has zero paths and does not transfer further.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
For teams +1 and *2, it is convenient to count the table from start to target: the even value has the predecessors valeu - 1 and valé / 2, the odd only vale - 1. If the path is to go through the point, often two independent segments are counted and the numbers are multiplied. The direction of the transitions should be written before the program, otherwise it is easy to fold the path out of the wrong state.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Set ways [1]
3
Take the following state
4
Shake the paths of the predecessors
5
Save Ways [value]
03
Remember
Check start = = Target: the answer is usually 1.
Determine whether forbidden is allowed to match the start or target.
Don't count on the forbidden point, and then use it as a predecessor.
Syntax of this page
dict.get(key, 0)
Returns the number of tracks from the predecessor or 0 if it is not possible.
range(start + 1, target + 1)
It's going on in an increasing order, when all the predecessors are already counted.
%
Checks if there's a reverse transition through a 2-way.
+=
Adds up the way from the extra predecessor.
if
Deletes the forbidden state or adds a conditional transition.
print
Computes the number of paths to the target.