Alterna · учебный материал

Searching and counting symbols

37.2: Searching and counting symbols. Short theory, one example, and Python step-by-step.

Что в этом материале

Содержание и основные понятия

For one passage, only the necessary condition is kept: the counter, the current length of the series and the best result.
substance and practice
Grade 8
EEG No. 24
01
How it works.
The line processing of the line contains a small state: the current series, the input counter, the position, or the best finding result.
count.
Number of events that satisfied the condition.
current.
The length of the series that ends on the current symbol.
best.
The best result of all the items already processed.
The window.
The segment s[i:i+k] checks the adjacent symbols or substrate.
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.
count
Considers non-overlapping substrates, and overlapping couples should be considered as index passage.
find
returns the index of first entry or -1;
index
It's similar, but in absence it's called
ValueError
I. For the update series
best
Right after the increase
current
Or the last episode could get lost.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Take the next symbol
3
Symbol A?
4
Yes: increase the series and best · No: drop
5
Remove best
03
Remember
Different overlapping and non-overlapping entrances.
Check an empty line and a line from the same symbol.
Update the best after the last symbol, not just when the series is broken.
Syntax of this page
for ch in s
He moves the lines in order from left to right.
range(len(s) - 1)
Gives you the right start on all pairs of neighbouring symbols.
s.count(part)
Considers non-overlapping memberships.
s.find(part)
Returns the index of the first part or -1.
s.rfind(part)
Returns the index of the last part or -1.
s.index(part)
As fin, but in the absence of part, it causes ValueError.
max(a, b)
Selects a larger result; it is convenient to update the serial record.
strip
Deletes the translation of the line and the blanks on the entrance edges.

Structural map of textbooks