The first time I hit forward the function definition is evaluated, and we're taken
to line nineteen where I've put a call to the function, using a slightly smaller
example. When the function is called, the parameter
s, refers to the string a c, c f, f h which we passed as an argument to the
function. After line eleven has executed, repeats
refers to the value zero. On line thirteen is the first line of the
for loop. I will get the initial value of zero.
I Initially refers to zero.
And the if condition is first checked. So, s at i, s at zero, is a is compared
with s at i + one, s at one, which is c. These characters are not equal, so the
body of the for loop is not executed. We move on and increment i.
So i gets the next value that range generates, which is one.
Now, i refers to one. And s at one is compared with s at two
because the characters here are equal, The body of the, if, is executed.
And repeats will be increased by one. Repeats now refers to the value one.
And i gets the next value that range generates which is two.
The string at index two is compared with the string at index three and they are not
equal, so the body of the f is not executed.
We advance to the for loop header again, and i will be the next value that range
generates. I, I refers to three.
The string at index three is compared with the string at index four, those two
characters are equal, so the body of the if is executed this time, repeats is
increased by one, so repeats now refers to the value two and i advances to the next
value, which is four. The string at index four is not equal to
the string at index five so the body of if won't be executed.
And notice that this is the last pair of characters that we need to compare, so
when the string at index -two, or the second last character of the string is
reached, that will be the last iteration of the for loop.
We've reached the header one more time and there are no more numbers generated by
range. So after this, Python advances to line
seventeen. The value to be returned is the value of
repeats, which is two and that is returned and then printed on line nineteen.
Now let's write another function that involves looping over indices.
This function is named shift left. It has a single list parameter and
modifies that list, so the return type is none-type, none is returned.
The modification to the list is that each element of the list, or each item, is
shifted one position to the left. And the first item is shifted, or moved,
to the last position. A precondition to this function is that
the length of the list is at least one. Here is an example list that has four
strings. Strings a, b, c, and d.
The strings will be shifted to the left, moving b from index one to index zero.
C from index two to index one. D
From index three to index two. And a from zero to the last position where
d originally was. Therefore the resulting list is b, c, d
and a. In the example, I showed moving b to the
position that a was in. And then c to the position that b was in,
originally in. One thing that I didn't explicitly show in
the example is that when we move b into a's position, we need to keep track of a.
What value is at index zero, in order to later be able to move that value to the
last position in the list. The variable first item is what I'm going
to use to keep track of this. So it gets the value at list index zero.
And we will then be able to take the value from index zero and put it in the last
position later on. The task that we're doing is the shifting
of items starting from one. So I'm using range again.
The first index that I will work with is index one.
And the stopping position is the length of the list.