diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-24 02:29:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-24 02:29:36 +0100 |
| commit | d15cc70438cf144374ae759ac3ac2a0bf5cdd2f6 (patch) | |
| tree | ae6a02ad75421c37fa078a6652fcfaf29cb8444f | |
| parent | 534573651e1308cf352060a21124419c434e46e7 (diff) | |
| parent | 330caa9b0235e1752247446b0bab94b7aa1108c5 (diff) | |
| download | perlweeklychallenge-club-d15cc70438cf144374ae759ac3ac2a0bf5cdd2f6.tar.gz perlweeklychallenge-club-d15cc70438cf144374ae759ac3ac2a0bf5cdd2f6.tar.bz2 perlweeklychallenge-club-d15cc70438cf144374ae759ac3ac2a0bf5cdd2f6.zip | |
Merge pull request #8433 from BarrOff/barroff-226
feat: add solutions for challenge 226 from BarrOff
| -rw-r--r-- | challenge-226/barroff/julia/ch-1.jl | 14 | ||||
| -rw-r--r-- | challenge-226/barroff/julia/ch-2.jl | 14 | ||||
| -rw-r--r-- | challenge-226/barroff/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-226/barroff/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-226/barroff/raku/ch-1.p6 | 20 | ||||
| -rw-r--r-- | challenge-226/barroff/raku/ch-2.p6 | 24 |
6 files changed, 123 insertions, 0 deletions
diff --git a/challenge-226/barroff/julia/ch-1.jl b/challenge-226/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..436af0cc42 --- /dev/null +++ b/challenge-226/barroff/julia/ch-1.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function shuffle_string(strng::String, ints::Array{Int}) + shuffle_dict = Dict{Integer,String}(x => y for (x, y) in zip(ints, split(strng, ""))) + return join([shuffle_dict[x] for x in collect(0:maximum(ints))], "") +end + +@testset "shuffle string" begin + @test shuffle_string("lacelengh", [3, 2, 0, 5, 4, 8, 6, 7, 1]) == "challenge" + @test shuffle_string("rulepark", [4, 7, 3, 1, 0, 5, 2, 6]) == "perlraku" +end + diff --git a/challenge-226/barroff/julia/ch-2.jl b/challenge-226/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..eca5aef1ef --- /dev/null +++ b/challenge-226/barroff/julia/ch-2.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function zero_array(ints::Array{Int}) + return length(filter(x -> x > 0, unique(ints))) +end + +@testset "zero array" begin + @test zero_array([1, 5, 0, 3, 5]) == 3 + @test zero_array([0]) == 0 + @test zero_array([2, 1, 4, 0, 3]) == 4 +end + diff --git a/challenge-226/barroff/perl/ch-1.pl b/challenge-226/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..1f32c917dd --- /dev/null +++ b/challenge-226/barroff/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use v5.38; +use List::Util qw( zip ); + +sub shuffle_string ( $string, @ints ) { + my %string_dict; + my @split_string = split( //, $string ); + map( $string_dict{ @$_[0] } = @$_[1], zip( \@ints, \@split_string ) ); + join '', @string_dict{ 0 .. length($string) - 1 }; +} + +#| Run test cases +sub MAIN() { + use Test2::V0 qw( is plan ); + plan 2; + + is shuffle_string( 'lacelengh', ( 3, 2, 0, 5, 4, 8, 6, 7, 1 ) ), + 'challenge', 'works for lacelengh'; + is shuffle_string( 'rulepark', ( 4, 7, 3, 1, 0, 5, 2, 6 ) ), + 'perlraku', 'works for rulepark'; +} + +MAIN(); diff --git a/challenge-226/barroff/perl/ch-2.pl b/challenge-226/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..f7d4510bfd --- /dev/null +++ b/challenge-226/barroff/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use v5.38; + +sub zero_array (@ints) { + use List::Util qw( uniq ); + scalar grep { $_ > 0 } uniq @ints; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say zero_array(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is zero_array( 1, 5, 0, 3, 5 ), 3, 'works for (1, 5, 0, 3, 5)'; + is zero_array(0), 0, 'works for (0)'; + is zero_array( 2, 1, 4, 0, 3 ), 4, 'works for (2, 1, 4, 0, 3)'; + } +} + +MAIN(); diff --git a/challenge-226/barroff/raku/ch-1.p6 b/challenge-226/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..234a9c852d --- /dev/null +++ b/challenge-226/barroff/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +use v6.d; + +sub shuffle-string(Str:D $string, Int:D @indices --> Str:D) { + my %shuffle-dict = @indices >>=><< $string.comb; + return join '', %shuffle-dict{0..^$string.chars}; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is shuffle-string('lacelengh', Array[Int:D].new((3,2,0,5,4,8,6,7,1))), + 'challenge', 'works for "lacelengh"'; + is shuffle-string('rulepark', Array[Int:D].new((4,7,3,1,0,5,2,6))), + 'perlraku', 'works for "rulepark"'; +} + diff --git a/challenge-226/barroff/raku/ch-2.p6 b/challenge-226/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..88308a381f --- /dev/null +++ b/challenge-226/barroff/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use v6.d; + +sub zero-array(Int:D @ints --> Int:D) { + (grep { $_ > 0 }, unique @ints).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is zero-array(Array[Int:D].new((1, 5, 0, 3, 5))), 3, 'works for (1, 5, 0, 3, 5)'; + is zero-array(Array[Int:D].new((0))), 0, 'works for (0)'; + is zero-array(Array[Int:D].new((2, 1, 4, 0, 3))), 4, 'works for (2, 1, 4, 0, 3)'; +} + +#| Take user provided list like 1 17 8 +multi sub MAIN(*@numbers where @numbers.elems ≥ 1) { + my Int @int-nums = @numbers; + say zero-array(@int-nums); +} + |
