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 8118765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:35:59+00:00 2026-06-06T04:35:59+00:00

The following is for Python 3.2.3. I would like to write a function that

  • 0

The following is for Python 3.2.3.


I would like to write a function that takes two arguments, a key string and a target string. These function is to recursively determine (it must be recursive) the positions of the key string in the target string.

Currently, my code is as follows.

def posSubStringMatchRecursive(target,key):
    import string
    index=str.rfind(target, key)
    if index !=-1:
        print (index)
        target=target[:(index+len(key)-1)]
        posSubStringMatchRecursive(target,key)

The issue with this is that there is no way to store all the locations of the key string in the target string in a list as the numbers indicating the location will just be printed out.

So, my question is, is there any way to change the code such that the positions of the key string in the target string can be stored in a list?


Example Output

countSubStringMatchRecursive ('aatcgdaaaggraaa', 'aa')

13
12
7
6
0

Edit

The following code seems to work without the issue in Ashwini’s code. Thanks, Lev.

def posSubStringMatchRecursive(target,key):
    import string
    index=str.rfind(target, key)
    if index ==-1:
        return []
    else:
        target=target[:(index+len(key)-1)]
        return ([index] + posSubStringMatchRecursive(target,key))
  • 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-06T04:36:01+00:00Added an answer on June 6, 2026 at 4:36 am
    def posSubStringMatchRecursive(target,key,res):
        import string
        index=str.rfind(target, key)
        if index !=-1:
    
            target=target[:(index+len(key)-1)]
            res.append(index) #append the index to the list res, 
            return posSubStringMatchRecursive(target,key,res) #Use return here when calling recursively else your program will return None, and also pass res to the function
        else:
            return res
    
     print(posSubStringMatchRecursive('aatcgdaaaggraaa', 'aa',[]))#pass a empty list to the function
    print(posSubStringMatchRecursive('aatcgdaaaggraaa', 'a',[]))
    

    output:`

    [13, 12, 7, 6, 0]`
    [14, 13, 12, 8, 7, 6, 1, 0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2
I would like to parse free-text time intervals like the following, using Python: 1
I have a Python module that I would like to upload to PyPI. So
I would like to write Python script, which will get i.e. 100 news/texts from
I have a need for a high-performance string hashing function in python that produces
I would like to write multiple functions in the same Python module, each of
I'm curious how the Python Ninjas around here would do the following, elegantly and
I have the following python script which takes some inputs and puts them in
I'm trying to write a Python class that has the ability to do the
I would like to rewrite the python code example from the book to C#

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.