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] 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