Hello I found some bugs in Dominoes.
Tests 4 and 5 don’t take any inputs so their correct assert is “” while it should not work like that. Please take a look into this.
Which test function in the test file? Which line of the test file?
There are a lot of existing solutions (about 800) which pass all the tests just fine so chances are high that the tests are correct.
Code Run
input_dominoes = [(1, 2), (3, 1), (2, 3)]
output_chain = can_chain(input_dominoes)
self.assert_correct_chain(input_dominoes, output_chain)
Test Failure
AssertionError: unexpectedly None : There should be a chain for
As you can see list in assertion is empty. I’m not sure what makes that happen but I can assure you that my code works fine. Maybe bug’s mine only however I hope it could be fixed in some way
The test says your code returns None for that input and that it ought to report that [(1, 2), (3, 1), (2, 3)] is a valid chain.
That test looks correct to me! What does your code return for that input? Would you like to share your code?
def link(dominoes):
linked={}
for stone in dominoes:
matches=[]
for i in range (0,len(dominoes)):
if stone[0] in dominoes[i] or stone[1] in dominoes[i]:
matches.append(dominoes[i])
linked[stone]=matches
return linked
def reverse_stone(stone,links):
new_stone=(stone[1],stone[0])
for key in links:
if stone in links[key]:
links[key].remove(stone)
links[key].append(new_stone)
return new_stone
def can_chain(dominoes):
result=[x]
length=len(dominoes)
if length==0:
return []
if length==1:
stone=dominoes[0]
if stone[0]==stone[1]:
return stone
else:
return None
links=link(dominoes)
result.append(dominoes.pop(0))
while dominoes:
stone=result[-1]
x=dominoes.pop(0)
if x in links[stone]:
if x[0]==stone[1]:
result.append(x)
else:
dominoes.append(x)
else:
dominoes.append(x)
if x in links[stone] and x[0]!=stone[1]:
x=reverse_stone(x,links)
dominoes.pop()
dominoes.append(x)
if x not in links[stone]:
return None
if result[0][0]==result[-1][1]:
return result
else:
return None
with same input dominoes, running my code manually gives output
[(1, 2), (2, 3), (3, 1)]
meanwhile, in exercism I get
AssertionError: Lists differ: != [(1, 2), (1, 3), (2, 3)]
Second list contains 3 additional elements.
First extra element 0:
(1, 2)
- [(1, 2), (1, 3), (2, 3)] : Dominoes used in the output must be the same as the ones given in the input
If you edit your post and put your code in a codeblock, it will be much easier to read.
You’re modifying the callers data here, which alters the data the test is using. You should never modify the caller’s data unless that’s explicitly wanted. Try changing the code not to modify the caller’s data and see if that helps.
It helped, thank you so much