Created
July 24, 2013 01:20
-
-
Save obikag/6067446 to your computer and use it in GitHub Desktop.
Function to compare tuples based on values within each tuple. Sequence of values in each tuple is not taken into consideration. This method returns true if all of the values in each of the tuples match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Method for comparing two tuples | |
def compare_tuple(tuple1,tuple2): | |
temp = [] | |
if len(tuple1) != len(tuple2): return False | |
for val1 in tuple1: | |
for val2 in tuple2: | |
if val1 == val2: | |
temp.append(val1) | |
break | |
return (len(temp) == (len(tuple1) and len(tuple2))) | |
# Test Section | |
t1 = (1,3,2,6,10) | |
t2 = (10,2,6,1,3) # Same as t1, different sequence | |
t3 = (1,3,2,6,10) # Same as t1 | |
t4 = (5,9,4,11,15) # Different tuple | |
t5 = (6,8,8,6) # Short tuple length | |
t6 = (1,3,5,6,10) # Similar to t1, one digit changed | |
# Start Test | |
print(compare_tuple(t1,t2)) # true | |
print(compare_tuple(t2,t3)) # true | |
print(compare_tuple(t3,t1)) # true | |
print(compare_tuple(t1,t4)) # false | |
print(compare_tuple(t1,t5)) # false | |
print(compare_tuple(t1,t6)) # false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment