Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:58:58+00:00 2026-06-02T22:58:58+00:00

I was playing around with a function for an assignment just to better understand

  • 0

I was playing around with a function for an assignment just to better understand it. It was meant to find the last occurrence of a sub-string within a string. The function should return the position of the start of the last occurrence of the sub-string or it must return -1 if the sub-string is not found at all. The ‘standard’ way was as follows:

def find_last(full, sub):
    start = -1
    while True:
        new = full.find(sub, start + 1)
        if new == -1:
            break
        else:
            start = new
    return start

I wanted to try and have it search in reverse, as this seemed to be the more efficient way. So I tried this:

def find_last(full, sub):
    start = -1
    while True:
        new = full.find(sub, start)
        if new == -1 and abs(start) <= len(full): #evals to False when beginning of string is reached
            start -= 1
        else:
            break
    return new

We were given a handful of test cases which needed to be passed and my reversed function passed all but one:

print find_last('aaaa', 'a')
>>>3
print find_last('aaaaa', 'aa')
>>>3
print find_last('aaaa', 'b')
>>>-1
print find_last("111111111", "1")
>>>8
print find_last("222222222", "")
>>>8 #should be 9
print find_last("", "3")
>>>-1
print find_last("", "")
>>>0

Can someone kindly explain why find is behaving this way with negative indexing? Or is it just some glaring mistake in my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:59:00+00:00Added an answer on June 2, 2026 at 10:59 pm

    The empty string can be found at any position. Initializing start with -1 makes your algorithm beginning its search at the penultimate position, not the last.

    The last position is after the last character of the string, but you are starting to look at the last character of the string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just playing around with the id() function & noticed that if you
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I was playing around with the following code: function recaller(){ while(x-- > 0)recaller(); }
I was playing around with recursion and did this simple function. I was assuming
There is an eval() function in Python I stumbled upon while playing around. I
I'm playing around with beginner Haskell, and I wanted to write an average function.
So I've been playing around with the replace function (method?) in js. $get('msgBar').replace(/+/g,' ');
i'm playing around with building a sql function that will extract numbers from a
I have just been playing around with IPython. Currently I am wondering how it
I'm playing around with box2djs . I'm trying to find a way to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.