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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:50:37+00:00 2026-05-25T06:50:37+00:00

Write a function commonElements(a1, a2) that takes in 2 tuples as arguments and returns

  • 0

Write a function commonElements(a1, a2) that takes in 2 tuples as arguments and returns a sorted tuple containing elements that are found in both tuples.

My task is :

    >>> commonElements((1, 2, 3), (2, 5, 1))
    (1, 2)
    >>> commonElements((1, 2, 3, 'p', 'n'), (2, 5 ,1, 'p'))
    (1, 2, 'p')
    >>> commonElements((1, 3, 'p', 'n'), ('a', 2 , 5, 1, 'p'))
    (1, 'p')

I tried to do it like this.

def commonElements(a1, a2):
    return tuple(set(a1).intersection( set(a2) ))

Anyone know what my mistake is with the requirement?
I can not pass.

  • 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-25T06:50:38+00:00Added an answer on May 25, 2026 at 6:50 am

    Set is not ordered. So the order of the result may be arbitrary.
    I would come up with something like that:

    def commonElements(a1, a2):
        L = []
        for el in a1:
            if el in a2:
                L.append(el)
        return tuple(L)
    

    Please, note, that this way of solving the problem would get the output elements ordered as in the tuple a1. So, as mentioned in the comments, the more correct way to call it is ‘ordering’, not ‘sorting’.
    Also, it has a complexity of O(n*m), where n and m are the lengths of the lists a1 and a2 respectively.

    O(n*log(m)) can be achieved in this case if bisect module is used to access the elements of the second tuple a2 (which should be sorted before the proceeding).

    If sorting in common way is required, I would stick with your code, a bit altered:

    def commonElements(a1, a2):
        return tuple(sorted(set(a1).intersection(set(a2))))
    

    On the average it has a complexity of O(min(m+n)*log(min(n+m))) (because of sorting), and O(n*m) in the worst case because of intersection.

    If the code needs to be implemented without using set (for example for the purposes of study), here is the code:

    def commonElements(a1, a2):
        L = []
        for el in a1:
            if el in a2:
                L.append(el)
        L.sort()
        return tuple(L)
    

    Complexity is O(n*m).

    With using bisect the code would look this way:

    from bisect import bisect_left
    def commonElements(a1, a2):
       L = []
       a2.sort() #sort a2 to be able to use binary search in the internal loop thus changing the complexity from O(n^2) to O(n*log(n)) (assuming n and m are rather equal).
       a2_len = len(a2)
       for el in a1:
           i = bisect_left(a2, el)
           if i != a2_len and a2[i] == el:
               L.append(x)
       # L.sort() #uncomment this line if the list in sorted order is needed (not just ordered as the first lits; it's possible to sort a1 in the very beginning of the function, but that would be slower on the average since L is smaller on the average than a1 or a2 (since L is their intersection).
       return tuple(L)
    

    Complexity is O(n*log(m)).

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

Sidebar

Related Questions

I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a function that returns the nearest next power of 2
I need to write a function that takes 4 bytes as input, performs a
I need to write a function that returns all characters that occur 2 or
I want to write a function that accepts two objects as parameters and compare
I want to write a function that accepts a parameter which can be either
I wanted to write a function that would take an object and convert it
I want to write function that saves the content of tiny mce and destroys
I'm trying to write function similar to http://whatismyudid.com/ that, then approved, will return the

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.