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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:44:24+00:00 2026-06-14T09:44:24+00:00

Don’t know how to explain the problem in a title. Here’s the problem: Assume

  • 0

Don’t know how to explain the problem in a title.

Here’s the problem:

Assume we have 4 group:

(a, b, c, d)
(e, f)
(g, h, i)
(j, k, l, m, n)

Now I’m given a 4 elements tuple, for example (a, e, h, m), neither 2 comes from one group, so I return True. If given (a, b, e, g), then a, b are from one group, return False.

Then here’s my current idea, I give each element an id start with group number and test for duplicate.

g1 = ['1a', '1b', '1c', '1d']
g2 = ['2e', '2f']
g3 = ['3g', '3h', '3i']
g4 = ['4j', '4k', '4l', '4m', '4n']


def test(elements):
    if len(elements) != 4:
        return False

    stack = []
    for e in elements:
        mark = e[:1]
        if mark in stack:
            return False
        stack.append(mark)

    ga = set(g1 + g2 + g3 + g4)
    return set(elements).issubset(ga)


print test(('1a', '1b', '2e', '3g'))
print test(('1a', '2e', '3g', '4m'))

But I think the string compare is not a very elegant solution, can this be done by another faster algorithm?

  • 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-14T09:44:25+00:00Added an answer on June 14, 2026 at 9:44 am

    If all the elements are hashable, I would use a set.intersection:

    g1 = set(['1a', '1b', '1c', '1d'])
    g2 = set(['2e', '2f'])
    g3 = set(['3g', '3h', '3i'])
    g4 = set(['4j', '4k', '4l', '4m', '4n'])
    
    sets = [g1,g2,g3,g4]
    test_this = ['1a','2e','3g','4j']
    
    all(len(s.intersection(test_this)) <= 1 for s in sets)
    

    Alternatively, if you don’t want to change the type of g1,g2 … you can change the type of test_this:

    g1 = ['1a', '1b', '1c', '1d']
    g2 = ['2e', '2f']
    g3 = ['3g', '3h', '3i']
    g4 = ['4j', '4k', '4l', '4m', '4n']
    lists = [g1,g2,g3,g4]
    test_this = set(['1a','2e','3g','4j'])
    
    all( len(test_this.intersection(lst)) <= 1 for lst in lists )
    

    The beauty here is that all is smart enough to short-circuit — and since we’re using a generator expression, we don’t need to calculate all the intersections up front. Python will only keep calculating the intersections as long as all the previous intersections had length less than or equal to 1.

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know a better title but here is what im trying to do. I
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know if I'm missing something here. I'd like to run multiple global variables
I don't know how better to describe this. So take this example. var a
Don't know what's wrong here, when I run the application it says Specified method
Don't know how to explain it better but i'm trying to get a response
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if this has been answered before. Have custom routes to users. If

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.