diff --git a/practice/dominoes/dominoes.py b/practice/dominoes/dominoes.py index d71e6a8..f85ed16 100644 --- a/practice/dominoes/dominoes.py +++ b/practice/dominoes/dominoes.py @@ -1,2 +1,61 @@ def can_chain(dominoes): - pass + """ + Determine if the given dominoes can be arranged in a chain. + + This function uses a depth-first search (DFS) algorithm to try to arrange + the dominoes in a chain where the numbers on the ends match and each + domino's numbers match the numbers on the ends of the adjacent dominoes. + + Args: + dominoes (list): A list of tuples where each tuple represents a domino + and contains two integers. + + Returns: + list: A list of tuples representing the chain if one exists, or an + empty list if no chain exists. + + Raises: + IndexError: An error occurs if the list is empty. + """ + def dfs(chain): + """ + Perform depth-first search to find a chain. + + Args: + chain (list): The current chain of dominoes. + + Returns: + list: A list of tuples representing the chain if one exists, or None if no chain exists. + """ + # If the list of dominoes is empty, return an empty list + if not dominoes: + return [] + + # If all dominoes are in the chain and the chain forms a loop, return the chain + if len(chain) == len(dominoes) and chain and chain[0][0] == chain[-1][1]: + return chain + + for i in range(len(dominoes)): + if visited[i]: + continue + + # If the chain is empty or the last number of the last domino in the chain matches the first number of the current domino + if not chain or chain[-1][1] == dominoes[i][0]: + visited[i] = True + result = dfs(chain + [dominoes[i]]) + if result: + return result + visited[i] = False + + # If the last number of the last domino in the chain matches the second number of the current domino + elif chain[-1][1] == dominoes[i][1]: + visited[i] = True + result = dfs(chain + [(dominoes[i][1], dominoes[i][0])]) + if result: + return result + visited[i] = False + + return None + + visited = [False] * len(dominoes) + return dfs([]) \ No newline at end of file