diff options
| author | Andinus <andinus@nand.sh> | 2020-10-24 01:27:30 +0530 |
|---|---|---|
| committer | Andinus <andinus@nand.sh> | 2020-10-24 01:27:30 +0530 |
| commit | 7790390e48a88a4ec5f313e46f80e4c1884b17c8 (patch) | |
| tree | cc7e473cd8151c1a06651cea153cc15e1b6e8da0 /challenge-078 | |
| parent | 4943cf327b0b3e226447e25087770ed57c4fbfd7 (diff) | |
| download | perlweeklychallenge-club-7790390e48a88a4ec5f313e46f80e4c1884b17c8.tar.gz perlweeklychallenge-club-7790390e48a88a4ec5f313e46f80e4c1884b17c8.tar.bz2 perlweeklychallenge-club-7790390e48a88a4ec5f313e46f80e4c1884b17c8.zip | |
Add all README.org files
These files generate README, it's better to commit them than keep in
`.gitignore'.
Diffstat (limited to 'challenge-078')
| -rw-r--r-- | challenge-078/andinus/README.org | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-078/andinus/README.org b/challenge-078/andinus/README.org new file mode 100644 index 0000000000..f77cb87b00 --- /dev/null +++ b/challenge-078/andinus/README.org @@ -0,0 +1,54 @@ +#+SETUPFILE: ~/.emacs.d/org-templates/level-2.org +#+HTML_LINK_UP: ../../writings/ +#+OPTIONS: toc:2 +#+EXPORT_FILE_NAME: index +#+TITLE: Challenge 078 + +* 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. +** 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=. +#+BEGIN_SRC perl +my @leader; +MAIN: while (my $arg = shift @ARGV) { + foreach my $elm (@ARGV) { + next MAIN if $arg < $elm; + } + push @leader, $arg; +} +#+END_SRC +* 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. +** 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. +#+BEGIN_SRC perl +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"; +} +#+END_SRC |
