From f79cd5b4725808caf63dc80e47fe969f4aed6dff Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Sun, 29 Oct 2023 22:23:23 +0100 Subject: feat: add solutions for challenge 240 from BarrOff --- challenge-240/barroff/julia/ch-1.jl | 13 +++++++++++++ challenge-240/barroff/julia/ch-2.jl | 12 ++++++++++++ challenge-240/barroff/nim/ch_1.nim | 26 ++++++++++++++++++++++++++ challenge-240/barroff/nim/ch_2.nim | 24 ++++++++++++++++++++++++ challenge-240/barroff/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-240/barroff/perl/ch-2.pl | 31 +++++++++++++++++++++++++++++++ challenge-240/barroff/raku/ch-1.p6 | 25 +++++++++++++++++++++++++ challenge-240/barroff/raku/ch-2.p6 | 28 ++++++++++++++++++++++++++++ 8 files changed, 188 insertions(+) create mode 100644 challenge-240/barroff/julia/ch-1.jl create mode 100644 challenge-240/barroff/julia/ch-2.jl create mode 100644 challenge-240/barroff/nim/ch_1.nim create mode 100644 challenge-240/barroff/nim/ch_2.nim create mode 100644 challenge-240/barroff/perl/ch-1.pl create mode 100644 challenge-240/barroff/perl/ch-2.pl create mode 100644 challenge-240/barroff/raku/ch-1.p6 create mode 100644 challenge-240/barroff/raku/ch-2.p6 diff --git a/challenge-240/barroff/julia/ch-1.jl b/challenge-240/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..baf7efa997 --- /dev/null +++ b/challenge-240/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function acronym(chk::String, str::Vector{String}) + return lowercase(chk) == join(map(x -> lowercase(x)[1], str)) +end + +@testset "acronym" begin + @test acronym("ppp", ["Perl", "Python", "Pascal"]) == true + @test acronym("rp", ["Perl", "Raku"]) == false + @test acronym("oac", ["Oracle", "Awk", "C"]) == true +end diff --git a/challenge-240/barroff/julia/ch-2.jl b/challenge-240/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..01eb435e5a --- /dev/null +++ b/challenge-240/barroff/julia/ch-2.jl @@ -0,0 +1,12 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function build_array(ints::Array{T}) where T <: Integer + return map(x -> ints[x] + 1 > length(ints) ? NaN : ints[ints[x] + 1], 1:length(ints)) +end + +@testset "build array" begin + @test build_array([0, 2, 1, 5, 3, 4]) == [0, 1, 2, 4, 5, 3] + @test build_array([5, 0, 1, 2, 3, 4]) == [4, 5, 0, 1, 2, 3] +end diff --git a/challenge-240/barroff/nim/ch_1.nim b/challenge-240/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..f42e200de6 --- /dev/null +++ b/challenge-240/barroff/nim/ch_1.nim @@ -0,0 +1,26 @@ +import std/[sequtils, strutils, sugar, unicode, unittest] + +# run tests with following command: +# nim c -r ch_1.nim + +proc acronym(chk: string, str: openArray[string]): bool = + result = toLower(chk) == join(map(str, x => toLowerAscii(x[0])), "") + +suite "acronym": + test "\"ppp\" [\"Perl\", \"Python\", \"Pascal\"]": + let + chk = "ppp" + str = ["Perl", "Python", "Pascal"] + check(acronym(chk, str) == true) + + test "\"rp\" [\"Perl\", \"Raku\"]": + let + chk = "rp" + str = ["Perl", "Raku"] + check(acronym(chk, str) == false) + + test "\"oac\" [\"Oracle\", \"Awk\", \"C\"]": + let + chk = "oac" + str = ["Oracle", "Awk", "C"] + check(acronym(chk, str) == true) diff --git a/challenge-240/barroff/nim/ch_2.nim b/challenge-240/barroff/nim/ch_2.nim new file mode 100644 index 0000000000..c1d661ba20 --- /dev/null +++ b/challenge-240/barroff/nim/ch_2.nim @@ -0,0 +1,24 @@ +import std/[sequtils, sugar, unittest] + +# run tests with following command: +# nim c -r ch_2.nim + +proc build_array(ints: openArray[int]): seq[int] = + result = mapIt(0..high(ints), if ints[it] >= len(ints): -1 else: ints[ints[it]]) + +suite "build array": + test "(0, 2, 1, 5, 3, 4)": + let + ints = [0, 2, 1, 5, 3, 4] + expected = [0, 1, 2, 4, 5, 3] + res = build_array(ints) + + check(expected == res) + + test "(5, 0, 1, 2, 3, 4)": + let + ints = [5, 0, 1, 2, 3, 4] + expected = [4, 5, 0, 1, 2, 3] + res = build_array(ints) + + check(expected == res) diff --git a/challenge-240/barroff/perl/ch-1.pl b/challenge-240/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..ed1339ddf0 --- /dev/null +++ b/challenge-240/barroff/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use v5.38; + +sub acronym ( $chk, @str ) { + lc($chk) eq join( '', map { substr( lc($_), 0, 1 ) } @str ) ? 1 : 0; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say acronym(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is acronym( "ppp", ( "Perl", "Python", "Pascal" ) ), 1, + 'works for "ppp" and ("Perl", "Python", "Pascal")'; + is acronym( "rp", ( "Perl", "Raku" ) ), 0, + 'works for "rp" and ("Perl", "Raku")'; + is acronym( "oac", ( "Oracle", "Awk", "C" ) ), 1, + 'works for "oac" and ("Oracle", "Awk", "C")'; + } +} + +MAIN(); diff --git a/challenge-240/barroff/perl/ch-2.pl b/challenge-240/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..58aa8e65bc --- /dev/null +++ b/challenge-240/barroff/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw/sum/; + +sub build_array (@ints) { + my @result = + map( { $ints[$_] >= @ints ? undef : $ints[ $ints[$_] ] } 0 .. @ints - 1 ); + return \@result; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say build_array(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 2; + + is build_array( 0, 2, 1, 5, 3, 4 ), [ 0, 1, 2, 4, 5, 3 ], + 'works for (0, 2, 1, 5, 3, 4)'; + is build_array( 5, 0, 1, 2, 3, 4 ), [ 4, 5, 0, 1, 2, 3 ], + 'works for (5, 0, 1, 2, 3, 4)'; + } +} + +MAIN(); diff --git a/challenge-240/barroff/raku/ch-1.p6 b/challenge-240/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..a6a23d8858 --- /dev/null +++ b/challenge-240/barroff/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub acronym(Str:D $chk, *@str --> Bool) { + $chk.lc eq [~] map({ substr($_.lc, 0..0) }, @str) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is acronym("ppp", ("Perl", "Python", "Pascal")), True, + 'works for "ppp" and ("Perl", "Python", "Pascal")'; + is acronym("rp", ("Perl", "Raku")), False, + 'works for "rp" and ("Perl", "Raku")'; + is acronym("oac", ("Oracle", "Awk", "C")), True, + 'works for "oac" and ("Oracle", "Awk", "C")'; +} + +#| Take user provided list like aba aabb abcd bac aabc +multi sub MAIN(Str:D $chk, *@words where @words.elems ≥ 1) { + say acronym(@words); +} diff --git a/challenge-240/barroff/raku/ch-2.p6 b/challenge-240/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..8e5852a83f --- /dev/null +++ b/challenge-240/barroff/raku/ch-2.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub build-array(@ints, --> Positional:D) { + List( + map( + { @ints[$_] ≥ @ints.elems ?? Nil !! @ints[@ints[$_]] }, + 0..^@ints.elems + ) + ) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is build-array([0, 2, 1, 5, 3, 4]), [0, 1, 2, 4, 5, 3], + 'works for (0, 2, 1, 5, 3, 4)'; + is build-array([5, 0, 1, 2, 3, 4]), [4, 5, 0, 1, 2, 3], + 'works for (5, 0, 1, 2, 3, 4)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints where @ints.elems ≥ 1) { + say build-array(@ints); +} -- cgit