aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-242/barroff/julia/ch-1.jl14
-rw-r--r--challenge-242/barroff/julia/ch-2.jl13
-rw-r--r--challenge-242/barroff/nim/ch_1.nim28
-rw-r--r--challenge-242/barroff/nim/ch_2.nim24
-rw-r--r--challenge-242/barroff/raku/ch-1.p622
-rw-r--r--challenge-242/barroff/raku/ch-2.p620
6 files changed, 121 insertions, 0 deletions
diff --git a/challenge-242/barroff/julia/ch-1.jl b/challenge-242/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..fbbce444e4
--- /dev/null
+++ b/challenge-242/barroff/julia/ch-1.jl
@@ -0,0 +1,14 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function missing_members(arr1::Array{T}, arr2::Array{T}) where {T<:Integer}
+ set1 = Set{T}(arr1)
+ set2 = Set{T}(arr2)
+ return (setdiff(set1, set2), setdiff(set2, set1))
+end
+
+@testset "missing members" begin
+ @test missing_members([1, 2, 3], [2, 4, 6]) == (Set([1, 3]), Set([4, 6]))
+ @test missing_members([1, 2, 3, 3], [1, 1, 2, 2]) == (Set([3]), Set{Int64}())
+end
diff --git a/challenge-242/barroff/julia/ch-2.jl b/challenge-242/barroff/julia/ch-2.jl
new file mode 100644
index 0000000000..49a260bdcd
--- /dev/null
+++ b/challenge-242/barroff/julia/ch-2.jl
@@ -0,0 +1,13 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function flip_matrix(matrix::Array{T,2}) where {T<:Integer}
+ map(y -> y == 0 ? 1 : 0, reverse(matrix, dims = (2)))
+end
+
+@testset "flip matrix" begin
+ @test flip_matrix([1 1 0; 1 0 1; 0 0 0]) == [1 0 0; 0 1 0; 1 1 1]
+ @test flip_matrix([1 1 0 0; 1 0 0 1; 0 1 1 1; 1 0 1 0]) ==
+ [1 1 0 0; 0 1 1 0; 0 0 0 1; 1 0 1 0]
+end
diff --git a/challenge-242/barroff/nim/ch_1.nim b/challenge-242/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..df12181cfb
--- /dev/null
+++ b/challenge-242/barroff/nim/ch_1.nim
@@ -0,0 +1,28 @@
+import std/[sets, unittest]
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+proc missing_members[T: SomeInteger](arr1: openArray[T], arr2: openArray[T]): (
+ HashSet[T], HashSet[T]) =
+ let
+ set1 = toHashSet(arr1)
+ set2 = toHashSet(arr2)
+ result = (set1 - set2, set2 - set1)
+
+suite "missing members":
+ test "[1, 2, 3] and [2, 4, 6]":
+ let
+ arr1 = [1, 2, 3]
+ arr2 = [2, 4, 6]
+ res = (toHashSet([1, 3]), toHashSet([4, 6]))
+
+ check(missing_members(arr1, arr2) == res)
+
+ test "[1, 2, 3, 3] and [1, 1, 2, 2]":
+ let
+ arr1 = [1, 2, 3, 3]
+ arr2 = [1, 1, 2, 2]
+ res = (toHashSet([3]), toHashSet[int]([]))
+
+ check(missing_members(arr1, arr2) == res)
diff --git a/challenge-242/barroff/nim/ch_2.nim b/challenge-242/barroff/nim/ch_2.nim
new file mode 100644
index 0000000000..1f0a358e2b
--- /dev/null
+++ b/challenge-242/barroff/nim/ch_2.nim
@@ -0,0 +1,24 @@
+import std/[algorithm, sequtils, sugar, unittest]
+
+# run tests with following command:
+# nim c -r ch_2.nim
+
+proc flip_matrix(matrix: seq[seq[int]]): seq[seq[int]] =
+ result = map(matrix, proc(x: seq[int]): seq[int] = mapIt(reversed(x), if it == 0: 1 else: 0))
+
+suite "flip matrix":
+ test "@[@[1, 1, 0], @[1, 0, 1], @[0, 0, 0]]":
+ let
+ matrix = @[@[1, 1, 0], @[1, 0, 1], @[0, 0, 0]]
+ expected = @[@[1, 0, 0], @[0, 1, 0], @[1, 1, 1]]
+ res = flip_matrix(matrix)
+
+ check(expected == res)
+
+ test "@[@[1, 1, 0, 0], @[1, 0, 0, 1], @[0, 1, 1, 1], @[1, 0, 1, 0]]":
+ let
+ matrix = @[@[1, 1, 0, 0], @[1, 0, 0, 1], @[0, 1, 1, 1], @[1, 0, 1, 0]]
+ expected = @[@[1, 1, 0, 0], @[0, 1, 1, 0], @[0, 0, 0, 1], @[1, 0, 1, 0]]
+ res = flip_matrix(matrix)
+
+ check(expected == res)
diff --git a/challenge-242/barroff/raku/ch-1.p6 b/challenge-242/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..99bfd1ef4a
--- /dev/null
+++ b/challenge-242/barroff/raku/ch-1.p6
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub missing-members(Int:D @arr1, Int:D @arr2 --> List:D) {
+ my Set[Int] $set1 = Set[Int](@arr1);
+ my Set[Int] $set2 = Set[Int](@arr2);
+ return ($set1 (-) $set2, $set2 (-) $set1);
+}
+
+#| Run test cases
+sub MAIN() {
+ use Test;
+ plan 2;
+
+ is-deeply missing-members(Array[Int](1, 2, 3), Array[Int](2, 4, 6)),
+ (Set[Int](1, 3), Set[Int](4, 6)),
+ 'works for (1, 2, 3) and (2, 4, 6)';
+ is-deeply missing-members(Array[Int](1, 2, 3, 3), Array[Int](1, 1, 2, 2)),
+ (Set[Int](3), Set[Int].new()),
+ 'works for (1, 2, 3, 3) and (1, 1, 2, 2)';
+}
diff --git a/challenge-242/barroff/raku/ch-2.p6 b/challenge-242/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..997ea38059
--- /dev/null
+++ b/challenge-242/barroff/raku/ch-2.p6
@@ -0,0 +1,20 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub flip-matrix(@matrix) {
+ map({ map({ $_ == 0 ?? 1 !! 0 }, reverse($_))}, @matrix);
+}
+
+#| Run test cases
+sub MAIN() {
+ use Test;
+ plan 2;
+
+ is flip-matrix(([1, 1, 0], [1, 0, 1], [0, 0, 0])),
+ ([1, 0, 0], [0, 1, 0], [1, 1, 1]),
+ 'works for ([1, 1, 0], [1, 0, 1], [0, 0, 0])';
+ is flip-matrix(([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])),
+ ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]),
+ 'works for ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])';
+}