aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-21 01:06:25 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-21 01:06:25 +0100
commitc495343d52dae9415a6ea527beaf33bbed923bad (patch)
tree539979b30766053c9e42ad8f770b086b449dd022 /challenge-078
parent57cdc88f2c0481d83653ff79a53733ce3174d805 (diff)
downloadperlweeklychallenge-club-c495343d52dae9415a6ea527beaf33bbed923bad.tar.gz
perlweeklychallenge-club-c495343d52dae9415a6ea527beaf33bbed923bad.tar.bz2
perlweeklychallenge-club-c495343d52dae9415a6ea527beaf33bbed923bad.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-078')
-rw-r--r--challenge-078/colin-crain/perl/ch-1.pl64
-rw-r--r--challenge-078/colin-crain/perl/ch-2.pl119
-rw-r--r--challenge-078/colin-crain/raku/ch-1.raku51
-rw-r--r--challenge-078/colin-crain/raku/ch-2.raku103
4 files changed, 337 insertions, 0 deletions
diff --git a/challenge-078/colin-crain/perl/ch-1.pl b/challenge-078/colin-crain/perl/ch-1.pl
new file mode 100644
index 0000000000..a36ff8652d
--- /dev/null
+++ b/challenge-078/colin-crain/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#! /opt/local/bin/perl
+#
+# follow_the_leader.pl
+#
+# TASK #1 › Leader Element
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input: @A = (9, 10, 7, 5, 6, 1)
+# Output: (10, 7, 6, 1)
+# Example 2:
+# Input: @A = (3, 4, 5)
+# Output: (5)
+#
+# method:
+# working from the end, the tail element is by definition the
+# "leader" of none. From there a running local maximum is
+# established among elements already seen and the focus moves one
+# element to the left. If the element is greater than the local
+# maximum, it is the leader and becomes the new maximum. It is also
+# unshifted onto the front of the output array.
+#
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+
+my @input = @ARGV;
+@input = (9, 10, 7, 5, 6, 1) if scalar @ARGV == 0;
+say "input: (", (join ', ', @input), ")";
+
+
+my @output = my ($max) = (pop @input);
+
+while (@input) {
+ my $ele = pop @input;
+ if ($ele > $max) {
+ $max = $ele;
+ unshift @output, $ele;
+ }
+}
+say "output: (", (join ', ', @output), ")";
+
+
+
+
+
+
+
+## ## ## ## ## SUBS:
diff --git a/challenge-078/colin-crain/perl/ch-2.pl b/challenge-078/colin-crain/perl/ch-2.pl
new file mode 100644
index 0000000000..cd07ef3383
--- /dev/null
+++ b/challenge-078/colin-crain/perl/ch-2.pl
@@ -0,0 +1,119 @@
+#! /opt/local/bin/perl
+#
+# swing_to_the_left.pl
+#
+# TASK #2 › Left Rotation
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input:
+# @A = (10 20 30 40 50)
+# @B = (3 4)
+#
+# Explanation:
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th
+# index member in the array.
+# [40 50 10 20 30]
+#
+# b) We left rotate the 4th index element (50) in the @A to make it 0th
+# index member in the array.
+# [50 10 20 30 40]
+#
+# Output:
+# [40 50 10 20 30]
+# [50 10 20 30 40]
+#
+# Example 2:
+#
+# Input:
+# @A = (7 4 2 6 3)
+# @B = (1 3 4)
+#
+# Explanation:
+# a) We left rotate the 1st index element (4) in the @A to make it
+# 0th index member in the array.
+# [4 2 6 3 7]
+#
+# b) We left rotate the 3rd index element (6) in the @A to make it
+# 0th index member in the array.
+# [6 3 7 4 2]
+#
+# c) We left rotate the 4th index element (3) in the @A to make it
+# 0th index member in the array.
+# [3 7 4 2 6]
+#
+# Output:
+# [4 2 6 3 7]
+# [6 3 7 5 2]
+# [3 7 4 2 6]
+
+# ----------------------------------------
+#
+# method:
+# I'm not sure "rotate" is exactly the right word for whats being
+# asked for here, but rather what we are being asked to do is to
+# produce the output as though the array had been rotated. The
+# distinction is important as we need to retain the integrety of the
+# original array and perform multiple transformations for output, as
+# the transformations are not consecutivly applied in a churning
+# fashion, but rather different applications to the atarting
+# sequence.
+#
+# The new sequences rearrange the elements such that the the tail of
+# the array, starting at the indicated element, becomes the head,
+# and the head, up to the break, becomes the tail. This can be done
+# easily with array slices.
+#
+# As removing the head element and placing it on the end is the act
+# of a left rotation, this is the same action moving a block of
+# elements simultaniously rather than individual elements
+# sequentially. In this sense it satisfies both the function and the
+# spirit of the challenge.
+#
+#
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+
+
+my @array = (7, 4, 2, 6, 3);
+my @idxs = (1, 3, 0, 4);
+my @output;
+my $end = scalar @array - 1;
+
+for my $idx (@idxs) {
+ $idx == 0 and do {(push @output, \@array); next};
+ my @shifted = @array[$idx..$end, 0..$idx-1];
+ push @output, \@shifted ;
+}
+
+say "array: (", (join ', ', @array), ")";
+say "index list: (", (join ', ', @idxs ), ")\n";
+
+say "[", (join ' ', $_->@*), "]" for @output;
+
+
+
+
+
+
+
+
+
+
+## ## ## ## ## SUBS:
diff --git a/challenge-078/colin-crain/raku/ch-1.raku b/challenge-078/colin-crain/raku/ch-1.raku
new file mode 100644
index 0000000000..e528ed3e06
--- /dev/null
+++ b/challenge-078/colin-crain/raku/ch-1.raku
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl6
+#
+#
+# follow-the-leader.raku
+#
+# TASK #1 › Leader Element
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input: @A = (9, 10, 7, 5, 6, 1)
+# Output: (10, 7, 6, 1)
+# Example 2:
+# Input: @A = (3, 4, 5)
+# Output: (5)
+#
+# method: working from the end, the tail element is by definition the
+# "leader" of none. From there a running local maximum is
+# established among elements already seen and the focus moves one
+# element to the left. If the element is greater than the local
+# maximum, it is the leader and becomes the new maximum. Leaders are
+# propgated to the output array, which needs to be revered back to
+#
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (*@input) ;
+
+## in
+@input.elems == 0 and @input = 9, 10, 7, 5, 6, 1;
+
+## work
+my $max = -Inf;
+my @output = @input.reverse
+ .map( { $_ > $max ?? ($max = $_) !! Nil } )
+ .grep( *.defined );
+@output .= reverse;
+
+## out
+say " input: ", @input;
+say "output: ", @output;
+
diff --git a/challenge-078/colin-crain/raku/ch-2.raku b/challenge-078/colin-crain/raku/ch-2.raku
new file mode 100644
index 0000000000..c491571918
--- /dev/null
+++ b/challenge-078/colin-crain/raku/ch-2.raku
@@ -0,0 +1,103 @@
+#!/usr/bin/env perl6
+#
+#
+# swing_to_the_left.raku
+#
+# TASK #2 › Left Rotation
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Example 1:
+# Input:
+# @A = (10 20 30 40 50)
+# @B = (3 4)
+#
+# Explanation:
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th
+# index member in the array.
+# [40 50 10 20 30]
+#
+# b) We left rotate the 4th index element (50) in the @A to make it 0th
+# index member in the array.
+# [50 10 20 30 40]
+#
+# Output:
+# [40 50 10 20 30]
+# [50 10 20 30 40]
+#
+# Example 2:
+#
+# Input:
+# @A = (7 4 2 6 3)
+# @B = (1 3 4)
+#
+# Explanation:
+# a) We left rotate the 1st index element (4) in the @A to make it
+# 0th index member in the array.
+# [4 2 6 3 7]
+#
+# b) We left rotate the 3rd index element (6) in the @A to make it
+# 0th index member in the array.
+# [6 3 7 4 2]
+#
+# c) We left rotate the 4th index element (3) in the @A to make it
+# 0th index member in the array.
+# [3 7 4 2 6]
+#
+# Output:
+# [4 2 6 3 7]
+# [6 3 7 5 2]
+# [3 7 4 2 6]
+#
+# ----------------------------------------
+#
+# method:
+# I'm not sure "rotate" is exactly the right word for whats being
+# asked for here, as rather what we are being asked to do is to
+# produce the output as though the array had been rotated. The
+# distinction is important as we need to retain the integrity of the
+# original array and perform multiple transformations for output, as
+# the transformations are not consecutivly applied in a churning
+# fashion, but rather different applications to the starting
+# sequence.
+#
+# The new sequences rearrange the elements such that the the tail of
+# the array, starting at the indicated element, becomes the head,
+# and the head, up to the break, becomes the tail. This can be done
+# easily with array slices.
+#
+# As removing the head element and placing it on the end is the act
+# of a left rotation, this is the same action moving a block of
+# elements simultaniously rather than individual elements
+# sequentially. In this sense it satisfies both the function and the
+# spirit of the challenge.
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (Str $arraystr = "7,4,2,6,3", Str $idxstr = "1,3,0,4") ;
+
+## in
+my @array = $arraystr.split(',');
+my @idxs = $idxstr.split(',');
+my @output;
+
+## work
+for @idxs -> $idx {
+ $idx == 0 and do { @output.push: @array; next };
+ @output.push: @array[|($idx..@array.end), |^$idx];
+}
+
+## out
+say "array: ", @array;
+say "index list: ", @idxs;
+say "output:\n";
+.say for @output;