Samuel
Samuel

Reputation: 3

Code that is able to search for a string in a text recursively

I have a code which satisfies the question above. However, I am personally curious on how to recode it such that if the text did not have a space and is given as "LoveIsWar", the code will still return true if the string is "War".

However, I thought of doing a check for letter by letter but I am unsure how to do it. Would really appreciate if I could get guidance on this!

def find(text, substring):
    if(len(text) <= 0):
        return None
    elif(text[0] == substring):
        return substring
    else:
        return find(text[1:], substring)


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

print(is_string_there("love is war","war"))
print(is_string_there("love is war","warfalse"))

This is the edited code that satisfies everything I want. Being able to check for the string if the text has spaces or not even if the string has capital letters in it.

def find(text, substring):
    if(len(text) <= 0):
        return None
    elif(text[0:len(substring)] == substring):
        return substring
    else:
        return find(text[1:], substring)


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

print(is_string_there("love is war","war"))
print(is_string_there("love is war","warfalse"))

Upvotes: 0

Views: 2916

Answers (5)

Drlight Dr
Drlight Dr

Reputation: 9

def finder(s, x, y):
    if len(x) == 0:
        return True
    elif len(s) < len(x):
        return False
    elif s[0] == x[0]:
        return finder(s[1:], x[1:], y)
    else:
        return finder(s[1:], y, y)
print(finder("1011101110", "111", "111"))

Upvotes: -1

Stuart
Stuart

Reputation: 1132

Great question. Given the constraint of a recursive function, in layman's terms, what you want to achieve is a sliding-window search on a string.

That is to say, that when you pass your 'text' into the find function, you do not pass it as an array, you pass the text itself.

The 'find' function then doesn't iterate through the elements in the 'array', but through the letters in the string, discarding them one by one. Hence, your "window" - or your view on the string, slides across it until you finish.

All you really need to modify is:

text[0] == substring

Should check if the text[0:length_of_substring] matches the substring. Remember, we have much the same operations on strings as we do on arrays!

If it does not, move 1 character along (much like you do in your array-based search).

For your interest, these kinds of problems can be solved very efficiently using algorithms such as Rabin Karp

A small optimisation you could make is breaking out of your search when you have fewer characters left in your "text" than there are in your substring.

Hope that helps!

Upvotes: 2

laundmo
laundmo

Reputation: 338

you do know you can check for substrings in python using the in operator?

if "war".lower() in "LoveIsWar".lower():
    return True

lower is used to match case insensitive

if you do need it to be a recursive function i suggest takign a look at this: https://www.geeksforgeeks.org/recursive-function-to-do-substring-search/

Upvotes: 0

Prashant Kumar
Prashant Kumar

Reputation: 2092

You can directly use in to figure out if a substring is present in a given string or not.

Something like this :

myString1 = "Love Is War"
myString2 = "LoveIsWar"

print("War" in myString1)

print("War" in myString2)

Upvotes: 0

ValeKnappich
ValeKnappich

Reputation: 202

I'm not sure if I'm missing something but why aren't you using the in keyword?

>>>"War" in "Love is War"
true
>>>"War" in "LoveIsWar"
true
>>>"Warfalse" in "Love is War"
false

Upvotes: 0

Related Questions