3.6.1.7 – Lists | more details
Now we want to show you some simple programs utilizing lists.
The first of them tries to find the greater value in the list. Look at the code in the editor.
The concept is rather simple – we temporarily assume that the first element is the largest one, and check the hypothesis against all the remaining elements in the list.
The code outputs 17
(as expected).
The code may be rewritten to make use of the newly introduced form of the for
loop:
The program above performs one unnecessary comparison, when the first element is compared with itself, but this isn’t a problem at all.
The code outputs 17
, too (nothing unusual).
The question is: which of these two actions consumes more computer resources – just one comparison, or slicing almost all of a list’s elements?
Fonte: https://edube.org/study/pe1
Sandbox: https://edube.org/sandbox