Question: Modify the find function in the program below so that it has a third p
ID: 3619693 • Letter: Q
Question
Question: Modify the find function in the program below so that it has a third parameter, the index in the string where it should start looking.def find(str, ch):
index = 0
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
In a sense, find is the opposite of the [] operator. Instead of taking an index and
extracting the corresponding character, it takes a character and finds the index
where that character appears. If the character is not found, the function returns
-1.
This is the first example we have seen of a return statement inside a loop. If
str[index] == ch, the function returns immediately, breaking out of the loop
prematurely.
If the character doesn’t appear in the string, then the program exits the loop
normally and returns -1.
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. Question: Modify the find function in the program below so that it has a third parameter, the index in the string where it should start looking.
def find(str, ch):
index = 0
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
In a sense, find is the opposite of the [] operator. Instead of taking an index and
extracting the corresponding character, it takes a character and finds the index
where that character appears. If the character is not found, the function returns
-1.
This is the first example we have seen of a return statement inside a loop. If
str[index] == ch, the function returns immediately, breaking out of the loop
prematurely.
If the character doesn’t appear in the string, then the program exits the loop
normally and returns -1.
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.
Explanation / Answer
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.