Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test whether tuples have the same content

Tags:

python

I have to implement a function cmpT which should return the following results:

>>> cmpT((1, 2), (1, 2))
True
>>> cmpT((1, 2), (2, 1))
True
>>> cmpT((1, 2), (1, 2, 1))
False
>>> cmpT((1, 2), ())
False

My Code:

 def cmpT(t1, t2): 
    if t1 == t2:
        return True
    else:
        return False

It does not give the required output, cmpT((1, 2), (2, 1)) does not return True. What is wrong?

like image 490
kn3l Avatar asked Oct 24 '25 03:10

kn3l


1 Answers

You should check for each element if it is in both lists and the same number of times. The best solution is just sorting.

def cmpT(t1, t2): 
    return sorted(t1) == sorted(t2)

Have a look: http://codepad.org/PH6LrAvU

like image 143
amit Avatar answered Oct 26 '25 11:10

amit