3

I'm trying to code a function to find the last substring in a string. I don't want a solution in any other code, I need to do it using my own procedure for course homework.

Most tests work, although when testing aa in aaaaa it fails. I understand why because its starting from a position with only a left, but how can I fix this?

def find_last(s, c):
    last_position = 0
    result = -1

    while True:
        next_position = s.find(c, last_position)
        if next_position == -1:
            break
        result = next_position
        last_position = next_position + len(c)

    return result

print(find_last("aaaaa", "aa")) # should output 3 but doesn't?
5
  • I sincerely like that you don't want code as an answer for this homework. I'll give you the nudge you need instead. You can try printing the aaaaa string from the end. Think: indexing by length of the string, then slowly building the substring from the end and checking along the way. ;) Commented Mar 17, 2019 at 19:15
  • Why can’t you just reverse the str and do what you are doing. Commented Mar 17, 2019 at 19:18
  • aaaaa wont always be the same, it needs to work for any string. I think @JeromeMontino also thought it was only for aaaaa not sure. Commented Mar 17, 2019 at 19:18
  • You can actually do this for any line, not just aaaaa. If you don't want to use built-in methods, you can either reverse as Raman pointed out and check from the reversed version OR you work backwards the string's index using its length as initial index and decrementing and checking along the way. Commented Mar 17, 2019 at 19:21
  • For brownie points, you might want to stop using str.find and replace it with string slicing and comparison. Commented Mar 17, 2019 at 19:32

3 Answers 3

1

If you are allowed to use built-in functions, you could do this:

idx = s[::-1].find(c[::-1])
return len(s) - (idx + len(c)) if idx >= 0 else -1
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is this line:

last_position = next_position + len(c)

This is skipping potential matches. As it is, your code considers only the first, third, and fifth positions for matches. As you say, the right answer comes from checking the fourth position (index == 3). But you're skipping that because you move the length of the test string each time, rather than moving forward by only one character.

I think you want:

last_position = next_position + 1

Comments

0

It's because you're increasing next_position with length of found substring thus missing last match.

def find_last(s, c):
    last_position = 0
    result = -1

    while True:
        next_position = s.find(c, last_position)
        if next_position == -1:
            break
        result = next_position
        #last_position = next_position + len(c)
        last_position += 1

    return result

print(find_last("aaaaa", "aa")) # -> 3

You could also use built-in python function rindex() which will return first index counting from end of string

print("aaaaa".rindex("aa")) # -> 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.