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

  • Home
  • SEARCH
  • 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 1102223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:09:10+00:00 2026-05-17T01:09:10+00:00

I have three sets: s0 = [set([16,9,2,10]), set([16,14,22,15]), set([14,7])] # true, 16 and 14

  • 0

I have three sets:

s0 = [set([16,9,2,10]), set([16,14,22,15]), set([14,7])]   # true, 16 and 14
s1 = [set([16,9,2,10]), set([16,14,22,15]), set([7,8])]    # false

I want a function that will return True if every set in the list intersects with at least one other set in the list. Is there a built-in for this or a simple list comprehension?

  • 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-05-17T01:09:11+00:00Added an answer on May 17, 2026 at 1:09 am

    It’s a little verbose but I think it’s a pretty efficient solution. It takes advantage of the fact that when two sets intersect, we can mark them both as connected. It does this by keeping a list of flags as long as the list of sets. when set i and set j intersect, it sets the flag for both of them. It then loops over the list of sets and only tries to find a intersection for sets that haven’t already been intersected. After reading the comments, I think this is what @Victor was talking about.

    s0 = [set([16,9,2,10]), set([16,14,22,15]), set([14,7])]   # true, 16 and 14
    s1 = [set([16,9,2,10]), set([16,14,22,15]), set([7,8])]    # false
    
    
    def connected(sets):
        L = len(sets)
    
        if not L: return True
        if L == 1: return False
    
        passed = [False] * L
        i = 0
        while True:
            while passed[i]: 
                i += 1
                if i == L: 
                    return True
    
            for j, s in enumerate(sets):
                if j == i: continue
                if sets[i] & s: 
                    passed[i] = passed[j] = True
                    break
            else:
                return False
    
    
    print connected(s0)
    print connected(s1)
    

    I decided that an empty list of sets is connected (If you produce an element of the list, I can produce an element that it intersects ;). A list with only one element is dis-connected trivially. It’s one line to change in either case if you disagree.

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

Sidebar

Related Questions

I have three unordered lists that have been created as Scriptaculous Sortables so that
I have three TextBox controls on the page <asp:TextBox ID=TextBox1 runat=server AutoPostBack=True OnTextChanged=TextBox_TextChanged TabIndex=1>
I have a windows form that sets the text property in a textbox to
I hope the title makes sense. I have a set of items that I
I have this function: function disableDiv(divId, action){ var divId = byId(divId); if(action==true){ divId.style.display='none'; }
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have three tables tag , page , pagetag With the data below page
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three (C++) classes: Player, Hand, and Card. Player has a member, hand,
I have three divs : <div id=login /> <div id=content /> <div id=menu />

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.