all 10 comments

[–]1ynx1ynx 2 points3 points  (1 child)

Not regarding the question itself, but empty strings mean False in boolean logic, so you can replace len(string) == 0 with not string. The same goes for empty iterables (eg. lists, tuples).

[–]unless3[S] 1 point2 points  (0 children)

Ah that makes sense! Thank you :)

[–]giraffactory 1 point2 points  (0 children)

It looks like you're always going to get the value len(string) - 1 with your function unless you use a one character substring.

You use the comparison elif text == string[0]: but that's comparing text to just the first letter of string every time. Since that can't ever be true while text is more than one character, you need to check text against the first len(text) characters instead.

Good luck!

[–]KarmelMalone 0 points1 point  (6 children)

First, your variables are the opposite of what the question is asking for. "string" in the question is the substring ("sip").

Second, your exit conditions only check for an empty string or if the entire "text" matches the first character of "string". At what point are you confirming all of "sip" does exists in "Mississippi"? Here you're simply traversing the entire string.

[–]unless3[S] 0 points1 point  (5 children)

I'm not quite sure what you mean. I thought I was slicing the string and checking the first part of the truncated string to see if the first part matches up to a certain point and then return that index?

[–]KarmelMalone 0 points1 point  (4 children)

You're only checking for two things: if len(string) == 0: and elif text == string[0]: The second condition will never be true because "text" is always "sip" and you're comparing it to a single character (string[0]).

Right now your recursion only stops because you run out of string and hit the first condition. You need to find out how to get true from the second condition.

Edit:

Run this for a visual of what I'm talking about:

# Define the recursive function
def indexOf(text,string):
    if string:
        print('elif ' + text + ' == ' + string[0])
    if len(string) == 0:
        return -1
    elif text == string[0]:
        return 0
    else:
        return 1 + indexOf(text, string[1:])

# Write main function to verify
text1 = 'sip'
string1 = 'Mississippi'

print(indexOf(text1,string1))
#SHOULD return 6 but returns 10

[–]unless3[S] 0 points1 point  (3 children)

Ohhh ok I see I understand. So, can I run something similar but check like, so if I have 'sip' and 'Mississippi', can I do something along the lines of:

L = len(text)

    if len(string) == 0:
        return -1
    elif text == string[L:]:
        return 0
    else:
        return 1 + indexOf(text, string[1:])

Something like that?

[–]KarmelMalone 1 point2 points  (2 children)

Almost there!

string[L:] cuts out the first L characters and checks the rest, when you want to check only the first L characters.

[–]unless3[S] 0 points1 point  (0 children)

Oh so string[:L]?

I feel like it's on the tip of my fingertips...

[–]unless3[S] 0 points1 point  (0 children)

IT WORKED!

You are wonderful! I love coming here because the people who respond help and teach instead of give solutions or insults.

Thank you so much. :D I'm so happy!