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

min and max without finished functions

35.3: min and max without finished functions. Short theory, one example and a step-by-step path Python.

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

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

To understand the branch deeper, we will build a minimum and maximum by hand: we will start the current candidate and replace him only with a stronger value.
substance and practice
Grade 8
OGE No. 6
OGE No. 16
01
How it works.
The built-in min and max are useful, but the manual candidate shows the invariant: after each best check, saves the best result among the values already seen.
The candidate.
The first acceptable value from which the search begins.
Invariant.
A statement that remains true after each iteration or verification.
Equality.
The sign > leaves the first position of maximum; >= leaves the last.
Independence.
For a consistent comparison of values, you need separate if, not elif.
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.
You can't start with a maximum of zero if the data can be negative. Start with the first element or the clearly defined lower limit of the condition. When you want not only the maximum, but also its position, update the value and index of one branch.
min
and
max
does not operate with empty sequence without additional arguments.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Take the first candidate
3
Compare the following value
4
Replace candidate if necessary
5
Bring Out the Best
03
Remember
Check all negative values.
Clearly decide what to do with equal maximums.
Do not call max() or min() until the behaviour of the empty set is determined.
Syntax of this page
min(a, b, ...)
Returns the lowest of the transmitted values.
max(a, b, ...)
Returns the largest of the transmitted values.
if value > best
The condition of a candidate's advancement.
enumerate(values)
It gives you pairs (index, value) when you look for a position in a sequence.
map(int, input().split())
He reads several whole lines.
print
We've got a candidate coming out.

Structural map of textbooks