Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
want to return recursive function named find(text,substring) that will return the substring if the substring is inside the text. or return an empty string if substring is not inside the text. supposed to define a function named is_string_there(text, string) to test the find function. the function should return True if the string is inside the text or False if the string is not inside the text.



output i want to achieve:
true
false

What I have tried:

<pre>def find(text, substring):
  if substring == is_string_there: # compare them first
        return is_string_there
    else:
        return  # how do i return empty
  
def is_string_there(text, string):

      
print(is_string_there("I love DSAG", "DSAG"))
print(is_string_there("I love Python", "DSAG"))
Posted
Updated 5-Feb-20 21:25pm
Comments
Richard MacCutchan 6-Feb-20 3:47am    
Recursion makes no sense for a simple test like this.
fadxo20 6-Feb-20 4:43am    
def find(text, substring):
if substring == is_string_there: # compare them first
return is_string_there
else:
return # how do i return empty

def is_string_there(text, string):
if find(text, string):
return True
else:
return False

print(is_string_there("I love DSAG", "DSAG"))
print(is_string_there("I love Python", "DSAG"))

i tried running this
but the error was
false, false. not the output that i want

1 solution

The is_string_there should be something like:
Python
def is_string_there(text, string):
  if find(text, string):
    return True
  else:
    return False

and the find should be recursive, e.g.
Python
def find(text, string):
  # stop condition here
  # call the function itself here, e.g. find(text[1:], string)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900