9.17. A find
functionΒΆ
Here is an implementation for the find
method.
In a sense, find
is the opposite of the indexing operator. Instead of taking
an index and extracting the corresponding character, it takes a character and
finds the index where that character appears for the first time. If the character is not found,
the function returns -1
.
The while
loop in this example uses a slightly more complex condition than we have seen
in previous programs. Here there are two parts to the condition. We want to keep going if there
are more characters to look through and we want to keep going if we have not found what we are
looking for. The variable found
is a boolean variable that keeps track of whether we have found
the character we are searching for. It is initialized to False. If we find the character, we
reassign found
to True.
The other part of the condition is the same as we used previously to traverse the characters of the
string. Since we have now combined these two parts with a logical and
, it is necessary for them
both to be True to continue iterating. If one part fails, the condition fails and the iteration stops.
When the iteration stops, we simply ask a question to find out why and then return the proper value.
Note
This pattern of computation is sometimes called a eureka traversal because as
soon as we find what we are looking for, we can cry Eureka! and stop looking. The way
we stop looking is by setting found
to True which causes the condition to fail.