aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2024-05-26 22:18:06 +0200
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2024-05-26 22:18:06 +0200
commitee7fd0942cf32ed5cfc66870ee5503a8ca934d62 (patch)
tree91b2eeb7879dfe91f6e605e509e7c5ce8b43f83d
parent5ae5dae1606d672f5123901550717cdb5ec72921 (diff)
downloadperlweeklychallenge-club-ee7fd0942cf32ed5cfc66870ee5503a8ca934d62.tar.gz
perlweeklychallenge-club-ee7fd0942cf32ed5cfc66870ee5503a8ca934d62.tar.bz2
perlweeklychallenge-club-ee7fd0942cf32ed5cfc66870ee5503a8ca934d62.zip
feat: add solutions for challenge 270 from BarrOff
-rw-r--r--challenge-270/barroff/julia/ch-1.jl30
-rw-r--r--challenge-270/barroff/raku/ch-1.p627
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-270/barroff/julia/ch-1.jl b/challenge-270/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..67e4b509c2
--- /dev/null
+++ b/challenge-270/barroff/julia/ch-1.jl
@@ -0,0 +1,30 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function special_positions(m::Matrix{T})::Int where {T<:Integer}
+ special_columns = filter(x -> sum(m[:, x]) == 1, 1:size(m, 2))
+ return length(
+ filter(
+ x -> sum(m[x, :]) == 1,
+ map(
+ x -> findfirst(
+ y -> y == 1,
+ m[:, x]
+ ), special_columns)
+ )
+ )
+end
+
+@testset "special positions" begin
+ @test special_positions(
+ [1 0 0;
+ 0 0 1;
+ 1 0 0
+ ]) == 1
+ @test special_positions(
+ [1 0 0;
+ 0 1 0;
+ 0 0 1
+ ]) == 3
+end
diff --git a/challenge-270/barroff/raku/ch-1.p6 b/challenge-270/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..e4beb0f2b4
--- /dev/null
+++ b/challenge-270/barroff/raku/ch-1.p6
@@ -0,0 +1,27 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub special-positions(@ints --> Int) {
+ my @special-rows = grep({ sum(@ints[$_]) == 1 }, 0..@ints.elems - 1);
+ my &special-columns = sub ($i) {
+ my $column = @ints[$i].first: * == 1, :k;
+ sum(map({ @ints[$_][$column]}, 0..@ints.elems - 1));
+ }
+ grep({ &special-columns($_) == 1 }, @special-rows).elems;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is special-positions([ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0],
+ ]), 1, 'works for first matrix';
+ is special-positions([ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ]), 3, 'works for second matrix';
+}