lundi 18 novembre 2019

How to detect specific parts of a tuple?

Working on a homework question which requires me to make a program which takes a set of tuple and detects if it counts as a domino cycle. For example, if given the tuples [(1,2),(2,3),(3,4)] , the program would return True. If an example like [(1,2),(3,4),(7,6)] is given it would return false. Also if only one set of tuple is given but both values within it is the same[(5,5)], True should be returned otherwise False[(2,3)]. this is the part which I'm having issues with is getting the program to detect when a single pair of tuple is given and wether the values inside it are the same.

This is what I have so far and it just returns none when I try to run it for a single pair tuple.

def cycle(tiles):


for i in range(len(tiles)-1):
    tile1 = tiles[i]
    tile2 = tiles [i+1]

    if tile1[1:] == tile2[0:1] or ((tile1 and not tile2) or (not tile1 and not tile2)):
        return True
    else:
        return False


print (cycle([(4,4)]))

Aucun commentaire:

Enregistrer un commentaire