Back to Freecodecamp

Challenge 214: Domino Chain Validator

curriculum/challenges/english/blocks/daily-coding-challenges-python/699c8e045ee7cb94ed2322d5.md

latest2.0 KB
Original Source

--description--

Given a 2D array representing a sequence of dominoes, determine whether it forms a valid chain.

  • Each element in the array represents a domino and will be an array of two numbers from 1 to 6, (inclusive).
  • For the chain to be valid, the second number of each domino must match the first number of the next domino.
  • The first number of the first domino and the last number of the last domino don't need to match anything.

--hints--

is_valid_domino_chain([[1, 3], [3, 6], [6, 5]]) should return True.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_domino_chain([[1, 3], [3, 6], [6, 5]]), True)`)
}})

is_valid_domino_chain([[6, 2], [3, 4], [4, 1]]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_domino_chain([[6, 2], [3, 4], [4, 1]]), False)`)
}})

is_valid_domino_chain([[2, 5], [5, 6], [5, 1]]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_domino_chain([[2, 5], [5, 6], [5, 1]]), False)`)
}})

is_valid_domino_chain([[4, 3], [3, 1], [1, 6], [6, 6], [6, 5], [5, 1], [1, 1], [1, 4], [4, 4], [4, 2]]) should return True.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_domino_chain([[4, 3], [3, 1], [1, 6], [6, 6], [6, 5], [5, 1], [1, 1], [1, 4], [4, 4], [4, 2]]), True)`)
}})

is_valid_domino_chain([[2, 3], [3, 3], [3, 6], [6, 1], [1, 4], [3, 5], [5, 5], [5, 4], [4, 2], [2, 2]]) should return False.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_domino_chain([[2, 3], [3, 3], [3, 6], [6, 1], [1, 4], [3, 5], [5, 5], [5, 4], [4, 2], [2, 2]]), False)`)
}})

--seed--

--seed-contents--

py
def is_valid_domino_chain(dominoes):

    return dominoes

--solutions--

py
def is_valid_domino_chain(dominoes):
    for i in range(len(dominoes) - 1):
        if dominoes[i][1] != dominoes[i + 1][0]:
            return False

    return True