diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 07:53:22 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 07:53:22 +0100 |
| commit | 3204a3bed980f50cbfa1eece85d8612a442c7b88 (patch) | |
| tree | 56a59d85517f12b4a85a55322a0cd0bd68af50b5 /challenge-080 | |
| parent | 8e430f3f7c63b38a0d595aa89a14baaa588e6f59 (diff) | |
| download | perlweeklychallenge-club-3204a3bed980f50cbfa1eece85d8612a442c7b88.tar.gz perlweeklychallenge-club-3204a3bed980f50cbfa1eece85d8612a442c7b88.tar.bz2 perlweeklychallenge-club-3204a3bed980f50cbfa1eece85d8612a442c7b88.zip | |
- Added Perl solutions to week 80.
Diffstat (limited to 'challenge-080')
| -rw-r--r-- | challenge-080/mohammad-anwar/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-080/mohammad-anwar/perl/ch-1.t | 43 | ||||
| -rw-r--r-- | challenge-080/mohammad-anwar/perl/ch-2.pl | 59 | ||||
| -rw-r--r-- | challenge-080/mohammad-anwar/perl/ch-2.t | 53 |
4 files changed, 203 insertions, 0 deletions
diff --git a/challenge-080/mohammad-anwar/perl/ch-1.pl b/challenge-080/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..15a961862b --- /dev/null +++ b/challenge-080/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 080 +# +# Task #1: Smallest Positive Number +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use strict; +use warnings; +use Data::Dumper; + +my $N = $ARGV[0] || "2, 3, 7, 6, 8, -1, -10, 15"; +printf("%s => %d\n", $N, smallest_positive_number(get_list($N))); + +# +# +# METHODS + +sub smallest_positive_number { + my ($array) = @_; + + my %numbers = (); + foreach my $n (@$array) { + next unless $n > 0; + $numbers{$n} = 1; + } + + my $i = 0; + foreach my $j (sort {$a <=> $b} keys %numbers) { + next if (++$i == $j); + return $i; + } + + return ++$i; +} + +sub get_list { + my ($l) = @_; + + die "ERROR: Missing list.\n" unless defined $l; + die "ERROR: Invalid list [$l].\n" unless ($l =~ /^[\-?\d\,?\s?]+$/); + + $l =~ s/\s//g; + return [ split /\,/, $l ]; +} diff --git a/challenge-080/mohammad-anwar/perl/ch-1.t b/challenge-080/mohammad-anwar/perl/ch-1.t new file mode 100644 index 0000000000..4312d4a022 --- /dev/null +++ b/challenge-080/mohammad-anwar/perl/ch-1.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 080 +# +# Task #1: Smallest Positive Number +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use strict; +use warnings; +use Test::More; + +is (smallest_positive_number([1, 2, 0]), 3, "testing (1, 2, 0)"); +is (smallest_positive_number([-8, -7, -6]), 1, "testing (-8, -7, -6)"); +is (smallest_positive_number([3, 4, -1, 1]), 2, "testing (3, 4, -1, 1)"); +is (smallest_positive_number([2, 3, 7, 6, 8, -1, -10, 15]), 1, "testing (2, 3, 7, 6, 8, -1, -10, 15)"); + + +done_testing; + +# +# +# METHODS + +sub smallest_positive_number { + my ($array) = @_; + + my %numbers = (); + foreach my $n (@$array) { + next unless $n > 0; + $numbers{$n} = 1; + } + + my $i = 0; + foreach my $j (sort {$a <=> $b} keys %numbers) { + next if (++$i == $j); + return $i; + } + + return ++$i; +} diff --git a/challenge-080/mohammad-anwar/perl/ch-2.pl b/challenge-080/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..7c1d3a684c --- /dev/null +++ b/challenge-080/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 080 +# +# Task #2: Count Candies +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use strict; +use warnings; +use List::Util qw(max); + +my $C = $ARGV[0] || "1, 5, 2, 1"; +printf("Total candies: %d\n", count_candies(get_rankings($C))); + +# +# +# METHODS + +sub count_candies { + my ($rankings) = @_; + + my $l2r = []; + my $r2l = []; + + push @$l2r, 1 for @$rankings; + push @$r2l, 1 for @$rankings; + + my $i = 1; + while ($i <= $#$rankings) { + $l2r->[$i] = $l2r->[$i - 1] + 1 + if ($rankings->[$i] > $rankings->[$i - 1]); + $i++ + } + + my $j = $#$rankings - 1; + while ($j >= 0) { + $r2l->[$j] = $r2l->[$j + 1] + 1 + if ($rankings->[$j] > $rankings->[$j + 1]); + $j--; + } + + my $count = 0; + $count += max($l2r->[$_], $r2l->[$_]) for (0 .. $#$rankings); + + return $count; +} + +sub get_rankings { + my ($c) = @_; + + die "ERROR: Missing rankings.\n" unless defined $c; + die "ERROR: Invalid rankings [$c].\n" unless ($c =~ /^[\d\,?\s?]+$/); + + $c =~ s/\s//g; + return [ split /\,/, $c ]; +} diff --git a/challenge-080/mohammad-anwar/perl/ch-2.t b/challenge-080/mohammad-anwar/perl/ch-2.t new file mode 100644 index 0000000000..3f62e234cb --- /dev/null +++ b/challenge-080/mohammad-anwar/perl/ch-2.t @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +# +# Perl Weekly Challenge - 080 +# +# Task #2: Count Candies +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use strict; +use warnings; +use Test::More; +use List::Util qw(max); + +is( count_candies([1, 2]), 3, "testing [1, 2]"); +is( count_candies([1, 2, 2]), 4, "testing [1, 2, 2]"); +is( count_candies([1, 5, 2, 1]), 7, "testing [1, 5, 2, 1]"); + +done_testing; + +# +# +# METHOD + +sub count_candies { + my ($rankings) = @_; + + my $l2r = []; + my $r2l = []; + + push @$l2r, 1 for @$rankings; + push @$r2l, 1 for @$rankings; + + my $i = 1; + while ($i <= $#$rankings) { + $l2r->[$i] = $l2r->[$i - 1] + 1 + if ($rankings->[$i] > $rankings->[$i - 1]); + $i++ + } + + my $j = $#$rankings - 1; + while ($j >= 0) { + $r2l->[$j] = $r2l->[$j + 1] + 1 + if ($rankings->[$j] > $rankings->[$j + 1]); + $j--; + } + + my $count = 0; + $count += max($l2r->[$_], $r2l->[$_]) for (0 .. $#$rankings); + + return $count; +} |
