aboutsummaryrefslogtreecommitdiff
path: root/challenge-078/andinus
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-09-21 14:20:42 +0800
committer冯昶 <seaker@qq.com>2020-09-21 14:20:42 +0800
commitbca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb (patch)
tree877181cfde26b706346d3468269e4674d75da772 /challenge-078/andinus
parentec09b571a6f2186fec8870a071a8d5d38596c850 (diff)
parent5ac16ac7e9826137e0da5597e954f4992c66205d (diff)
downloadperlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.gz
perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.tar.bz2
perlweeklychallenge-club-bca0c362c212fc0dadc5ed7d9a5e4fa1aece4bfb.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-078/andinus')
-rw-r--r--challenge-078/andinus/README69
-rw-r--r--challenge-078/andinus/blog-1.txt1
-rw-r--r--challenge-078/andinus/blog-2.txt1
-rwxr-xr-xchallenge-078/andinus/perl/ch-1.pl16
-rwxr-xr-xchallenge-078/andinus/perl/ch-2.pl15
5 files changed, 81 insertions, 21 deletions
diff --git a/challenge-078/andinus/README b/challenge-078/andinus/README
index 9437e98f28..8455cdb3ee 100644
--- a/challenge-078/andinus/README
+++ b/challenge-078/andinus/README
@@ -1,29 +1,30 @@
━━━━━━━━━━━━━━━
- CHALLENGE 077
+ CHALLENGE 078
━━━━━━━━━━━━━━━
Table of Contents
─────────────────
-1 Task 1 - Fibonacci Sum
+1 Task 1 - Leader Element
.. 1.1 Perl
+2 Task 2 - Left Rotation
+.. 2.1 Perl
-1 Task 1 - Fibonacci Sum
-════════════════════════
+1 Task 1 - Leader Element
+═════════════════════════
- You are given a positive integer `$N'.
+ You are given an array @A containing distinct integers.
- Write a script to find the total number of Fibonacci Numbers required
- to get `$N' on addition. You are NOT allowed to repeat a number. Print
- 0 if none found.
+ Write a script to find all leader elements in the array @A. Print (0)
+ if none found.
- *Note*: This solution is incomplete. Others have pushed complete
- solutions, look at those.
+ • An element is leader if it is greater than all the elements to its
+ right side.
1.1 Perl
@@ -31,22 +32,48 @@ Table of Contents
• Program: [file:perl/ch-1.pl]
- Make a list of all possible sums of `$input'.
+ 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 @sums;
- │ foreach my $num (0 ... $input / 2) {
- │ my $diff = $input - $num;
- │ push @sums, [$diff, $num];
+ │ my @leader;
+ │ MAIN: while (my $arg = shift @ARGV) {
+ │ foreach my $elm (@ARGV) {
+ │ next MAIN if $arg < $elm;
+ │ }
+ │ push @leader, $arg;
│ }
└────
- Loop over `@sums' & then print those sets which have both `$sums->[0]'
- & `$sums->[1]' in fibonacci series.
+
+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.
┌────
- │ sub is_fib { return Math::Fibonacci::isfibonacci(@_) }
+ │ my @A = qw(10 20 30 40 50);
+ │ my @B = qw(3 4);
- │ foreach (@sums) {
- │ next unless is_fib($_->[0]) and is_fib($_->[1]);
- │ say "$_->[0] + $_->[1]";
+ │ foreach (@B) {
+ │ my @tmp = @A;
+ │ foreach (1 ... scalar @tmp - $_) {
+ │ unshift @tmp, pop @tmp;
+ │ }
+ │ print join(', ', @tmp), "\n";
│ }
└────
diff --git a/challenge-078/andinus/blog-1.txt b/challenge-078/andinus/blog-1.txt
new file mode 100644
index 0000000000..9fcc375b27
--- /dev/null
+++ b/challenge-078/andinus/blog-1.txt
@@ -0,0 +1 @@
+https://andinus.tilde.institute/pwc/challenge-078/#org6981fcd
diff --git a/challenge-078/andinus/blog-2.txt b/challenge-078/andinus/blog-2.txt
new file mode 100644
index 0000000000..f5bded527d
--- /dev/null
+++ b/challenge-078/andinus/blog-2.txt
@@ -0,0 +1 @@
+https://andinus.tilde.institute/pwc/challenge-078/#org473ab7c
diff --git a/challenge-078/andinus/perl/ch-1.pl b/challenge-078/andinus/perl/ch-1.pl
new file mode 100755
index 0000000000..503c0163f6
--- /dev/null
+++ b/challenge-078/andinus/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+die "usage: ./ch-1.pl [space seperated numbers]\n" unless @ARGV;
+
+my @leader;
+MAIN: while (my $arg = shift @ARGV) {
+ foreach my $elm (@ARGV) {
+ next MAIN if $arg < $elm;
+ }
+ push @leader, $arg;
+}
+
+print join(', ', @leader), "\n";
diff --git a/challenge-078/andinus/perl/ch-2.pl b/challenge-078/andinus/perl/ch-2.pl
new file mode 100755
index 0000000000..ce17de1bde
--- /dev/null
+++ b/challenge-078/andinus/perl/ch-2.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+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";
+}