From ff41e666a7ee97151ef0f650504ebb764b9e3b40 Mon Sep 17 00:00:00 2001 From: depial <91621102+depial@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:57:22 -0400 Subject: [PATCH 1/4] add new practice exercise dominoes --- config.json | 8 ++ .../practice/dominoes/.docs/instructions.md | 13 +++ exercises/practice/dominoes/.meta/config.json | 15 ++++ exercises/practice/dominoes/.meta/example.jl | 15 ++++ exercises/practice/dominoes/.meta/tests.toml | 49 +++++++++++ exercises/practice/dominoes/dominoes.jl | 3 + exercises/practice/dominoes/runtests.jl | 82 +++++++++++++++++++ 7 files changed, 185 insertions(+) create mode 100644 exercises/practice/dominoes/.docs/instructions.md create mode 100644 exercises/practice/dominoes/.meta/config.json create mode 100644 exercises/practice/dominoes/.meta/example.jl create mode 100644 exercises/practice/dominoes/.meta/tests.toml create mode 100644 exercises/practice/dominoes/dominoes.jl create mode 100644 exercises/practice/dominoes/runtests.jl diff --git a/config.json b/config.json index cfb29693..7f8de4a8 100644 --- a/config.json +++ b/config.json @@ -945,6 +945,14 @@ "practices": [], "prerequisites": [], "difficulty": 6 + }, + { + "slug": "dominoes", + "name": "Dominoes", + "uuid": "76e27165-47f5-4c4f-b747-c047872ff02c", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, diff --git a/exercises/practice/dominoes/.docs/instructions.md b/exercises/practice/dominoes/.docs/instructions.md new file mode 100644 index 00000000..1ced9f64 --- /dev/null +++ b/exercises/practice/dominoes/.docs/instructions.md @@ -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. diff --git a/exercises/practice/dominoes/.meta/config.json b/exercises/practice/dominoes/.meta/config.json new file mode 100644 index 00000000..227a6ba2 --- /dev/null +++ b/exercises/practice/dominoes/.meta/config.json @@ -0,0 +1,15 @@ +{ + "authors": [], + "files": { + "solution": [ + "dominoes.jl" + ], + "test": [ + "runtests.jl" + ], + "example": [ + ".meta/example.jl" + ] + }, + "blurb": "Make a chain of dominoes." +} diff --git a/exercises/practice/dominoes/.meta/example.jl b/exercises/practice/dominoes/.meta/example.jl new file mode 100644 index 00000000..e8028ab4 --- /dev/null +++ b/exercises/practice/dominoes/.meta/example.jl @@ -0,0 +1,15 @@ +function dominoes(stones) + function dfs(stones, chain, indices) + length(indices) == length(stones) && first(first(chain)) == last(last(chain)) && return chain + for (i, stone) in enumerate(stones) + if i ∉ indices && last(last(chain)) ∈ stone + check = dfs(stones, push!(chain, last(last(chain)) == first(stone) ? stone : reverse(stone)), push!(indices, i)) + isa(check, Vector) && return check + delete!(indices, i) + pop!(chain) + end + end + false + end + isempty(stones) ? [] : dfs(stones, [first(stones)], Set(1)) +end diff --git a/exercises/practice/dominoes/.meta/tests.toml b/exercises/practice/dominoes/.meta/tests.toml new file mode 100644 index 00000000..08c8e08d --- /dev/null +++ b/exercises/practice/dominoes/.meta/tests.toml @@ -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" diff --git a/exercises/practice/dominoes/dominoes.jl b/exercises/practice/dominoes/dominoes.jl new file mode 100644 index 00000000..9f2b51c4 --- /dev/null +++ b/exercises/practice/dominoes/dominoes.jl @@ -0,0 +1,3 @@ +function dominoes(stones) + # Your code here +end diff --git a/exercises/practice/dominoes/runtests.jl b/exercises/practice/dominoes/runtests.jl new file mode 100644 index 00000000..6915d6a5 --- /dev/null +++ b/exercises/practice/dominoes/runtests.jl @@ -0,0 +1,82 @@ +using Test + +include("dominoes.jl") + +@testset verbose = true "tests" begin + function runtestset() + @testset "empty input = empty output" begin + @test dominoes([]) == [] + end + + @testset "singleton input = singleton output" begin + chain = dominoes([1, 1]) + @test isvalidchain(chain, true) + end + + @testset "singleton that can't be chained" begin + chain = dominoes([1, 2]) + @test isvalidchain(chain, false) + end + + @testset "three elements" begin + chain = dominoes([[1, 2], [3, 1], [2, 3]]) + @test isvalidchain(chain, true) + end + + @testset "can reverse dominoes" begin + chain = dominoes([[1, 2], [1, 3], [2, 3]]) + @test isvalidchain(chain, true) + end + + @testset "can't be chained" begin + chain = dominoes([[1, 2], [4, 1], [2, 3]]) + @test isvalidchain(chain, false) + end + + @testset "disconnected - simple" begin + chain = dominoes([[1, 1], [2, 2]]) + @test isvalidchain(chain, false) + end + + @testset "disconnected - double loop" begin + chain = dominoes([[1, 2], [2, 1], [3, 4], [4, 3]]) + @test isvalidchain(chain, false) + end + + @testset "disconnected - single isolated" begin + chain = dominoes([[1, 2], [2, 3], [3, 1], [4, 4]]) + @test isvalidchain(chain, false) + end + + @testset "need backtrack" begin + chain = dominoes([[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]]) + @test isvalidchain(chain, true) + end + + @testset "separate loops" begin + chain = dominoes([[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]]) + @test isvalidchain(chain, true) + end + + @testset "nine elements" begin + chain = dominoes([[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]]) + @test isvalidchain(chain, true) + end + + @testset "separate three-domino loops" begin + chain = dominoes([[1, 2], [2, 3], [3, 1], [4, 5], [5, 6], [6, 4]]) + @test isvalidchain(chain, false) + end + end + + function isvalidchain(chain, valid) + !valid && return isa(chain, Bool) ? !chain : false + first(first(chain)) != last(last(chain)) && return false + for i in 1:length(chain)-1 + last(chain[i]) != first(chain[i+1]) && return false + end + true + end + + runtestset() +end From 1439c468b3a1f7bc7eb923b4f10dab6aee3a6118 Mon Sep 17 00:00:00 2001 From: depial <91621102+depial@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:31:13 -0400 Subject: [PATCH 2/4] change testing to true/false --- .../practice/dominoes/.docs/instructions.md | 8 +- exercises/practice/dominoes/.meta/example.jl | 6 +- exercises/practice/dominoes/runtests.jl | 123 +++++++----------- 3 files changed, 55 insertions(+), 82 deletions(-) diff --git a/exercises/practice/dominoes/.docs/instructions.md b/exercises/practice/dominoes/.docs/instructions.md index 1ced9f64..f73007c0 100644 --- a/exercises/practice/dominoes/.docs/instructions.md +++ b/exercises/practice/dominoes/.docs/instructions.md @@ -2,12 +2,10 @@ 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. +Compute whether it is possible 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 example given the stones `[2|1]`, `[2|3]` and `[1|3]` you could 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. This would return `true`. -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 +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`, so this would return `false`. Some test cases may use duplicate stones in a chain solution, assume that multiple Domino sets are being used. diff --git a/exercises/practice/dominoes/.meta/example.jl b/exercises/practice/dominoes/.meta/example.jl index e8028ab4..9f9802ec 100644 --- a/exercises/practice/dominoes/.meta/example.jl +++ b/exercises/practice/dominoes/.meta/example.jl @@ -1,15 +1,15 @@ function dominoes(stones) function dfs(stones, chain, indices) - length(indices) == length(stones) && first(first(chain)) == last(last(chain)) && return chain + length(indices) == length(stones) && first(first(chain)) == last(last(chain)) && return true for (i, stone) in enumerate(stones) if i ∉ indices && last(last(chain)) ∈ stone check = dfs(stones, push!(chain, last(last(chain)) == first(stone) ? stone : reverse(stone)), push!(indices, i)) - isa(check, Vector) && return check + check && return true delete!(indices, i) pop!(chain) end end false end - isempty(stones) ? [] : dfs(stones, [first(stones)], Set(1)) + isempty(stones) || dfs(stones, [first(stones)], Set(1)) end diff --git a/exercises/practice/dominoes/runtests.jl b/exercises/practice/dominoes/runtests.jl index 6915d6a5..4b2ea031 100644 --- a/exercises/practice/dominoes/runtests.jl +++ b/exercises/practice/dominoes/runtests.jl @@ -3,80 +3,55 @@ using Test include("dominoes.jl") @testset verbose = true "tests" begin - function runtestset() - @testset "empty input = empty output" begin - @test dominoes([]) == [] - end - - @testset "singleton input = singleton output" begin - chain = dominoes([1, 1]) - @test isvalidchain(chain, true) - end - - @testset "singleton that can't be chained" begin - chain = dominoes([1, 2]) - @test isvalidchain(chain, false) - end - - @testset "three elements" begin - chain = dominoes([[1, 2], [3, 1], [2, 3]]) - @test isvalidchain(chain, true) - end - - @testset "can reverse dominoes" begin - chain = dominoes([[1, 2], [1, 3], [2, 3]]) - @test isvalidchain(chain, true) - end - - @testset "can't be chained" begin - chain = dominoes([[1, 2], [4, 1], [2, 3]]) - @test isvalidchain(chain, false) - end - - @testset "disconnected - simple" begin - chain = dominoes([[1, 1], [2, 2]]) - @test isvalidchain(chain, false) - end - - @testset "disconnected - double loop" begin - chain = dominoes([[1, 2], [2, 1], [3, 4], [4, 3]]) - @test isvalidchain(chain, false) - end - - @testset "disconnected - single isolated" begin - chain = dominoes([[1, 2], [2, 3], [3, 1], [4, 4]]) - @test isvalidchain(chain, false) - end - - @testset "need backtrack" begin - chain = dominoes([[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]]) - @test isvalidchain(chain, true) - end - - @testset "separate loops" begin - chain = dominoes([[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]]) - @test isvalidchain(chain, true) - end - - @testset "nine elements" begin - chain = dominoes([[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]]) - @test isvalidchain(chain, true) - end - - @testset "separate three-domino loops" begin - chain = dominoes([[1, 2], [2, 3], [3, 1], [4, 5], [5, 6], [6, 4]]) - @test isvalidchain(chain, false) - end + @testset "empty input = empty output" begin + @test dominoes([]) == true end - - function isvalidchain(chain, valid) - !valid && return isa(chain, Bool) ? !chain : false - first(first(chain)) != last(last(chain)) && return false - for i in 1:length(chain)-1 - last(chain[i]) != first(chain[i+1]) && return false - end - true + + @testset "singleton input = singleton output" begin + @test dominoes([1, 1]) == true + end + + @testset "singleton that can't be chained" begin + @test dominoes([1, 2]) == false + end + + @testset "three elements" begin + @test dominoes([[1, 2], [3, 1], [2, 3]]) == true + end + + @testset "can reverse dominoes" begin + @test dominoes([[1, 2], [1, 3], [2, 3]]) == true + end + + @testset "can't be chained" begin + @test dominoes([[1, 2], [4, 1], [2, 3]]) == false + end + + @testset "disconnected - simple" begin + @test dominoes([[1, 1], [2, 2]]) == false + end + + @testset "disconnected - double loop" begin + @test dominoes([[1, 2], [2, 1], [3, 4], [4, 3]]) == false + end + + @testset "disconnected - single isolated" begin + @test dominoes([[1, 2], [2, 3], [3, 1], [4, 4]]) == false + end + + @testset "need backtrack" begin + @test dominoes([[1, 2], [2, 3], [3, 1], [2, 4], [2, 4]]) == true + end + + @testset "separate loops" begin + @test dominoes([[1, 2], [2, 3], [3, 1], [1, 1], [2, 2], [3, 3]]) == true + end + + @testset "nine elements" begin + @test dominoes([[1, 2], [5, 3], [3, 1], [1, 2], [2, 4], [1, 6], [2, 3], [3, 4], [5, 6]]) == true + end + + @testset "separate three-domino loops" begin + @test dominoes([[1, 2], [2, 3], [3, 1], [4, 5], [5, 6], [6, 4]]) == false end - - runtestset() end From ed828fcc59bd11eca326aaee4ab2a6bc2b3f8e6d Mon Sep 17 00:00:00 2001 From: depial <91621102+depial@users.noreply.github.com> Date: Fri, 12 Jul 2024 04:21:18 -0400 Subject: [PATCH 3/4] revert changes to instructions and add instruction.append.md --- exercises/practice/dominoes/.docs/instructions.append.md | 2 ++ exercises/practice/dominoes/.docs/instructions.md | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 exercises/practice/dominoes/.docs/instructions.append.md diff --git a/exercises/practice/dominoes/.docs/instructions.append.md b/exercises/practice/dominoes/.docs/instructions.append.md new file mode 100644 index 00000000..b153eca3 --- /dev/null +++ b/exercises/practice/dominoes/.docs/instructions.append.md @@ -0,0 +1,2 @@ +### Note: +In this version of the exercise, you are required to return `true` or `false` if a valid chain is possible or not. \ No newline at end of file diff --git a/exercises/practice/dominoes/.docs/instructions.md b/exercises/practice/dominoes/.docs/instructions.md index f73007c0..1ced9f64 100644 --- a/exercises/practice/dominoes/.docs/instructions.md +++ b/exercises/practice/dominoes/.docs/instructions.md @@ -2,10 +2,12 @@ Make a chain of dominoes. -Compute whether it is possible 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. +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 could 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. This would return `true`. +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`, so this would return `false`. +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. From 8c48100ec21125940ce535cd933424550c66e84c Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 12 Jul 2024 11:12:03 +0200 Subject: [PATCH 4/4] Update exercises/practice/dominoes/.docs/instructions.append.md --- exercises/practice/dominoes/.docs/instructions.append.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/dominoes/.docs/instructions.append.md b/exercises/practice/dominoes/.docs/instructions.append.md index b153eca3..8f19767c 100644 --- a/exercises/practice/dominoes/.docs/instructions.append.md +++ b/exercises/practice/dominoes/.docs/instructions.append.md @@ -1,2 +1,2 @@ -### Note: +# Instructions append In this version of the exercise, you are required to return `true` or `false` if a valid chain is possible or not. \ No newline at end of file