#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    2

    Last occurrence position


    Hi

    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.

    Code:
    def find_last(search, target):
        begin = search.find(target)
        if begin == -1:
            return begin
        elif begin >= 0:
            thru = begin + 1
            while search != "":
                mid = search.find(target, thru)
                search = search[mid:]
                return 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:

    "aaaa"
    "aaa"
    "aa"
    "a"

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    I think you can simplify things. You just keep track of the location if found, and then add one to the start position and look again. Note that "thru"'s value does not change in your code so if the fifth letter is found you then slice off the first 5 and start looking at "thru" which would be the 10th letter. Try your code with print find_last("aaababaaaba", "b"). And you return at the bottom of the first pass through the while loop so only find the second occurrence, otherwise it would be an infinite loop when not found ==> mid = -1 ==> slice last letter over and over.
    Code:
    def find_last(search, to_find):
        begin = search.find(to_find)
        location = begin
        while location > -1:
            begin = location     ## previous find/search found something
            location += 1
            location = search.find(to_find, location)
    
        return begin    
    
    print find_last("aaaaa", "a")
    print find_last("aaaaa", "aa")
    print find_last("aaaaa", "b")
    Last edited by dwblas; May 16th, 2013 at 08:05 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    2
    Thank you

IMN logo majestic logo threadwatch logo seochat tools logo