aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-10 15:25:11 +0100
committerGitHub <noreply@github.com>2020-08-10 15:25:11 +0100
commit64ab43a3f463180dbcba7165d2b0077b5cfc54af (patch)
tree62661c7d85fc566782f1a0ac9905fe2a00dfd061
parent67d235f2ed26008b3b99080cee2f6f378aa9dd80 (diff)
parent6b83dd8a38bf00bc40ef4690fabbfd3e5987e7fe (diff)
downloadperlweeklychallenge-club-64ab43a3f463180dbcba7165d2b0077b5cfc54af.tar.gz
perlweeklychallenge-club-64ab43a3f463180dbcba7165d2b0077b5cfc54af.tar.bz2
perlweeklychallenge-club-64ab43a3f463180dbcba7165d2b0077b5cfc54af.zip
Merge pull request #2065 from poizon/challenge-73-1
Added Perl solutions by Pavel Kuptsov for Task-1
-rw-r--r--challenge-073/pavel_kuptsov/README2
-rw-r--r--challenge-073/pavel_kuptsov/perl/ch-1.pl49
-rw-r--r--challenge-073/pavel_kuptsov/perl/ch-2.pl27
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-073/pavel_kuptsov/README b/challenge-073/pavel_kuptsov/README
new file mode 100644
index 0000000000..d4211cf62e
--- /dev/null
+++ b/challenge-073/pavel_kuptsov/README
@@ -0,0 +1,2 @@
+Solution by Pavel Kuptsov
+https://github.com/poizon
diff --git a/challenge-073/pavel_kuptsov/perl/ch-1.pl b/challenge-073/pavel_kuptsov/perl/ch-1.pl
new file mode 100644
index 0000000000..cb9fed9447
--- /dev/null
+++ b/challenge-073/pavel_kuptsov/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl -w
+use strict;
+use warnings;
+use v5.26;
+use DDP;
+# TASK #1 › Min Sliding Window
+# You are given an array of integers @A and sliding window size $S.
+
+# Write a script to create an array of min from each sliding window.
+
+# Example
+# Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3
+# Output: (0, 0, 0, 2, 3, 3, 4, 4)
+
+# [(1 5 0) 2 9 3 7 6 4 8] = Min (0)
+# [1 (5 0 2) 9 3 7 6 4 8] = Min (0)
+# [1 5 (0 2 9) 3 7 6 4 8] = Min (0)
+# [1 5 0 (2 9 3) 7 6 4 8] = Min (2)
+# [1 5 0 2 (9 3 7) 6 4 8] = Min (3)
+# [1 5 0 2 9 (3 7 6) 4 8] = Min (3)
+# [1 5 0 2 9 3 (7 6 4) 8] = Min (4)
+# [1 5 0 2 9 3 7 (6 4 8)] = Min (4)
+
+my @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8);
+my $S = 3;
+my @O = ();
+
+for (my $i=0; $i <= $#A; $i++)
+{
+ my $min = sliding($i);
+ next unless defined $min;
+ push @O, $min;
+}
+
+say join ',', @O;
+
+sub sliding
+{
+ my $slide = shift;
+ my @tmp = ();
+ my $max = $S-1 + $slide;
+
+ for ( my $i = $slide; $i <= $max; $i++ )
+ {
+ push @tmp, $A[$i] if defined $A[$max];
+ }
+
+ return (sort @tmp)[0];
+} \ No newline at end of file
diff --git a/challenge-073/pavel_kuptsov/perl/ch-2.pl b/challenge-073/pavel_kuptsov/perl/ch-2.pl
new file mode 100644
index 0000000000..30de2c8f87
--- /dev/null
+++ b/challenge-073/pavel_kuptsov/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl -w
+use strict;
+use warnings;
+use v5.26;
+use DDP;
+# TASK #2 › Smallest Neighbour
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers @A.
+# Write a script to create an array that represents the smallest element to the left of each corresponding index. If none found then use 0.
+
+# Example 1
+# Input: @A = (7, 8, 3, 12, 10)
+# Output: (0, 7, 0, 3, 3)
+
+# For index 0, the smallest number to the left of $A[0] is none, so we put 0.
+# For index 1, the smallest number to the left of $A[1] in (7), is 7 so we put 7.
+# For index 2, the smallest number to the left of $A[2] in (7, 8) is none, so we put 0.
+# For index 3, the smallest number to the left of $A[3] in (7, 8, 3) is 3, so we put 3.
+# For index 4, the smallest number to the left of $A[4] is (7, 8, 3, 12) is 3, so we put 3 again.
+
+# Example 2
+# Input: @A = (4, 6, 5)
+# Output: (0, 4, 4)
+
+# For index 0, the smallest number to the left of $A[0] is none, so we put 0.
+# For index 1, the smallest number to the left of $A[1] in (4) is 4, so we put 4.
+# For index 2, the smallest number to the left of $A[2] in (4, 6) is 4, so we put 4 again. \ No newline at end of file