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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:03:24+00:00 2026-06-01T02:03:24+00:00

This is a very simple code in place of a bigger problem, but I’m

  • 0

This is a very simple code in place of a bigger problem, but I’m hoping I can tackle it in chunks. I’ll start with my first problem.

def testrecurse(z,target):
    x=[]
    if z<target:
        z*=2
        x.append(z)
        x.extend(testrecurse(z,target))
    return x

This is a test function to help my brain out with recursion. It takes a number, then shows all the multiplications of two until it hits the target number. so if I enter:

testrecurse(1,1000)

I receive:

[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

which is great! Output looks good and clean. But here’s my problem, I’m having a hard time appending or adding that very first value, in my output. Here’s what I want the output to look like.

[1,2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

I’ve tried changing

x=[] to x=[z]

but then I receive:

[1, 2, 2, 4, 4, 8, 8, 16, 16, 32, 32, 64, 64, 128, 128, 256, 256, 512, 512, 1024, 1024]

any help would be appreciated, I’m new to recursion and it makes my head hurt.

  • 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-01T02:03:26+00:00Added an answer on June 1, 2026 at 2:03 am

    How about this?

    def testrecurse(z, target):
        if z >= target:
            return []
        return [z] + testrecurse(2 * z, target)
    

    Example:

    >>> testrecurse(1, 1000)
    [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
    

    Note that it does not include 1024 any more. If you want this, change the third line to

            return [z]
    

    Of course you wouldn’t normally write this recursively, but rather use a for loop or itertools.takewhile().

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

Sidebar

Related Questions

Maybe a very simple question. How can I put in this code <Query> <Where>
I've been stuck on this likely very simple problem, but haven't gotten anywhere with
This is a very simple question, but one I can't find an answer to.
This very simple code gives me tons of errors: #include <iostream> #include <string> int
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
i am trying to compile this very simple piece of code class myList {
Which method is very effective and simple to code to attain this functionality ?
I was making this very simple lex program (just an introductory program). But on
Okay, this is probably very simple but, I have the below checks (not at
This is probably a very silly and simple question to most of you, but

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.