Here is an updated version of what I came up with (which suits your use cases):
def correspond(a, b): """Looks at two lists, if they are the same length and the length is even then it looks to make sure that the pairs are swapped (even if they are moved) >>> print correspond([1,2,3,4], [2,1,4,3]) True >>> print correspond([1,2,3,4], [2,1,4,5]) #One value is out of place False >>> print correspond([1,2,3,4], [2,1,3]) #One value list is shorter False >>> print correspond([1,2,3,4], [3,4,1,2]) #values are moved but not swapped False >>> print correspond("ABCD", "BADC") True """ if len(a) == len(b) and len(a) % 2 == 0: try: for i in xrange(0,len(a),2): if (1+b.index(a[i])) == b.index(a[i+1]): return False return True except ValueError: return False else: return False if __name__ == "__main__": import doctest doctest.testmod()
Jason sperske
source share