Skip to content

Commit

Permalink
[New Exercise]: Dominoes (#275)
Browse files Browse the repository at this point in the history
I leave the difficulty at 5 atm
  • Loading branch information
glaxxie authored Sep 22, 2023
1 parent 3e8476b commit d7f815a
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "dominoes",
"name": "Dominoes",
"uuid": "c3ba77dc-ca14-4d76-b449-f9cabad16d00",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
Expand Down
13 changes: 13 additions & 0 deletions exercises/practice/dominoes/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Make a chain of dominoes.

Compute a way to order a given set of dominoes in such a way that they form a correct domino chain (the dots on one half of a stone match the dots on the neighboring half of an adjacent stone) and that dots on the halves of the stones which don't have a neighbor (the first and last stone) match each other.

For example given the stones `[2|1]`, `[2|3]` and `[1|3]` you should compute something
like `[1|2] [2|3] [3|1]` or `[3|2] [2|1] [1|3]` or `[1|3] [3|2] [2|1]` etc, where the first and last numbers are the same.

For stones `[1|2]`, `[4|1]` and `[2|3]` the resulting chain is not valid: `[4|1] [1|2] [2|3]`'s first and last numbers are not the same.
4 != 3

Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used.
60 changes: 60 additions & 0 deletions exercises/practice/dominoes/.meta/Dominoes.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Function Test-DominoesChain() {
<#
.SYNOPSIS
Implement a function to test if a dominoes chain can be made.
.DESCRIPTION
Given a list of dominoes, check to see if they can form a chain.
A correct chain mean the dots on one half of a stone match the dots on the neighboring half of an adjacent stone,
and that dots on the halves of the stones which don't have a neighbor (the first and last stone) match each other.
.PARAMETER Dominoes
A list of 2-elements arrays, each array represent a domino.
.EXAMPLE
Test-DominoesChain -Dominoes @( @(1, 5), @(5, 6), @(6, 1))
Return: true
#>
[CmdletBinding()]
Param(
[Collections.Generic.List[int[]]]$Dominoes
)
if ($Dominoes.Count -eq 0) {
return $true
}

$len = $Dominoes.Count
[Collections.Generic.List[Int[]]]$chain = $Dominoes[0]
$Dominoes.RemoveAt(0)

Backtrack $Dominoes $chain $len
}


Function Backtrack($dominoes, $chain, $len) {
<#
.DESCRIPTION
Helper function to backtrack solutions
#>
if ($chain.Count -eq $len) {
return $chain[0][0] -eq $chain[-1][1]
}

$tail = $chain[-1][1]

for ($i = 0; $i -lt $dominoes.Count; $i++) {
$left_pip, $right_pip = $dominoes[$i]
if ($tail -in $dominoes[$i]) {
$domino = $right_pip -eq $tail ? @($right_pip, $left_pip) : $dominoes[$i]
$chain.Add($domino)
$dominoes.RemoveAt($i)
if (Backtrack $dominoes $chain $len) {
return $true
}else {
$chain.RemoveAt($chain.Count -1)
$dominoes.Insert($i, $domino)
}
}
}
return $false
}
17 changes: 17 additions & 0 deletions exercises/practice/dominoes/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"glaxxie"
],
"files": {
"solution": [
"Dominoes.ps1"
],
"test": [
"Dominoes.tests.ps1"
],
"example": [
".meta/Dominoes.example.ps1"
]
},
"blurb": "Make a chain of dominoes."
}
49 changes: 49 additions & 0 deletions exercises/practice/dominoes/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[31a673f2-5e54-49fe-bd79-1c1dae476c9c]
description = "empty input = empty output"

[4f99b933-367b-404b-8c6d-36d5923ee476]
description = "singleton input = singleton output"

[91122d10-5ec7-47cb-b759-033756375869]
description = "singleton that can't be chained"

[be8bc26b-fd3d-440b-8e9f-d698a0623be3]
description = "three elements"

[99e615c6-c059-401c-9e87-ad7af11fea5c]
description = "can reverse dominoes"

[51f0c291-5d43-40c5-b316-0429069528c9]
description = "can't be chained"

[9a75e078-a025-4c23-8c3a-238553657f39]
description = "disconnected - simple"

[0da0c7fe-d492-445d-b9ef-1f111f07a301]
description = "disconnected - double loop"

[b6087ff0-f555-4ea0-a71c-f9d707c5994a]
description = "disconnected - single isolated"

[2174fbdc-8b48-4bac-9914-8090d06ef978]
description = "need backtrack"

[167bb480-dfd1-4318-a20d-4f90adb4a09f]
description = "separate loops"

[cd061538-6046-45a7-ace9-6708fe8f6504]
description = "nine elements"

[44704c7c-3adb-4d98-bd30-f45527cf8b49]
description = "separate three-domino loops"
22 changes: 22 additions & 0 deletions exercises/practice/dominoes/Dominoes.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Function Test-DominoesChain() {
<#
.SYNOPSIS
Implement a function to test if a dominoes chain can be made.
.DESCRIPTION
Given a list of dominoes, check to see if they can form a chain.
A correct chain mean the dots on one half of a stone match the dots on the neighboring half of an adjacent stone,
and that dots on the halves of the stones which don't have a neighbor (the first and last stone) match each other.
.PARAMETER Dominoes
A list of 2-elements arrays, each array represent a domino.
.EXAMPLE
Test-DominoesChain -Dominoes @( @(1, 5), @(5, 6), @(6, 1))
Return: true
#>
[CmdletBinding()]
Param(
[Collections.Generic.List[int[]]]$Dominoes
)
}
83 changes: 83 additions & 0 deletions exercises/practice/dominoes/Dominoes.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
BeforeAll {
. "./Dominoes.ps1"
}

Describe "Dominoes test cases" {
It "empty input = empty output" {
$got = Test-DominoesChain -Dominoes @( )

$got | Should -BeTrue
}

It "singleton input = singleton output" {
$got = Test-DominoesChain -Dominoes @( ,@(1, 1) )

$got | Should -BeTrue
}

It "singleton that can't be chained" {
$got = Test-DominoesChain -Dominoes @( ,@(1, 2) )

$got | Should -BeFalse
}

It "three elements" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(3, 1), @(2, 3) )

$got | Should -BeTrue
}

It "can reverse dominoes" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(1, 3), @(2, 3) )

$got | Should -BeTrue
}

It "can't be chained" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(4, 1), @(2, 3) )

$got | Should -BeFalse
}

It "disconnected - simple" {
$got = Test-DominoesChain -Dominoes @( @(1, 1), @(2, 2) )

$got | Should -BeFalse
}

It "disconnected - double loop" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(2, 1), @(3, 4), @(4, 3) )

$got | Should -BeFalse
}

It "disconnected - single isolated" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(2, 3), @(3, 1), @(4, 4) )

$got | Should -BeFalse
}

It "need backtrack" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(2, 3), @(3, 1), @(2, 4), @(2, 4) )

$got | Should -BeTrue
}

It "separate loops" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(2, 3), @(3, 1), @(1, 1), @(2, 2), @(3, 3) )

$got | Should -BeTrue
}

It "nine elements" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(5, 3), @(3, 1), @(1, 2), @(2, 4), @(1, 6), @(2,3), @(3, 4), @(5, 6) )

$got | Should -BeTrue
}

It "separate three-domino loops" {
$got = Test-DominoesChain -Dominoes @( @(1, 2), @(2, 3), @(3, 1), @(4, 5), @(5, 6), @(6, 4) )

$got | Should -BeFalse
}
}

0 comments on commit d7f815a

Please sign in to comment.