Could anybody help me with my solution of Dominoes exam (section Python)? In Pycharm, everything works well but when I try to submit, 7 from 13 tests are not passed. My I understood something wrong? Or using importing “copy” is not allowed? I understand that my code is cumbersome due to frequent use of deepcopy, but it should not influence the functionality.
from copy import deepcopy
def search_number_twin(number, dominoes_list, solution_list, alternate_list):
_dominoes_list = deepcopy(dominoes_list)
found = False
next_element = []
for element in _dominoes_list:
for side in range(2):
if element[side] == number:
if found == False:
found = True
next_element = deepcopy(element)
if side == 1:
next_element.reverse()
dominoes_list.remove(element)
else:
new_alternate = [deepcopy(solution_list)]
new_element = deepcopy(element)
if side == 1:
new_element.reverse()
new_alternate[0].append(new_element)
new_dominoes_alternate = deepcopy(_dominoes_list)
new_dominoes_alternate.remove(element)
new_alternate.append(new_dominoes_alternate)
alternate_list.append(new_alternate)
return next_element
def can_chain(dominoes):
if dominoes == []:
return []
exists = True
dominoes_list = []
last_element = list(dominoes[0])
another_last_element = deepcopy(last_element)
another_last_element.reverse()
solution_list = [last_element]
dominoes.remove(dominoes[0])
for domino in dominoes:
dominoes_list.append(list(domino))
alternate_list = [[[another_last_element], deepcopy(dominoes_list)]]
while dominoes_list != []:
next_element = search_number_twin(last_element[1], dominoes_list, solution_list, alternate_list)
if next_element != []:
last_element = deepcopy(next_element)
solution_list.append(deepcopy(next_element))
elif len(alternate_list) > 0:
solution_list = alternate_list[len(alternate_list) - 1][0]
last_element = solution_list[len(solution_list) - 1]
dominoes_list = alternate_list[len(alternate_list) - 1][1]
alternate_list.remove(alternate_list[len(alternate_list) - 1])
else:
print("Solution does not exist.")
exists = False
break
if exists:
solution = []
for element in solution_list:
solution.append(tuple(element))
return solution