diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-04 14:46:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-04 14:46:08 +0100 |
| commit | 74c3e861d2a2349532468f68d5bb9616c033fbd1 (patch) | |
| tree | 633320c374406b50cc9784a2c51dcb80b70d5b77 | |
| parent | 588dfe187664f7194485a5ac3d845bcd4981582f (diff) | |
| parent | 2bb8cae394210ee02d29dbb0af056e3cab513cc4 (diff) | |
| download | perlweeklychallenge-club-74c3e861d2a2349532468f68d5bb9616c033fbd1.tar.gz perlweeklychallenge-club-74c3e861d2a2349532468f68d5bb9616c033fbd1.tar.bz2 perlweeklychallenge-club-74c3e861d2a2349532468f68d5bb9616c033fbd1.zip | |
Merge pull request #2444 from Cris-HD/branch-for-challenge-080
added challenges
| -rwxr-xr-x | challenge-080/cristian-heredia/perl/ch-1.pl | 52 | ||||
| -rwxr-xr-x | challenge-080/cristian-heredia/perl/ch-2.pl | 44 |
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-080/cristian-heredia/perl/ch-1.pl b/challenge-080/cristian-heredia/perl/ch-1.pl new file mode 100755 index 0000000000..78fdad42a7 --- /dev/null +++ b/challenge-080/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,52 @@ +=begin + +TASK #1 › Smallest Positive Number Bits +Submitted by: Mohammad S Anwar +You are given an 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 + +=end +=cut + +use strict; +use warnings; +use List::Util qw(min); + +#Variables +my @N = (1, 8, -1); +my @sort = sort @N; + + +onlyPositive(); +checkSmallest(); + +sub onlyPositive { + + foreach(my $i=0; $i<@sort; $i++) { + if ($sort[$i] < 0) { + splice @sort, $i, 1; + } + } +} + +sub checkSmallest { + + my @missing = map $sort[$_-1]+1..$sort[$_]-1, 1..@sort-1; + print "Output: ". min(@missing)."\n"; + +} + + + + diff --git a/challenge-080/cristian-heredia/perl/ch-2.pl b/challenge-080/cristian-heredia/perl/ch-2.pl new file mode 100755 index 0000000000..53a4a94b0f --- /dev/null +++ b/challenge-080/cristian-heredia/perl/ch-2.pl @@ -0,0 +1,44 @@ +=begin + +TASK #2 › Count Candies +Submitted by: Mohammad S Anwar +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 mmediate neighbors on either side. + +=end +=cut + +use strict; +use warnings; +use Data::Dumper; + +#Variables +my @N = (1, 4, 3, 2); +my $candies = @N; +my $lengh = @N; + +#print "Candies = $candies\n"; +secondRule(); +print "Output = $candies\n"; + +sub secondRule { + + foreach (my $i = 0; $i<$lengh; $i++) { + if ($i == 0 and $N[$i] > ($i + 1)) { + $candies++; + } + if ($i == ($lengh - 1) and $N[$i] > ($i + 1)) { + $candies++; + } + if ($N[$i] > ($i + 1) and $i != 0) { + $candies++; + } + if ($N[$i] > ($i - 1) and $i != 0) { + $candies++; + } + } +}
\ No newline at end of file |
