Question: Add a fourth parameter, end, that specifies where to stop looking. War
ID: 3619811 • Letter: Q
Question
Question: Add a fourth parameter, end, that specifies where to stop looking.Warning: This exercise is a bit tricky. The default value of end should
be len(str), but that doesn’t work. The default values are evaluated
when the function is defined, not when it is called. When find is
defined, str doesn’t exist yet, so you can’t find its length.
--
The reading for this question is posted below.
---
Optional arguments:
We have seen built-in functions that take a variable number of arguments. For
example, string.find can take two, three, or four arguments.
It is possible to write user-defined functions with optional argument lists. For
example, we can upgrade our own version of find to do the same thing as
string.find.
This is the original version from Section 7.7:
def find(str, ch):
index = 0
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
This is the new and improved version:
def find(str, ch, start=0):
index = start
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
The third parameter, start, is optional because a default value, 0, is provided.
If we invoke find with only two arguments, it uses the default value and starts
from the beginning of the string:
>>> find("apple", "p")
1
If we provide a third argument, it overrides the default:
>>> find("apple", "p", 2)
2
>>> find("apple", "p", 3)
-1 Question: Add a fourth parameter, end, that specifies where to stop looking.
Warning: This exercise is a bit tricky. The default value of end should
be len(str), but that doesn’t work. The default values are evaluated
when the function is defined, not when it is called. When find is
defined, str doesn’t exist yet, so you can’t find its length.
--
The reading for this question is posted below.
---
Optional arguments:
We have seen built-in functions that take a variable number of arguments. For
example, string.find can take two, three, or four arguments.
It is possible to write user-defined functions with optional argument lists. For
example, we can upgrade our own version of find to do the same thing as
string.find.
This is the original version from Section 7.7:
def find(str, ch):
index = 0
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
This is the new and improved version:
def find(str, ch, start=0):
index = start
while index < len(str):
if str[index] == ch:
return index
index = index + 1
return -1
The third parameter, start, is optional because a default value, 0, is provided.
If we invoke find with only two arguments, it uses the default value and starts
from the beginning of the string:
>>> find("apple", "p")
1
If we provide a third argument, it overrides the default:
>>> find("apple", "p", 2)
2
>>> find("apple", "p", 3)
-1
Explanation / Answer
Dear, find function with fourth parameter end that specifies to stop looking def find(str, ch, start=0,end):index = start
while index <= end:
if str[index] == ch:
return index
index = index + 1
return -1 To invoke this function >>> find("Mangos,apples", "g", 0,5) it returns 3 >>> find("Mangos,apples", "g", 5,10) it returns -1 Hope this will help you... To invoke this function >>> find("Mangos,apples", "g", 0,5) it returns 3 >>> find("Mangos,apples", "g", 5,10) it returns -1 Hope this will help you... >>> find("Mangos,apples", "g", 5,10) it returns -1 Hope this will help you...
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.