Nov-01-2020, 05:32 PM
(This post was last modified: Nov-01-2020, 08:34 PM by deanhystad.)
Quote:Ts = [(45,50,30,21), (30,9,56,3), (56,10,17,4), (41,30,50,56)]The third tuple starts with 56 which also appears in the second tuple. If that shouldn't result in setting result[2] = True then I don't understand your tuple matching rule.
You may not understand "result" in the first example because there is an error. I messed up what True and False meant:
def find_matches(tuples): result = [False] # <- Was incorrectly set to True for i in range(len(tuples)-1): pair = list(set(tuples[i])) + list(set(tuples[i+1])) // Protect against duplicates in a list result.append(len(set(pair)) != len(pair)) # And corresponding error here. return result print(find_matches([(45,50,30,21), (30,9,56,3), (57,10,21,4), (41,30,50,56)]))What this program does is grab two consecutive tuples and add them together. First time through it creates a list from the first two tuples: (45,50,30,21) and (30,9,56,3) and calls this pair.
pair = [45,50,30,21,30,9,56,3]
Then it compares the length of pair to the lenght of set(pair). Creating a set from a list removes any duplicate values, so the only way len(pair) == len(set(pair)) is if all values in pair are unique.
pair = [45,50,30,21,30,9,56,3], set(pair) = {3,9,45,50,21,56,30}. len(pair) != len(set(pair))
I ran the code on a few different test cases and noticed it failed for tuples like this:
Ts = [(1,1,1,1), (2,2,2,2), (3,3,3,3), 4,4,4,4)]
The result for this should be [False, False, False, False] but it came up [False, True, True, True]. The reason is that set(tuple) removes all duplicates, so (1,1,1,1,2,2,2,2) becomes (1,2). To guard against this I need to remove all duplicates from each tuple before combining them in pair.
pair = list(set((1,1,1,1)) + list(set((2,2,2,2)) == [1] + [2] == [1, 2]