diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-02 23:29:20 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-02 23:29:20 +0100 |
| commit | deeb51ac8e3a8cf29e105c2e09467b04dd3d18eb (patch) | |
| tree | d5f4e59522016e28e061ff4a970791000eb925db /challenge-215 | |
| parent | c2bcc8ac11ed87ca49a85282480c8c20b7cf323a (diff) | |
| download | perlweeklychallenge-club-deeb51ac8e3a8cf29e105c2e09467b04dd3d18eb.tar.gz perlweeklychallenge-club-deeb51ac8e3a8cf29e105c2e09467b04dd3d18eb.tar.bz2 perlweeklychallenge-club-deeb51ac8e3a8cf29e105c2e09467b04dd3d18eb.zip | |
- Added solutions by Jorg Sommrey.
- Added solutions by Robert DiCicco.
Diffstat (limited to 'challenge-215')
| -rw-r--r-- | challenge-215/robert-dicicco/perl/ch-2.pl | 72 | ||||
| -rw-r--r-- | challenge-215/robert-dicicco/raku/ch-2.raku | 71 |
2 files changed, 143 insertions, 0 deletions
diff --git a/challenge-215/robert-dicicco/perl/ch-2.pl b/challenge-215/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..2a247a48c3 --- /dev/null +++ b/challenge-215/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +=begin pod +----------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-05-02 +Challenge 215 Number Placement ( Perl ) +----------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; +use IO::Prompter; + +sub CheckZeroes { + my $arr = shift; + my $cnt = shift; + my $zcnt = $cnt * 2; + while($zcnt < scalar @$arr - 1) { + if ((@$arr[$zcnt] == 0) && (@$arr[$zcnt-1] != 1)) { + @$arr[$zcnt] = 1; + } + $zcnt++; + } + say "Output: 1 = ",@$arr; +} + +sub HowManyZeroes { + my $arr = shift; + my $z = 0; + my $zcnt = 0; + while ($z < scalar @$arr - 1) { + if (@$arr[$z] == 0) { + $zcnt++; + } + $z++; + } + return $zcnt; +} +my $testarr = prompt -num, 'Enter an array as a string of numbers '; +my @numbers = split(//,$testarr); +chomp(@numbers); + +my $count = prompt -num, 'Enter count '; + +#say "Input: \@numbers = ",@numbers; +#say "Count = ",$count; +my $zeroes = HowManyZeroes(\@numbers); +if ($zeroes < $count * 2) { + say "Output: 0"; +} else { + CheckZeroes(\@numbers,$count-1); +} + +=begin pod +----------------------------------------- +SAMPLE OUTPUT +perl .\NumberPlacement.pl +Enter an array as a string of numbers 10001 +Enter count 1 +Output: 1 = 10101 + +perl .\NumberPlacement.pl +Enter an array as a string of numbers 10001 +Enter count 2 +Output: 0 + +perl .\NumberPlacement.pl +Enter an array as a string of numbers 101000001 +Enter count 3 +Output: 1 = 101010101 +----------------------------------------- +=cut diff --git a/challenge-215/robert-dicicco/raku/ch-2.raku b/challenge-215/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..da1a5ce11c --- /dev/null +++ b/challenge-215/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku +#`{ +----------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-05-02 +Challenge 215 Number Placement ( Raku ) + +When entering, the first digit is the count, the remainder is the array +For example, the first example would be 1 1 0 0 0 1 +which stands for a count of one, with array 1 0 0 0 1 +----------------------------------------- +} +use v6; + +sub CheckZeroes(@arr is copy, $cnt) { + my $zcnt = $cnt * 2; + while $zcnt < @arr.elems - 1 { + if ((@arr[$zcnt] == 0) && (@arr[$zcnt-1] != 1)) { + @arr[$zcnt] = 1; + } + $zcnt++; + } + say "Output: 1 = ",@arr; +} + +sub HowManyZeroes(@arr) { + my $z = 0; + my $zcnt = 0; + while $z < (@arr.elems) - 1 { + if (@arr[$z] == 0) { + $zcnt++; + } + $z++; + } + return $zcnt; +} + +unit sub MAIN ($count where 0 <= $count <= 9, *@numbers where @numbers.elems > 0 && all(@numbers) ~~ UInt); +say "Input: \@numbers = ", @numbers; +say "Count = $count"; + +my $test = HowManyZeroes(@numbers); + +my $zeroes = HowManyZeroes(@numbers); + +if ($zeroes < $count * 2) { + say "Output: 0"; +} else { + CheckZeroes(@numbers,$count-1); +} + + +#`{ +----------------------------------------- +SAMPLE OUTPUT +raku .\NumberPlacement.rk 1 1 0 0 0 1 +Input: @numbers = [1 0 0 0 1] +Count = 1 +Output: 1 = [1 0 1 0 1] + +PS G:\Projects\Perl\Challenges> raku .\NumberPlacement.rk 2 1 0 0 0 1 +Input: @numbers = [1 0 0 0 1] +Count = 2 +Output: 0 + +PS G:\Projects\Perl\Challenges> raku .\NumberPlacement.rk 3 1 0 1 0 0 0 0 0 1 +Input: @numbers = [1 0 1 0 0 0 0 0 1] +Count = 3 +Output: 1 = [1 0 1 0 1 0 1 0 1] +----------------------------------------- +} |
