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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With