diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-11 21:51:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-11 21:51:30 +0000 |
| commit | 21bab6fbc151b2b15533c801427f7657a42c252a (patch) | |
| tree | 786e319a55b051ad9a2cb64c934be11abb05bb7a | |
| parent | d3c4da7ff01935c750ecc2567051cc01e494626e (diff) | |
| parent | 7917cf7c630338e3ec9c656e90108baa90bb0c25 (diff) | |
| download | perlweeklychallenge-club-21bab6fbc151b2b15533c801427f7657a42c252a.tar.gz perlweeklychallenge-club-21bab6fbc151b2b15533c801427f7657a42c252a.tar.bz2 perlweeklychallenge-club-21bab6fbc151b2b15533c801427f7657a42c252a.zip | |
Merge pull request #9560 from BarrOff/barroff-255
feat: add solutions for challenge 255 from BarrOff
| -rw-r--r-- | challenge-255/barroff/julia/ch-1.jl | 20 | ||||
| -rw-r--r-- | challenge-255/barroff/nim/ch_1.nim | 26 | ||||
| -rw-r--r-- | challenge-255/barroff/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-255/barroff/raku/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-255/barroff/raku/ch-2.p6 | 39 |
5 files changed, 140 insertions, 0 deletions
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..<len(s): + if s_split[i] != t_split[i]: + return t_split[i] + return t_split[^1] + +suite "odd character": + test "Perl": + check(odd_character("Perl", "Peerl") == 'e') + + test "Weekly": + check(odd_character("Weekly", "Weeakly") == 'a') + + test "Box": + check(odd_character("Box", "Boxy") == 'y') diff --git a/challenge-255/barroff/perl/ch-1.pl b/challenge-255/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..18aa56e2a8 --- /dev/null +++ b/challenge-255/barroff/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use v5.38; + +sub odd_character ( $s, $t ) { + my @sorted_s = sort( split( //, $s ) ); + my @sorted_t = sort( split( //, $t ) ); + for ( 0 .. @sorted_s - 1 ) { + if ( $sorted_s[$_] ne $sorted_t[$_] ) { + return $sorted_t[$_]; + } + } + return $sorted_t[-1]; +} + +sub MAIN() { + if ( @ARGV > 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); +} |
