diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-24 22:26:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-24 22:26:55 +0100 |
| commit | 5cf5028703bcb99f09e91d70d42bf763964aa6a6 (patch) | |
| tree | 137073a547c70e594df8b85cd0d6e31c790f4904 | |
| parent | d7b6db2ca3002002a4b87ff8e344b520a888f061 (diff) | |
| parent | 2ba8eb66eaa246d069694ff96953392507f17ed1 (diff) | |
| download | perlweeklychallenge-club-5cf5028703bcb99f09e91d70d42bf763964aa6a6.tar.gz perlweeklychallenge-club-5cf5028703bcb99f09e91d70d42bf763964aa6a6.tar.bz2 perlweeklychallenge-club-5cf5028703bcb99f09e91d70d42bf763964aa6a6.zip | |
Merge pull request #8761 from BarrOff/barroff-235
feat: add solutions for challenge 235 from BarrOff
| -rw-r--r-- | challenge-235/barroff/julia/ch-1.jl | 15 | ||||
| -rw-r--r-- | challenge-235/barroff/julia/ch-2.jl | 15 | ||||
| -rw-r--r-- | challenge-235/barroff/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-235/barroff/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-235/barroff/raku/ch-1.p6 | 25 | ||||
| -rw-r--r-- | challenge-235/barroff/raku/ch-2.p6 | 26 |
6 files changed, 145 insertions, 0 deletions
diff --git a/challenge-235/barroff/julia/ch-1.jl b/challenge-235/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..5710fc7192 --- /dev/null +++ b/challenge-235/barroff/julia/ch-1.jl @@ -0,0 +1,15 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function remove_one(ints::Array{T}) where T <: Integer + if length(ints) < 3 return true end + comparisons = map(x -> ints[x] < ints[x + 1] ? 0 : 1, 1:length(ints) - 1) + return sum(comparisons) < 2 +end + +@testset "remove one" begin + @test remove_one([0, 2, 9, 4, 6]) == true + @test remove_one([5, 1, 3, 2]) == false + @test remove_one([2, 2, 3]) == true +end diff --git a/challenge-235/barroff/julia/ch-2.jl b/challenge-235/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..0909210a35 --- /dev/null +++ b/challenge-235/barroff/julia/ch-2.jl @@ -0,0 +1,15 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function duplicate_zeros(ints::Array{T}) where T <: Integer + result = T[] + map(x -> x == 0 ? push!(result, 0, 0) : push!(result, x), ints) + return result[1:length(ints)] +end + +@testset "duplicate zeros" begin + @test duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0]) == [1, 0, 0, 2, 3, 0, 0, 4] + @test duplicate_zeros([1, 2, 3]) == [1, 2, 3] + @test duplicate_zeros([0, 3, 0, 4, 5]) == [0, 0, 3, 0, 0] +end diff --git a/challenge-235/barroff/perl/ch-1.pl b/challenge-235/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..78b652f9d3 --- /dev/null +++ b/challenge-235/barroff/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw/sum/; + +sub remove_one (@ints) { + return 1 if @ints < 3; + my @comparisons = + map( { $ints[$_] < $ints[ $_ + 1 ] ? 0 : 1 } 0 .. @ints - 2 ); + return sum(@comparisons) < 2 ? 1 : 0; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say remove_one(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is remove_one( ( 0, 2, 9, 4, 6 ) ), 1, 'works for (0, 2, 9, 4, 6)'; + is remove_one( ( 5, 1, 3, 2 ) ), 0, 'works for (5, 1, 3, 2)'; + is remove_one( ( 2, 2, 3 ) ), 1, 'works for (2, 2, 3)'; + } +} + +MAIN(); diff --git a/challenge-235/barroff/perl/ch-2.pl b/challenge-235/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..900414b082 --- /dev/null +++ b/challenge-235/barroff/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw/sum/; + +sub duplicate_zeros (@ints) { + my @result; + map( { $_ == 0 ? push( @result, 0, 0 ) : push( @result, $_ ) } @ints ); + @result = @result[ 0 .. @ints - 1 ]; + return \@result; +} + +sub MAIN() { + if (@ARGV) { + + #| Run command line arguments + say remove_one(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is duplicate_zeros( ( 1, 0, 2, 3, 0, 4, 5, 0 ) ), + [ 1, 0, 0, 2, 3, 0, 0, 4 ], 'works for (1, 0, 2, 3, 0, 4, 5, 0)'; + is duplicate_zeros( ( 1, 2, 3 ) ), [ 1, 2, 3 ], 'works for (1, 2, 3)'; + is duplicate_zeros( ( 0, 3, 0, 4, 5 ) ), [ 0, 0, 3, 0, 0 ], + 'works for (0, 3, 0, 4, 5)'; + } +} + +MAIN(); diff --git a/challenge-235/barroff/raku/ch-1.p6 b/challenge-235/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..076c7239ab --- /dev/null +++ b/challenge-235/barroff/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub remove-one(@ints --> Bool:D) { + return True if @ints.elems ≤ 2; + my @comparisons = map({ @ints[$_] < @ints[$_ + 1] ?? 0 !! 1 }, 0..@ints.elems - 2); + return sum(@comparisons) < 2; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is remove-one([0, 2, 9, 4, 6]), True, 'works for (0, 2, 9, 4, 6)'; + is remove-one([5, 1, 3, 2]), False, 'works for (5, 1, 3, 2)'; + is remove-one([2, 2, 3]), True, 'works for (2, 2, 3)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints where @ints.elems ≥ 1) { + say remove-one(@ints); +} + diff --git a/challenge-235/barroff/raku/ch-2.p6 b/challenge-235/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..17b09830fc --- /dev/null +++ b/challenge-235/barroff/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +use v6.d; + +sub duplicate-zeros(@ints --> Positional) { + my @result = map({ $_ == 0 ?? Slip.new(0, 0) !! $_ }, @ints); + return @result[0..@ints.elems - 1]; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is duplicate-zeros([1, 0, 2, 3, 0, 4, 5, 0]), (1, 0, 0, 2, 3, 0, 0, 4), + 'works for (1, 0, 2, 3, 0, 4, 5, 0)'; + is duplicate-zeros([1, 2, 3]), (1, 2, 3), + 'works for (1, 2, 3)'; + is duplicate-zeros([0, 3, 0, 4, 5]), (0, 0, 3, 0, 0), + 'works for (0, 3, 0, 4, 5)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints where @ints.elems ≥ 1) { + say duplicate-zeros(@ints); +} |
