From e89140d1ab9ec6b20a1d37ca346cfa3dc0fefb5e Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Mon, 22 Jul 2024 00:26:22 +0200 Subject: feat: add solutions for challenge 278 from BarrOff --- challenge-278/barroff/julia/ch-1.jl | 17 +++++++++++++++++ challenge-278/barroff/julia/ch-2.jl | 18 ++++++++++++++++++ challenge-278/barroff/raku/ch-1.p6 | 28 ++++++++++++++++++++++++++++ challenge-278/barroff/raku/ch-2.p6 | 29 +++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 challenge-278/barroff/julia/ch-1.jl create mode 100644 challenge-278/barroff/julia/ch-2.jl create mode 100644 challenge-278/barroff/raku/ch-1.p6 create mode 100644 challenge-278/barroff/raku/ch-2.p6 diff --git a/challenge-278/barroff/julia/ch-1.jl b/challenge-278/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..2da17950e4 --- /dev/null +++ b/challenge-278/barroff/julia/ch-1.jl @@ -0,0 +1,17 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function sort_string(str::AbstractString)::String + str_dict = Dict{Int,String}() + map(x -> str_dict[parse(Int, x[end])] = x[1:end-1], split(str)) + return join(map(x -> str_dict[x], 1:length(str_dict)), " ") +end + +@testset "sort string" begin + @test sort_string("and2 Raku3 cousins5 Perl1 are4") == + "Perl and Raku are cousins" + @test sort_string("guest6 Python1 most4 the3 popular5 is2 language7") == + "Python is the most popular guest language" + @test sort_string("Challenge3 The1 Weekly2") == "The Weekly Challenge" +end diff --git a/challenge-278/barroff/julia/ch-2.jl b/challenge-278/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..2b6402a9a3 --- /dev/null +++ b/challenge-278/barroff/julia/ch-2.jl @@ -0,0 +1,18 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function reverse_word(word::AbstractString, chr::Char)::String + if contains(word, chr) + char_pos = findfirst(chr, word) + return join(sort(split(word[1:char_pos], ""))) * word[(char_pos + 1):end] + else + return word + end +end + +@testset "reverse word" begin + @test reverse_word("challenge", 'e') == "acehllnge" + @test reverse_word("programming", 'a') == "agoprrmming" + @test reverse_word("champion", 'b') == "champion" +end diff --git a/challenge-278/barroff/raku/ch-1.p6 b/challenge-278/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..bb0d57bb83 --- /dev/null +++ b/challenge-278/barroff/raku/ch-1.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub sort-string(Str $str --> Str) { + my %str-dict; + map({ $_ ~~ /^(\w+)(\d)$/; %str-dict{$1} = $0 }, words($str)); + map({%str-dict{$_}}, 1..%str-dict.elems).join: " "; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is sort-string("and2 Raku3 cousins5 Perl1 are4"), + "Perl and Raku are cousins", 'works for "and2 Raku3 cousins5 Perl1 are4"'; + is sort-string("guest6 Python1 most4 the3 popular5 is2 language7"), + "Python is the most popular guest language", + 'works for "guest6 Python1 most4 the3 popular5 is2 language7"'; + is sort-string("Challenge3 The1 Weekly2"), "The Weekly Challenge", + 'works for "Challenge3 The1 Weekly2"'; +} + +#| Take user provided strings like and2 Raku3 cousins5 Perl1 are4 +multi sub MAIN(*@strings where so @strings.all ~~ /^\w+\d$/) { + say sort-string((@strings.join: " ")); +} diff --git a/challenge-278/barroff/raku/ch-2.p6 b/challenge-278/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..829939b551 --- /dev/null +++ b/challenge-278/barroff/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +use v6.d; + +sub reverse-word(Str $word, Str $char --> Str) { + return $word unless $word.contains($char); + $word ~~ / + ^ + (\w*? "$char") + (\w*) + /; + sort($0.comb).join ~ $1; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is reverse-word("challenge", "e"), "acehllnge", 'works for "challenge" and "e"'; + is reverse-word("programming", "a"), "agoprrmming", + 'works for "programming" and "a"'; + is reverse-word("champion", "b"), "champion", 'works for "champion" and "b"'; +} + +#| Take user provided strings like challenge e +multi sub MAIN(Str $word, Str $char where chars($char) == 1) { + say reverse-word($word, $char); +} -- cgit