Skip to content

Commit

Permalink
change testing to true/false
Browse files Browse the repository at this point in the history
  • Loading branch information
depial committed Jul 11, 2024
1 parent ff41e66 commit 1439c46
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 82 deletions.
8 changes: 3 additions & 5 deletions exercises/practice/dominoes/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions exercises/practice/dominoes/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -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
123 changes: 49 additions & 74 deletions exercises/practice/dominoes/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1439c46

Please sign in to comment.