I am currently trying to complete the Udacity Intro to Computer Programming course and I am stuck on this homework assignment.
Here is the explanation:
Define a procedure, find_last, that takes as input
two strings, a search string and a target string,
and returns the last position in the search string
where the target string appears, or -1 if there
are no occurences.
Example: find_last('aaaa', 'a') returns 3
I wouldn't like any answers, just explanation to the code snippet I will post.
def find_last(search, target):
begin = search.find(target)
if begin == -1:
elif begin >= 0:
thru = begin + 1
while search != "":
mid = search.find(target, thru)
search = search[mid:]
For the WHILE block I am trying to search "search" for "target" and then loop back and search after the occurrence of "target" then recreate "search" with everything up to the first occurrence removed. Then redo it until the last occurrence.
So for example "search" would be reduced like this:
Removing occurrences from the front forward until hitting the last one and reveling the position it starts at.
I am determined to figure this stuff out on my own so I haven't looked at anything else but I can't figure it out because I am pretty sure I am mis using something.
Any hints or explanation to what I am doing wrong would be greatly appreciated!!
Also I hope I explained myself well!!!
P.S. I can only use what's in the current code. Nothing super fancy, hehe.