From 7917cf7c630338e3ec9c656e90108baa90bb0c25 Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Sun, 11 Feb 2024 22:44:52 +0100 Subject: feat: add solutions for challenge 255 from BarrOff --- challenge-255/barroff/julia/ch-1.jl | 20 +++++++++++++++++++ challenge-255/barroff/nim/ch_1.nim | 26 +++++++++++++++++++++++++ challenge-255/barroff/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++ challenge-255/barroff/raku/ch-1.p6 | 22 +++++++++++++++++++++ challenge-255/barroff/raku/ch-2.p6 | 39 +++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 challenge-255/barroff/julia/ch-1.jl create mode 100644 challenge-255/barroff/nim/ch_1.nim create mode 100644 challenge-255/barroff/perl/ch-1.pl create mode 100644 challenge-255/barroff/raku/ch-1.p6 create mode 100644 challenge-255/barroff/raku/ch-2.p6 diff --git a/challenge-255/barroff/julia/ch-1.jl b/challenge-255/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..7419ba9aeb --- /dev/null +++ b/challenge-255/barroff/julia/ch-1.jl @@ -0,0 +1,20 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function odd_character(s::T, t::T) where {T<:AbstractString} + sorted_s = sort(split(s, "")) + sorted_t = sort(split(t, "")) + for i in zip(sorted_s, sorted_t[1:end - 1]) + if i[1] != i[2] + return i[2] + end + end + return sorted_t[end] +end + +@testset "odd character" begin + @test odd_character("Perl", "Peerl") == "e" + @test odd_character("Weekly", "Weeakly") == "a" + @test odd_character("Box", "Boxy") == "y" +end diff --git a/challenge-255/barroff/nim/ch_1.nim b/challenge-255/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..3fb1882ca7 --- /dev/null +++ b/challenge-255/barroff/nim/ch_1.nim @@ -0,0 +1,26 @@ +import std/[sugar, unittest] + +# run tests with following command: +# nim c -r ch_1.nim + +proc odd_character(s, t: string): char = + let + s_split = collect: + for c in s: c + t_split = collect: + for c in t: c + + for i in 0.. 1 ) { + + #| Run on command line argument + say odd_character( $ARGV[1], $ARGV[2] ); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is odd_character( 'Perl', 'Peerl' ), 'e', 'works for "Perl"'; + is odd_character( 'Weekly', 'Weeakly' ), 'a', 'works for "Weekly"'; + is odd_character( 'Box', 'Boxy' ), 'y', 'works for "Box"'; + } +} + +MAIN(); diff --git a/challenge-255/barroff/raku/ch-1.p6 b/challenge-255/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..8050158238 --- /dev/null +++ b/challenge-255/barroff/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env raku + +use v6.d; + +sub odd-character(Str:D $s, Str:D $t --> Str:D) { + (Bag($t.comb) (-) Bag($s.comb)).keys[0]; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is odd-character("Perl", "Preel"), "e", 'works for "Perl"'; + is odd-character("Weekly", "Weeakly"), "a", 'works for "Weekly"'; + is odd-character("Box", "Boxy"), "y", 'works for "Box"'; +} + +#| Take user provided word like aba +multi sub MAIN(Str:D $s, Str:D $t) { + say odd-character($s, $t); +} diff --git a/challenge-255/barroff/raku/ch-2.p6 b/challenge-255/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..dba1c8ca8b --- /dev/null +++ b/challenge-255/barroff/raku/ch-2.p6 @@ -0,0 +1,39 @@ +#!/usr/bin/env raku + +use v6.d; + +sub most-frequent-word(Str:D $p, Str:D $w --> Str:D) { + my $frequencies = Bag( + grep( + { not($_ ~~ /$w/) }, + split( + /\W+/, + $p + ) + ) + ); + sort( + { $frequencies{$^a} ≤ $frequencies{$^b}}, + keys($frequencies) + )[0]; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is most-frequent-word( + "Joe hit a ball, the hit ball flew far after it was hit.", + "hit" + ), "ball", 'works for "hit"'; + is most-frequent-word("Perl and Raku belong to the same family." ~ + " Perl is the most popular language in the weekly challenge.", + "the" + ), "Perl", 'works for "Perl"'; +} + +#| Take user provided word like aba +multi sub MAIN(Str:D $p, Str:D $w) { + say most-frequent-word($p, $w); +} -- cgit