From 53f6116a8421948cc1e76fda4c1da3437a87b129 Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Mon, 10 Feb 2025 07:40:30 +0100 Subject: feat: add solutions for challenge 307 from BarrOff --- challenge-307/barroff/julia/ch-1.jl | 14 ++++++++++++++ challenge-307/barroff/julia/ch-2.jl | 13 +++++++++++++ challenge-307/barroff/nim/ch_1.nim | 23 +++++++++++++++++++++++ challenge-307/barroff/raku/ch-1.p6 | 23 +++++++++++++++++++++++ challenge-307/barroff/raku/ch-2.p6 | 26 ++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 challenge-307/barroff/julia/ch-1.jl create mode 100644 challenge-307/barroff/julia/ch-2.jl create mode 100644 challenge-307/barroff/nim/ch_1.nim create mode 100644 challenge-307/barroff/raku/ch-1.p6 create mode 100644 challenge-307/barroff/raku/ch-2.p6 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.. 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); +} -- cgit