diff options
| author | Cris-HD <crisn7@hotmail.com> | 2020-10-04 14:16:39 +0200 |
|---|---|---|
| committer | Cris-HD <crisn7@hotmail.com> | 2020-10-04 14:16:39 +0200 |
| commit | 2bb8cae394210ee02d29dbb0af056e3cab513cc4 (patch) | |
| tree | b142f6ce1300dce778cfef05e5691330382bf0ae | |
| parent | a95c9c60f43dece00d7f04b5fa720732116cc1e2 (diff) | |
| download | perlweeklychallenge-club-2bb8cae394210ee02d29dbb0af056e3cab513cc4.tar.gz perlweeklychallenge-club-2bb8cae394210ee02d29dbb0af056e3cab513cc4.tar.bz2 perlweeklychallenge-club-2bb8cae394210ee02d29dbb0af056e3cab513cc4.zip | |
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 |
