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, for example [(5,5)]
, True
should be returned otherwise False
, for example [(2,3)]
. this is the part which I'm having issues with is getting the program to detect when a single tuple is given and wether the values inside it are the same. Also there are empty domino pieces in the game as well, I want the program to return True if an empty list is given such as "[]".
This is what I have so far and it just returns None
when I try to run it for a single pair tuple. For multiple pairs of tuples, it works.
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)]))
You didn't account for one tile case
def cycle(tiles):
if len(tiles) == 0:
return True
if len(tiles) == 1:
return tiles[0][0] == tiles [0][1]
for i in range(len(tiles)-1):
if tiles[i][1] != tiles[i+1][0]:
return False
return True
print (cycle([(1,2),(2,3),(3,4)])) #True
print (cycle([(1,2),(5,3),(3,4)])) #False
print (cycle([(4,4)])) # True
print((cycle([(1,2),(2,3),(4,4)]))) #False
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, for example [(5,5)]
, True
should be returned otherwise False
, for example [(2,3)]
. this is the part which I'm having issues with is getting the program to detect when a single tuple is given and wether the values inside it are the same. Also there are empty domino pieces in the game as well, I want the program to return True if an empty list is given such as "[]".
This is what I have so far and it just returns None
when I try to run it for a single pair tuple. For multiple pairs of tuples, it works.
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)]))
If time is not a factor, run two for loops to test for domino:
In [1]: m = [(1,2),(2,3),(3,4)]
In [2]: n = [(1,2),(3,4),(7,6)]
In [3]: all([i[1] == j[0] for i, j in zip(m[:-1], m[1:])])
Out[3]: True
In [4]: all([i[1] == j[0] for i, j in zip(n[:-1], n[1:])])
Out[4]: False
So the function should be:
def cycle(M):
if not M:
return True #will return True for empty list
if len(M) == 1:
return M[0] == M[1]
return all(i[1] == j[0] for i, j in zip(M[:-1], M[1:]))
This should work:
dominoes=[(1,2),(1,3),(3,4),(4,5),(5,6)]
def isdomino(agg, item):
#print(agg, item)
if agg[0] == 1:
if item[0] == item[1]:
return 'True', ''
else:
return 'False', ''
else:
if agg[0] == 'False':
return 'False', ''
if agg[1] == item[0]:
return 'True', item[1]
return 'False', item[1]
def checkdominoes(dominoes):
if len(dominoes) == 0:
return ('True', '')
else:
return reduce(isdomino, dominoes, (len(dominoes),dominoes[0][0]))
dominoes=[(1,2),(1,3),(3,4),(4,5),(5,6)]
something = checkdominoes(dominoes)
eval(something[0])
# False
dominoes=[(1,2),(2,3),(3,4),(4,5),(5,6)]
something = checkdominoes(dominoes)
eval(something[0])
# True
dominoes=[(1,2)]
something = checkdominoes(dominoes)
eval(something[0])
# False
dominoes=[(2,2)]
something = checkdominoes(dominoes)
eval(something[0])
# True
dominoes=[]
something = checkdominoes(dominoes)
eval(something[0])
# True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.