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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:10:39+00:00 2026-05-11T03:10:39+00:00

Given this input: [1,2,3,4] I’d like to generate the set of spanning sets: [1]

  • 0

Given this input: [1,2,3,4]

I’d like to generate the set of spanning sets:

[1] [2] [3] [4] [1] [2] [3,4] [1] [2,3] [4] [1] [3] [2,4] [1,2] [3] [4] [1,3] [2] [4] [1,4] [2] [3] [1,2] [3,4] [1,3] [2,4] [1,4] [2,3] [1,2,3] [4] [1,2,4] [3] [1,3,4] [2] [2,3,4] [1] [1,2,3,4] 

Every set has all the elements of the original set, permuted to appear in unique subsets. What is the algorithm that produces these sets? I’ve tried Python generator functions using choose, permutation, combination, power set, and so on, but can’t get the right combination.

20 Jan 2009

This is not a homework question. This is an improved answer I was working on for http://www.projecteuler.net problem # 118. I already had a slow solution but came up with a better way — except I could not figure out how to do the spanning set.

I’ll post my code when I get back from an Inauguration Party.

21 Jan 2009

This is the eventual algorithm I used:

def spanningsets(items):     if len(items) == 1:         yield [items]     else:         left_set, last = items[:-1], [items[-1]]         for cc in spanningsets(left_set):             yield cc + [last]             for i,elem in enumerate(cc):                 yield cc[:i] + [elem + last] + cc[i+1:] 

@Yuval F: I know how to do a powerset. Here’s a straightforward implementation:

def powerset(s) :     length = len(s)     for i in xrange(0, 2**length) :         yield [c for j, c in enumerate(s) if (1 << j) & i]     return 
  • 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. 2026-05-11T03:10:39+00:00Added an answer on May 11, 2026 at 3:10 am

    This should work, though I haven’t tested it enough.

    def spanningsets(items):     if not items: return     if len(items) == 1:         yield [[items[-1]]]     else:         for cc in spanningsets(items[:-1]):             yield cc + [[items[-1]]]             for i in range(len(cc)):                 yield cc[:i] + [cc[i] + [items[-1]]] + cc[i+1:]  for sset in spanningsets([1, 2, 3, 4]):     print ' '.join(map(str, sset)) 

    Output:

    [1] [2] [3] [4] [1, 4] [2] [3] [1] [2, 4] [3] [1] [2] [3, 4] [1, 3] [2] [4] [1, 3, 4] [2] [1, 3] [2, 4] [1] [2, 3] [4] [1, 4] [2, 3] [1] [2, 3, 4] [1, 2] [3] [4] [1, 2, 4] [3] [1, 2] [3, 4] [1, 2, 3] [4] [1, 2, 3, 4] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 243k
  • Answers 243k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's only possible if you jailbreak your iPhone May 13, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer The datetime and timedelta class from the built-in datetime module… May 13, 2026 at 7:52 am
  • Editorial Team
    Editorial Team added an answer I tried to duplicate your error both by using the… May 13, 2026 at 7:52 am

Related Questions

Given this input: [1,2,3,4] I'd like to generate the set of spanning sets: [1]
By which I mean this: Given the input set of numbers: 1,2,3,4,5 becomes 1-5.
I have a CSV of file of data that I can load in R
The book I'm reading about Erlang has exercises in the back of it and
I'm pretty fresh to database handling, but now completely without experience. However, I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.