(Python) The for loop statement. Typical tasks


Task
Find the sum of all integers between 100 and 500. 

Let's write a program that solves this problem without using a formula. If we just write the result of the addition to the variable s, for example, as
\(s=100+101+102+103+...+500\),

we will spend a lot of time on the recording itself, because the computer will not understand how to use the ellipsis in an arithmetic expression and we will have to write all the numbers from 100 to 500. And the value of such a program will be negligible. Especially if we want to change our numbers and take a different range.

What should we do?
If we pay attention to the entry above, then we constantly use the addition "+".
You can try adding numbers to the s variable gradually. For example, using this notation
s=s+i.
What we did here:
1) on the right we put the expression s+i, , that is, we take the value of the variable s and add the value of the variable to it i;
2) on the left we set the name of the variable s, that is, the entire calculation result on the right will be stored in the same variable s, so we will change the value of the variable s
It remains only to change the value of the variable i in the desired range. This can be done with a for.
loop  
The numbers from 100 to 500 that are in our range should go into the i variable in turn. 
Example
# IMPORTANT! First you need to reset the variable s, # so that at the first step the number 100 is added to zero, # and not to what's in memory! s = 0 for i in range(100, 501): # loop header where i s += i # changes its value from 100 to 500 in steps of 1, # in the body of the loop gradually to the variable s,   # add the value of the changing variable i, # and save the result back in the variable s

This solution is very similar to calculating the sum over actions:
 \(s = 0 + 100 = 100, \\ s = 100 + 101 = 201, \\ s = 201 + 102 = 303 \\ ... \)
 
 

When studying the conditional operator, we touched on the topic of finding the maximum number from several entered numbers. In the "Maximum of four numbers" problem, we used the following algorithm:
1. Assign the value of the variable Max to the first of four variables
2. If the value of the second variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the second variable
3. If the value of the third variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the third variable
4. If the value of the fourth variable is greater than the value in the maximum variable, then replace the value of the maximum variable with the value of the fourth variable

It can be seen that we compared each number (denoted by X) with the variable maximum, as follows
pseudocode:
input Х
if (maximum < X), then
  maximum = X
The main thing in this code is to determine what initial value the variable maximum will have.
Usually, when solving the problem of finding the maximum or minimum, the initial value of the variable maximum is assigned equal to the first number.
Thus, the above code must be executed 1 times less than the number of numbers (since the first number must be entered and stored as the initial value of the variable maximum).
If we have the number of numbers set from the keyboard (for example, into the variable n), then we can organize a cycle (from 2 to n), using the same variable to store the number.
Try to write the whole program yourself

If we need to find the maximum (minimum) not among all elements of the array, but only among numbers that satisfy a certain condition, then we must take into account the fact that the first element that we take as the initial value of the maximum (minimum) will not always satisfy our requirements.

For example, if we look for the maximum negative number, then having the data set: {5, -2, 4, 2, -1, -3} we will get the number 5 in the initial value of the maximum, and it is positive and more than any negative. And therefore, the condition X> maximum will always be false.

Therefore, it is not enough to add only a check for a negative number in the algorithm from the previous task, it is also necessary to take into account the fact that the first number may not satisfy the required condition (in this case, be negative)
You can fix this by adding the following condition inside the loop:
pseudocode:
if X is negative, then
   if maximum> = 0 or maximum <X, then
     maximum = X
In the indicated code, the condition maximum> = 0 allows the action maximum = A to be performed even if initially a value that obviously exceeds the others gets into the variable maximum (in the example considered by us, the value is 5).

We also note that if the range of variation of numbers is known, then as the initial value of the maximum (minimum), you can take the minimum (maximum) number from the specified range.

Task:  Given N numbers. Find the second largest maximum element of the sequence..

This task has two interpretations.
For example, if we are given a set of numbers: 10 15 20 35 14 35 10, then what should be the answer?
By "second largest maximum element" or simply "second maximum", we can understand:
1) the value that would have stood in the penultimate place if we had all the values ​​arranged (sorted) in non-decreasing order (each next one is greater than or equal to the previous one). Then for the considered set of numbers the answer will be the value 35
2) the value of the element, of which only the maximum is greater. Then the answer is 20
If there is only one maximum element in the set of numbers (all the others are smaller), then both interpretations coincide, and the answer will be the same for both cases, otherwise the answer will be different.

Consider the first case (the algorithm will be written in pseudocode).
To find the answer, we will use two variables:
1) maximum1 - maximum value (first maximum)
2) maximum2 - second maximum (our answer)
If the range of variation of the values is known, then as the initial values we take a number that is obviously less than the lower boundary of the range (for example, for a range from -1000 to 1000 - take the number -1001)
If the range of values is unknown, then the first two input numbers can be written in the initial values of the variables maximum 1 and maximum 2, and then compare these two variables
input N //amount of numbers
input a, b
maximum1 = а
maximum2 = b
if b > a then
    maximum1 = b
    maximum2 = a
Next, we consider all the other elements (the first 2 have already been viewed, so we will start from the 3rd)
for i <-- 3 to n
    input а
    if а > maximum1 //a value greater than the maximum1
      then 
        maximum2 = maximum1 //the former first high will become the second
        maximum1 = а  //the first high will be a new element
      else
       // next element no more than maximum1
       // it must be compared with a value of maximum 2
        if а > maximum2 then
            maximum2 = а  // accept it as a new value maximum 2
                          // maximum1 in this case does not change
Try to implement this algorithm yourself