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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:39:17+00:00 2026-06-15T00:39:17+00:00

i have homework in which i must use recursion to find all occurances of

  • 0

i have homework in which i must use recursion to find all occurances of a number/letter/word in a list and return their index in the original list.. I have searched this site for previously answered question but I couldn’t find any answer regarding recursion with the option to continue checking the list even after the first occurances has been found..

should look like this pretty much:

>>> find_value( [4,7,5,3,2,5,3,7,8,6,5,6], 5)
[2,5,10]

my code so far goes like this:

def find_all(x,y):
    if len(x) == 1 and x[0] == y:
        return [i for i, y in enumerate(x)]
    return find_all(x[1:],y)

Though it only minimize the list and gives me the same [0] as the index.. which is true, for the divided list.. this way I will never get the original index..
Thanks
– if this already exist, I am sorry for i have searched and couldn’t find.

  • 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-15T00:39:18+00:00Added an answer on June 15, 2026 at 12:39 am

    The point of assigning a homework is usually so that you can explore the problem and learn from it. In this case, it is recursion which is usually hard for beginners.

    The point of recursion is to construct an answer for a larger problem from a solution of a smaller ones. So it is best to start off with the smallest one possible:

    def find_all(haystack, needle):
        if not haystack:
            # no occurrences can happen
            return []
    

    If the list is not empty, we can check if the first element is what we are looking for:

        if haystack[0] == needle:
            occurrences = [0] # the index of the first element is always 0
        else:
            occurrences = []
    

    We will also need the solution to the smaller problem:

        recursive_occurences = find_all(haystack[1:], needle)
    

    Now the problem you have noticed is that the indices that are returned are always 0. That’s because they are indices in the smaller list. If an item has index 0 in a smaller list, it means its index in the largest list is actually 1 (this is the main part your program was missing), thus:

        for x in recursive_occurences:
            occurrences.append(x+1)
    

    And return the complete answer:

        return occurrences
    

    I hope this helps you a bit, so you can do your next homework on your own.

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

Sidebar

Related Questions

I have homework which I have to use scriptlets in , I need to
I have a homework problem in which I need to remove all the green
I have a homework in which i have 640x480 image and i want to
I have a Homework Planner application which I want to create an Alarm for
I have a homework assignment which asks to have the user input a date
I have this homework problem where I need to use regex to remove every
Minimum Set Cover is a question where you must find the minimum number of
Greetings I've been working on a homework in which I must create a linked
I have a homework question which asks to read a string through raw input
As question states, I am doing homework which have 2 variables BOOK, and MAGAZINE

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.