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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:23:01+00:00 2026-06-01T21:23:01+00:00

This is the code I came up with: def combinations(input): ret = [”] for

  • 0

This is the code I came up with:

def combinations(input):
    ret = ['']
    for i in range(len(input)):
        ret.extend([prefix+input[i] for prefix in ret])
    return ret

This algorithm is O(2^n) time, but can space be reduced? I heard using yield might work, but having trouble thinking through how to implement with yield. Please don’t use the built in combination function — I would like to see how it’s implemented.

  • 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-01T21:23:03+00:00Added an answer on June 1, 2026 at 9:23 pm

    Your question specifically said you wanted to see what the code would look like, so here is a hand coded example of an O(n) space solution:

    def combinations(input_list, acc=''):
    
        if not input_list:
            yield acc
            return
    
        next_val = input_list[0]
    
        for rest in combinations(input_list[1:], acc):
            yield rest
    
        acc += next_val
    
        # In python 3.2, you can use "yield from combinations(input_list[1:], acc)"
        for rest in combinations(input_list[1:], acc):
            yield rest
    

    Note that the substring arithmetic might be expensive (since it has to copy the string many times), so here is a slightly more efficient version in terms of complexity:

    def combinations(input_list, acc='', from_idx=0):
    
        if len(input_list) <= from_idx:
            yield acc
            return
    
        next_val = input_list[from_idx]
    
        for rest in combinations(input_list, acc, from_idx + 1):
            yield rest
    
        acc += next_val
    
        # In python 3.2, you can use "yield from combinations(input_list[1:], acc)"
        for rest in combinations(input_list, acc, from_idx + 1):
            yield rest
    

    I’m not using Python 3.2, but if you were you could write it like this:

    def combinations(input_list, acc='', from_idx=0):
    
        if len(input_list) <= from_idx:
            yield acc
            return
    
        next_val = input_list[from_idx]
    
        yield from combinations(input_list, acc, from_idx + 1)
        acc += next_val
        yield from combinations(input_list, acc, from_idx + 1)
    

    I should also note that this is purely academic since itertools.combinations does a fine job and works for a wider array of inputs (including generator expressions).

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

Sidebar

Related Questions

I came up with this code: def DSigmoid(value): return (math.exp(float(value))/((1+math.exp(float(value)))**2)) a.) Will this return
I want a code that will return a list like this : def list
I am reading this blog and came across following code RunAgain = Class.new(Exception) def
I came across this in a code review: def some_method(self, path): path = os.path.abspath(os.path.expanduser(path
I came across this code on reddit . I would have thought that type
in the book i'm learning from i came across this code snippit: while (i
I just came a cross this nice code that makes this scatter matrix plot:
Whilst working on some generally horrible Javascript code this morning, I came across the
I recently came along this line of code: CustomData_em_free_block(&em->vdata, &eve->data); And I thought, isn't:
Today I came across this question: you have a code static int counter =

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.