diff options
| author | Andinus <andinus@nand.sh> | 2020-09-29 00:12:53 +0530 |
|---|---|---|
| committer | Andinus <andinus@nand.sh> | 2020-09-29 00:12:53 +0530 |
| commit | 8b8c38e4361cfc9ce1d73cbd25d63f3565e4c289 (patch) | |
| tree | ff1158a447ffd8df5266f4b281201867d968212d /challenge-080 | |
| parent | aa14cbf8342e04b936f40bcc720a23a258137ecd (diff) | |
| download | perlweeklychallenge-club-8b8c38e4361cfc9ce1d73cbd25d63f3565e4c289.tar.gz perlweeklychallenge-club-8b8c38e4361cfc9ce1d73cbd25d63f3565e4c289.tar.bz2 perlweeklychallenge-club-8b8c38e4361cfc9ce1d73cbd25d63f3565e4c289.zip | |
Add challenge-080's ch-1, ch-2 solution in Perl
Diffstat (limited to 'challenge-080')
| -rw-r--r-- | challenge-080/andinus/README | 106 | ||||
| -rw-r--r-- | challenge-080/andinus/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-080/andinus/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-080/andinus/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-080/andinus/perl/ch-2.pl | 20 |
5 files changed, 119 insertions, 37 deletions
diff --git a/challenge-080/andinus/README b/challenge-080/andinus/README index 8455cdb3ee..825903731b 100644 --- a/challenge-080/andinus/README +++ b/challenge-080/andinus/README @@ -1,30 +1,28 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 078 + CHALLENGE 080 + + Andinus ━━━━━━━━━━━━━━━ Table of Contents ───────────────── -1 Task 1 - Leader Element +1 Task 1 - Smallest Positive Number Bits .. 1.1 Perl -2 Task 2 - Left Rotation +2 Task 2 - Count Candies .. 2.1 Perl -1 Task 1 - Leader Element -═════════════════════════ - - You are given an array @A containing distinct integers. +1 Task 1 - Smallest Positive Number Bits +════════════════════════════════════════ - Write a script to find all leader elements in the array @A. Print (0) - if none found. + You are given unsorted list of integers `@N'. - • An element is leader if it is greater than all the elements to its - right side. + Write a script to find out the smallest positive number missing. 1.1 Perl @@ -32,30 +30,57 @@ Table of Contents • Program: [file:perl/ch-1.pl] - We take input from `@ARGV', loop over it. And then we loop over the - elements at right, goto next if `$arg' is less than `$elm'. This will - push all the leader elements to `@leader'. + We take input from `@ARGV', sort it & remove all inputs less than 2. + We check if the smallest positive number is 2. + ┌──── + │ my @sorted = sort { $a <=> $b } @ARGV; + │ + │ # Print 1 if there are no positive numbers in @sorted. + │ print "1\n" and exit 0 if $sorted[$#sorted] < 1; + │ + │ while (my $arg = shift @sorted) { + │ next if $arg < 2; + │ print "2\n" and exit 0 unless $arg == 2; + │ last; + │ } + └──── + + Now we are sure the smallest positive number is not 1 or 2 & `@sorted' + doesn't contain any number less than 2, infact it doesn't contain any + number less than 3. + + We loop from `3 ... $sorted[$#sorted] + 1' & then over `@sorted' + array. The first number from the array is dropped if it's equal to + `$num'. If not then `$num' is the smallest positive number, we print + it & exit the `MAIN' loop. + + This won't print the smallest positive number if the user passed + consecutive set of numbers, we just add `print "$num\n"' at the end to + cover this case. ┌──── - │ my @leader; - │ MAIN: while (my $arg = shift @ARGV) { - │ foreach my $elm (@ARGV) { - │ next MAIN if $arg < $elm; + │ MAIN: foreach my $num (3 ... $sorted[$#sorted] + 1) { + │ foreach (@sorted) { + │ shift @sorted and next MAIN if $num == $_; + │ print "$num\n" and last MAIN; │ } - │ push @leader, $arg; + │ # Print the last element if it was a continous series of positive + │ # numbers. + │ print "$num\n"; │ } └──── -2 Task 2 - Left Rotation +2 Task 2 - Count Candies ════════════════════════ - You are given array @A containing positive numbers and @B containing - one or more indices from the array @A. + You are given rankings of `@N' candidates. - Write a script to left rotate @A so that the number at the first index - of @B becomes the first element in the array. Similary, left rotate @A - again so that the number at the second index of @B becomes the first - element in the array. + Write a script to find out the total candies needed for all + candidates. You are asked to follow the rules below: + + 1. You must given at least one candy to each candidate. + 2. Candidate with higher ranking get more candies than their mmediate + neighbors on either side. 2.1 Perl @@ -63,17 +88,24 @@ Table of Contents • Program: [file:perl/ch-2.pl] - Loop over `@B' & then rotate the elements. Same could've been done - with Left Rotation, I find this easier to understand. + Giving at least one day to all candidates. ┌──── - │ my @A = qw(10 20 30 40 50); - │ my @B = qw(3 4); - │ - │ foreach (@B) { - │ my @tmp = @A; - │ foreach (1 ... scalar @tmp - $_) { - │ unshift @tmp, pop @tmp; - │ } - │ print join(', ', @tmp), "\n"; + │ my $candies = scalar @ARGV; + └──── + + Handling first & last index, we do this outside the loop to keep it + simple. + ┌──── + │ $candies++ if $ARGV[0] > $ARGV[1]; + │ $candies++ if $ARGV[$#ARGV] > $ARGV[$#ARGV - 1]; + └──── + + Loop handles rest of the entries. + ┌──── + │ foreach my $index (1 ... $#ARGV - 1) { + │ $candies++ if $ARGV[$index] > $ARGV[$index - 1]; + │ $candies++ if $ARGV[$index] > $ARGV[$index + 1]; │ } + │ + │ print "$candies\n"; └──── diff --git a/challenge-080/andinus/blog-1.txt b/challenge-080/andinus/blog-1.txt new file mode 100644 index 0000000000..487c05db91 --- /dev/null +++ b/challenge-080/andinus/blog-1.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-080/#org54c0be2 diff --git a/challenge-080/andinus/blog-2.txt b/challenge-080/andinus/blog-2.txt new file mode 100644 index 0000000000..f55b1bd9b4 --- /dev/null +++ b/challenge-080/andinus/blog-2.txt @@ -0,0 +1 @@ +https://andinus.tilde.institute/pwc/challenge-080/#orgaf148fd diff --git a/challenge-080/andinus/perl/ch-1.pl b/challenge-080/andinus/perl/ch-1.pl new file mode 100755 index 0000000000..960d7137cc --- /dev/null +++ b/challenge-080/andinus/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +die "usage: ./ch-1.pl [space seperated numbers]\n" + unless scalar @ARGV; + +my @sorted = sort { $a <=> $b } @ARGV; + +# Print 1 if there are no positive numbers in @sorted. +print "1\n" and exit 0 if $sorted[$#sorted] < 1; + +while (my $arg = shift @sorted) { + next if $arg < 2; + print "2\n" and exit 0 unless $arg == 2; + last; +} + +MAIN: foreach my $num (3 ... $sorted[$#sorted] + 1) { + foreach (@sorted) { + shift @sorted and next MAIN if $num == $_; + print "$num\n" and last MAIN; + } + # Print the last element if it was a continous series of positive + # numbers. + print "$num\n"; +} diff --git a/challenge-080/andinus/perl/ch-2.pl b/challenge-080/andinus/perl/ch-2.pl new file mode 100755 index 0000000000..dbc6c26b5f --- /dev/null +++ b/challenge-080/andinus/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +die "usage: ./ch-2.pl [space seperated numbers]\n" + unless scalar @ARGV; + +my $candies = scalar @ARGV; + +# Handle first & last index. +$candies++ if $ARGV[0] > $ARGV[1]; +$candies++ if $ARGV[$#ARGV] > $ARGV[$#ARGV - 1]; + +foreach my $index (1 ... $#ARGV - 1) { + $candies++ if $ARGV[$index] > $ARGV[$index - 1]; + $candies++ if $ARGV[$index] > $ARGV[$index + 1]; +} + +print "$candies\n"; |
