diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-04 14:28:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-04 14:28:50 +0100 |
| commit | 031653895b6a4249ad96e4175e9514cbe78ff656 (patch) | |
| tree | 59f9c10096037e2fe0b0586cf095f0bad30a5f32 | |
| parent | 84ac3a3dadb3b8c522696ccd4c245cc1de557772 (diff) | |
| parent | 39cfda6be2cf4bbe5804aef1c4a6c7bcf313cea4 (diff) | |
| download | perlweeklychallenge-club-031653895b6a4249ad96e4175e9514cbe78ff656.tar.gz perlweeklychallenge-club-031653895b6a4249ad96e4175e9514cbe78ff656.tar.bz2 perlweeklychallenge-club-031653895b6a4249ad96e4175e9514cbe78ff656.zip | |
Merge pull request #2440 from wanderdoc/master
Solutions to challenge-080.
| -rw-r--r-- | challenge-080/wanderdoc/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-080/wanderdoc/perl/ch-2.pl | 59 |
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-080/wanderdoc/perl/ch-1.pl b/challenge-080/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..63b6cf07c6 --- /dev/null +++ b/challenge-080/wanderdoc/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given unsorted list of integers @N. Write a script to find out the smallest positive number missing. +Example 1: Input: @N = (5, 2, -2, 0) Output: 1 +Example 2: Input: @N = (1, 8, -1) Output: 2 +Example 3: Input: @N = (2, 0, -1) Output: 1 +=cut + + + + + + +use Test::More; + +sub smallest_positive_missing +{ + my @arr = @_; + + my $str = ''; + my @positives = grep $_ > 0, @arr; + + return "No positives!" unless @positives; + vec($str, $_, 1) = 1 for @positives; + my $bits = substr(unpack("b*", $str), 1); + return index($bits, 0) + 1; +} + +is(smallest_positive_missing(5, 2, -2, 0), 1, 'Example 1'); +is(smallest_positive_missing(1, 8, -1), 2, 'Example 2'); +is(smallest_positive_missing(2, 0, -1), 1, 'Example 3'); +is(smallest_positive_missing( -10), 'No positives!', 'Negative only'); +is(smallest_positive_missing(1 .. 10_000_000), 10000001, 'Many positives'); +done_testing(); diff --git a/challenge-080/wanderdoc/perl/ch-2.pl b/challenge-080/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..287de14fdd --- /dev/null +++ b/challenge-080/wanderdoc/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given rankings of $N candidates. Write a script to find out the total candies needed for all candidates. You are asked to follow the rules below: +a) You must given at least one candy to each candidate. +b) Candidate with higher ranking get more candies than their neighbours. +Example 1: +Input: $N = (1, 2, 2) + +Explanation: + +Applying rule #a, each candidate will get one candy. So total candies needed so far 3. Now applying rule #b, the first candidate do not get any more candy as its rank is lower than it's neighbours. The second candidate get one more candy as it's ranking is higher than it's neighbour. Finally the third candidate do not get any extra candy as it's ranking is not higher than neighbour. Therefore total candies required is 4. + +Output: 4 + +Example 2: +Input: $N = (1, 4, 3, 2) + +Explanation: + +Applying rule #a, each candidate will get one candy. So total candies needed so far 4. Now applying rule #b, the first candidate do not get any more candy as its rank is lower than it's neighbours. The second candidate get one more candy as it's ranking is higher than it's neighbour. The third candidate also get one more candy as it's ranking is higher than it's neighbour. Finally the fourth candidate do not get any extra candy as it's ranking is not higher than neighbour. Therefore total candies required is 6. + +Output: 6 + +=cut + +use Test::More; + + +is(count_candies(1, 2, 2), 4, 'Example 1'); +is(count_candies(1, 4, 3, 2), 6, 'Example 2'); +is(count_candies(2, 0, 2), 5, 'New Example'); + +done_testing(); + + + + +sub count_candies +{ + my @arr = @_; + + my $candies = scalar @arr; + return $candies if $candies == 1; + + $candies++ if ($arr[0] > $arr[1]); + + $candies++ if ($arr[$#arr] > $arr[$#arr - 1]); + + for my $i ( 1 .. $#arr - 1 ) + { + + $candies++ if ( $arr[$i] > $arr[$i - 1] or $arr[$i] > $arr[$i + 1] ); + } + return $candies; + +}
\ No newline at end of file |
