aboutsummaryrefslogtreecommitdiff
path: root/challenge-307/barroff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2025-02-10 07:40:30 +0100
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2025-02-10 07:40:30 +0100
commit53f6116a8421948cc1e76fda4c1da3437a87b129 (patch)
treefa75a297618bb5c87a75ad4c39de4b4c960326b3 /challenge-307/barroff
parentaa4b8399bdb3da0a50173fcab689bfa80b9b54e1 (diff)
downloadperlweeklychallenge-club-53f6116a8421948cc1e76fda4c1da3437a87b129.tar.gz
perlweeklychallenge-club-53f6116a8421948cc1e76fda4c1da3437a87b129.tar.bz2
perlweeklychallenge-club-53f6116a8421948cc1e76fda4c1da3437a87b129.zip
feat: add solutions for challenge 307 from BarrOff
Diffstat (limited to 'challenge-307/barroff')
-rw-r--r--challenge-307/barroff/julia/ch-1.jl14
-rw-r--r--challenge-307/barroff/julia/ch-2.jl13
-rw-r--r--challenge-307/barroff/nim/ch_1.nim23
-rw-r--r--challenge-307/barroff/raku/ch-1.p623
-rw-r--r--challenge-307/barroff/raku/ch-2.p626
5 files changed, 99 insertions, 0 deletions
diff --git a/challenge-307/barroff/julia/ch-1.jl b/challenge-307/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..742a9c304b
--- /dev/null
+++ b/challenge-307/barroff/julia/ch-1.jl
@@ -0,0 +1,14 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function checkOrder(ints::Vector{Int})::Vector{Int}
+ sortedInts = sort(ints)
+ return filter(x -> ints[x] != sortedInts[x], 1:length(ints)) .- 1
+end
+
+@testset "check order" begin
+ @test checkOrder([5, 2, 4, 3, 1]) == [0, 2, 3, 4]
+ @test checkOrder([1, 2, 1, 1, 3]) == [1, 3]
+ @test checkOrder([3, 1, 3, 2, 3]) == [0, 1, 3]
+end
diff --git a/challenge-307/barroff/julia/ch-2.jl b/challenge-307/barroff/julia/ch-2.jl
new file mode 100644
index 0000000000..04794007ad
--- /dev/null
+++ b/challenge-307/barroff/julia/ch-2.jl
@@ -0,0 +1,13 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function find_anagrams(words::Vector{String})::Int
+ cleanedWords = [join(sort(collect(x))) for x in words]
+ return count(x -> cleanedWords[x] != cleanedWords[x+1], 1:length(words)-1) + 1
+end
+
+@testset "find anagrams" begin
+ @test find_anagrams(["acca", "dog", "god", "perl", "repl"]) == 3
+ @test find_anagrams(["abba", "baba", "aabb", "ab", "ab"]) == 2
+end
diff --git a/challenge-307/barroff/nim/ch_1.nim b/challenge-307/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..3a0e6cf123
--- /dev/null
+++ b/challenge-307/barroff/nim/ch_1.nim
@@ -0,0 +1,23 @@
+import std/unittest
+
+from std/algorithm import sorted
+from std/sequtils import filterIt, toSeq
+
+# run tests with following command:
+# nim r ch_1.nim
+
+proc checkOrder(ints: openArray[int]): seq[int] =
+ let
+ sortedInts: seq[int] = sorted(ints)
+
+ result = filterIt(toSeq(0..<len(ints)), ints[it] != sortedInts[it])
+
+suite "check order":
+ test "[5, 2, 4, 3, 1]":
+ check(checkOrder([5, 2, 4, 3, 1]) == [0, 2, 3, 4])
+
+ test "[1, 2, 1, 1, 3]":
+ check(checkOrder([1, 2, 1, 1, 3]) == [1, 3])
+
+ test "[3, 1, 3, 2, 3]":
+ check(checkOrder([3, 1, 3, 2, 3]) == [0, 1, 3])
diff --git a/challenge-307/barroff/raku/ch-1.p6 b/challenge-307/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..20f0665ea8
--- /dev/null
+++ b/challenge-307/barroff/raku/ch-1.p6
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub check-order(@ints --> Seq) {
+ my @sorted-ints = sort @ints;
+ return grep({ @ints[$_] ≠ @sorted-ints[$_] }, 0..@ints.elems - 1);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is check-order([5, 2, 4, 3, 1]), [0, 2, 3, 4], 'works for [5, 2, 4, 3, 1]';
+ is check-order([1, 2, 1, 1, 3]), [1, 3], 'works for [1, 2, 1, 1, 3]';
+ is check-order([3, 1, 3, 2, 3]), [0, 1, 3], 'works for [3, 1, 3, 2, 3]';
+}
+
+#| Take user provided number like 1 0 1
+multi sub MAIN(*@ints) {
+ say check-order(@ints);
+}
diff --git a/challenge-307/barroff/raku/ch-2.p6 b/challenge-307/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..1527766718
--- /dev/null
+++ b/challenge-307/barroff/raku/ch-2.p6
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub find-anagrams(@words --> Int) {
+ grep(
+ { @words[$_].comb.sort ≠ @words[$_ + 1].comb.sort },
+ 0..@words.elems - 2
+ ).elems + 1;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is find-anagrams(["acca", "dog", "god", "perl", "repl"]), 3,
+ 'works for ["acca", "dog", "god", "perl", "repl"]';
+ is find-anagrams(["abba", "baba", "aabb", "ab", "ab"]), 2,
+ 'works for ["abba", "baba", "aabb", "ab", "ab"]';
+}
+
+#| Take user provided strings like the cattle was rattle by the battery cat bat rat
+multi sub MAIN(*@words) {
+ say find-anagrams(@words);
+}