aboutsummaryrefslogtreecommitdiff
path: root/challenge-071
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-19 16:23:30 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-19 16:23:30 +0100
commit8d45f86c1e20b1d31ca574aa8be6aec8d10e89b7 (patch)
treeaf1c61cc10444e3196550dbdc76e1e4d0228b2ac /challenge-071
parent4ed1edb56f6452d5b64f2ec143345317955f31f8 (diff)
downloadperlweeklychallenge-club-8d45f86c1e20b1d31ca574aa8be6aec8d10e89b7.tar.gz
perlweeklychallenge-club-8d45f86c1e20b1d31ca574aa8be6aec8d10e89b7.tar.bz2
perlweeklychallenge-club-8d45f86c1e20b1d31ca574aa8be6aec8d10e89b7.zip
Add Perl solution to challenge 071
Diffstat (limited to 'challenge-071')
-rw-r--r--challenge-071/paulo-custodio/Makefile2
-rw-r--r--challenge-071/paulo-custodio/README1
-rw-r--r--challenge-071/paulo-custodio/perl/ch-1.pl44
-rw-r--r--challenge-071/paulo-custodio/perl/ch-2.pl82
-rw-r--r--challenge-071/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-071/paulo-custodio/t/test-2.yaml30
6 files changed, 169 insertions, 0 deletions
diff --git a/challenge-071/paulo-custodio/Makefile b/challenge-071/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-071/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-071/paulo-custodio/README b/challenge-071/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-071/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-071/paulo-custodio/perl/ch-1.pl b/challenge-071/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..3e8a429961
--- /dev/null
+++ b/challenge-071/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+# Challenge 071
+#
+# TASK #1 › Peak Element
+# Submitted by: Mohammad S Anwar
+# You are given positive integer $N (>1).
+#
+# Write a script to create an array of size $N with random unique elements
+# between 1 and 50.
+#
+# In the end it should print peak elements in the array, if found.
+#
+# An array element is called peak if it is bigger than it’s neighbour.
+#
+# Example 1
+# Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ]
+# Peak: [ 48, 45, 21 ]
+# Example 2
+# Array: [ 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ]
+# Peak: [ 47, 32, 39, 36 ]
+
+use Modern::Perl;
+
+my @n = @ARGV;
+say join(", ", peek_elem(@n));
+
+sub peek_elem {
+ my(@n) = @_;
+ my @out;
+ return @out if @n<2;
+ if ($n[0]>$n[1]) {
+ push @out, $n[0];
+ }
+ for my $i (1 .. @n-2) {
+ if ($n[$i]>$n[$i-1] && $n[$i]>$n[$i+1]) {
+ push @out, $n[$i];
+ }
+ }
+ if ($n[-1]>$n[-2]) {
+ push @out, $n[-1];
+ }
+ return @out;
+}
diff --git a/challenge-071/paulo-custodio/perl/ch-2.pl b/challenge-071/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..e707c19476
--- /dev/null
+++ b/challenge-071/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/env perl
+
+# Challenge 071
+#
+# TASK #2 › Trim Linked List
+# Submitted by: Mohammad S Anwar
+# You are given a singly linked list and a positive integer $N (>0).
+#
+# Write a script to remove the $Nth node from the end of the linked list and
+# print the linked list.
+#
+# If $N is greater than the size of the linked list then remove the first
+# node of the list.
+#
+# NOTE: Please use pure linked list implementation.
+#
+# Example
+# Given Linked List: 1 -> 2 -> 3 -> 4 -> 5
+# when $N = 1
+# Output: 1 -> 2 -> 3 -> 4
+# when $N = 2
+# Output: 1 -> 2 -> 3 -> 5
+# when $N = 3
+# Output: 1 -> 2 -> 4 -> 5
+# when $N = 4
+# Output: 1 -> 3 -> 4 -> 5
+# when $N = 5
+# Output: 2 -> 3 -> 4 -> 5
+# when $N = 6
+# Output: 2 -> 3 -> 4 -> 5
+
+use Modern::Perl;
+
+my $n = shift||1;
+my $list;
+while (@ARGV) {
+ $list = [pop @ARGV, $list];
+}
+my $len = list_len($list);
+my $remove = $n > $len ? 0 : $len-$n;
+$list = remove_n($remove, $list);
+show($list);
+
+sub list_len {
+ my($list) = @_;
+ my $len = 0;
+ while ($list) {
+ $len++;
+ $list = $list->[1];
+ }
+ return $len;
+}
+
+sub remove_n {
+ my($remove, $list) = @_;
+ if ($remove == 0) {
+ return $list->[1];
+ }
+ else {
+ my $p = $list;
+ my $last;
+ while ($p) {
+ if ($remove == 0) {
+ $last->[1] = $p->[1];
+ }
+ $last = $p;
+ $p = $p->[1];
+ $remove--;
+ }
+ return $list;
+ }
+}
+
+sub show {
+ my($list) = @_;
+ my @out;
+ while ($list) {
+ push @out, $list->[0];
+ $list = $list->[1];
+ }
+ say join(" ", @out);
+}
diff --git a/challenge-071/paulo-custodio/t/test-1.yaml b/challenge-071/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..6dd46333e5
--- /dev/null
+++ b/challenge-071/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 18 45 38 25 10 7 21 6 28 48
+ input:
+ output: 45, 21, 48
+- setup:
+ cleanup:
+ args: 47 11 32 8 1 9 39 14 36 23
+ input:
+ output: 47, 32, 39, 36
diff --git a/challenge-071/paulo-custodio/t/test-2.yaml b/challenge-071/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..e6a55c8822
--- /dev/null
+++ b/challenge-071/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,30 @@
+- setup:
+ cleanup:
+ args: 1 1 2 3 4 5
+ input:
+ output: 1 2 3 4
+- setup:
+ cleanup:
+ args: 2 1 2 3 4 5
+ input:
+ output: 1 2 3 5
+- setup:
+ cleanup:
+ args: 3 1 2 3 4 5
+ input:
+ output: 1 2 4 5
+- setup:
+ cleanup:
+ args: 4 1 2 3 4 5
+ input:
+ output: 1 3 4 5
+- setup:
+ cleanup:
+ args: 5 1 2 3 4 5
+ input:
+ output: 2 3 4 5
+- setup:
+ cleanup:
+ args: 6 1 2 3 4 5
+ input:
+ output: 2 3 4 5