3.6.1.7 – Lists | more details

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

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.

%d blogger hanno fatto clic su Mi Piace per questo: