diff options
| author | Bob Lied <boblied+github@gmail.com> | 2023-09-18 10:09:01 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2023-09-18 10:09:01 -0500 |
| commit | abc5d51f65ffafb1be1919668984f50335915c6c (patch) | |
| tree | aa55861d94fff632a7a21f251bce32d07f17122e | |
| parent | cf0d06f9fb5b80165ef5a59c3537ce9763b4abbc (diff) | |
| download | perlweeklychallenge-club-abc5d51f65ffafb1be1919668984f50335915c6c.tar.gz perlweeklychallenge-club-abc5d51f65ffafb1be1919668984f50335915c6c.tar.bz2 perlweeklychallenge-club-abc5d51f65ffafb1be1919668984f50335915c6c.zip | |
Week 235 Task 2 solution
| -rw-r--r-- | challenge-235/bob-lied/perl/ch-2.pl | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-235/bob-lied/perl/ch-2.pl b/challenge-235/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..1c2b6ac1b1 --- /dev/null +++ b/challenge-235/bob-lied/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge 235 Task 2 Duplicate Zeros +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to duplicate each occurrence of ZERO in the given array +# and shift the remaining to the right but make sure the size of array +# remain the same. +# Example 1 Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +# Ouput: (1, 0, 0, 2, 3, 0, 0, 4) +# Example 2 Input: @ints = (1, 2, 3) +# Ouput: (1, 2, 3) +# Example 3 Input: @ints = (0, 3, 0, 4, 5) +# Ouput: (0, 0, 3, 0, 0) +#============================================================================= + +use v5.36; + +use FindBin qw($Bin); +use lib "$FindBin::Bin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub duplicateZeros(@ints) +{ + [ (map { $_ || (0,0) } @ints)[0 .. $#ints] ] +} + +sub dz_A(@ints) +{ + my $maxLen = @ints; + my @output; + while ( @output < $maxLen ) + { + push @output, shift @ints; + push @output, 0 if ( $output[-1] == 0 && @output < $maxLen ); + } + return \@output; +} + +sub runTest +{ + use Test2::V0; + + is( duplicateZeros(1,0,2,3,0,4,5,0), [1,0,0,2,3,0,0,4], "Example 1"); + is( duplicateZeros(1,2,3 ), [1,2,3 ], "Example 2"); + is( duplicateZeros(0,3,0,4,5 ), [0,0,3,0,0 ], "Example 3"); + is( duplicateZeros(0), [0], "One Zero"); + is( duplicateZeros(2, 1, 0), [2, 1, 0], "Ends on a zero"); + + is( dz_A(1,0,2,3,0,4,5,0), [1,0,0,2,3,0,0,4], "Example 1"); + is( dz_A(1,2,3 ), [1,2,3 ], "Example 2"); + is( dz_A(0,3,0,4,5 ), [0,0,3,0,0 ], "Example 3"); + is( dz_A(0), [0], "One Zero"); + is( dz_A(2, 1, 0), [2, 1, 0], "Ends on a zero"); + + done_testing; +} |
