diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-28 05:21:39 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-28 05:21:39 +0100 |
| commit | 112e460c3f67b1d7c0a8c3cb31d26f113645eeb2 (patch) | |
| tree | de9f63affba8e9dc891b20c5520b83c2331e2b29 /challenge-080/andinus/README | |
| parent | 1e95beb93e91a87f7490daef571e2320d763da08 (diff) | |
| download | perlweeklychallenge-club-112e460c3f67b1d7c0a8c3cb31d26f113645eeb2.tar.gz perlweeklychallenge-club-112e460c3f67b1d7c0a8c3cb31d26f113645eeb2.tar.bz2 perlweeklychallenge-club-112e460c3f67b1d7c0a8c3cb31d26f113645eeb2.zip | |
- Added template for Challenge 080.
Diffstat (limited to 'challenge-080/andinus/README')
| -rw-r--r-- | challenge-080/andinus/README | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/challenge-080/andinus/README b/challenge-080/andinus/README new file mode 100644 index 0000000000..8455cdb3ee --- /dev/null +++ b/challenge-080/andinus/README @@ -0,0 +1,79 @@ + ━━━━━━━━━━━━━━━ + CHALLENGE 078 + ━━━━━━━━━━━━━━━ + + +Table of Contents +───────────────── + +1 Task 1 - Leader Element +.. 1.1 Perl +2 Task 2 - Left Rotation +.. 2.1 Perl + + + + + +1 Task 1 - Leader Element +═════════════════════════ + + You are given an array @A containing distinct integers. + + Write a script to find all leader elements in the array @A. Print (0) + if none found. + + • An element is leader if it is greater than all the elements to its + right side. + + +1.1 Perl +──────── + + • 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'. + ┌──── + │ my @leader; + │ MAIN: while (my $arg = shift @ARGV) { + │ foreach my $elm (@ARGV) { + │ next MAIN if $arg < $elm; + │ } + │ push @leader, $arg; + │ } + └──── + + +2 Task 2 - Left Rotation +════════════════════════ + + You are given array @A containing positive numbers and @B containing + one or more indices from the array @A. + + 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. + + +2.1 Perl +──────── + + • 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. + ┌──── + │ 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"; + │ } + └──── |
