aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-040/andrezgz/perl5/ch-1.pl45
-rw-r--r--challenge-040/andrezgz/perl5/ch-2.pl29
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-040/andrezgz/perl5/ch-1.pl b/challenge-040/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..f05f91443f
--- /dev/null
+++ b/challenge-040/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-040/
+# Task #1
+# Show multiple arrays content
+# You are given two or more arrays. Write a script to display values of each list at a given index.
+#
+# For example:
+#
+# Array 1: [ I L O V E Y O U ]
+# Array 2: [ 2 4 0 3 2 0 1 9 ]
+# Array 3: [ ! ? £ $ % ^ & * ]
+# We expect the following output:
+#
+# I 2 !
+# L 4 ?
+# O 0 £
+# V 3 $
+# E 2 %
+# Y 0 ^
+# O 1 &
+# U 9 *
+
+use strict;
+use warnings;
+
+use v5.20;
+use feature qw(postderef);
+no warnings qw(experimental::postderef);
+
+my @arrays = (
+ [qw/ I L O V E Y O U /],
+ [qw/ 2 4 0 3 2 0 1 9 /],
+ [qw/ ! ? £ $ % ^ & * /]
+);
+
+my $max=0;
+for (@arrays) {
+ $max = $_->@* if ($_->@* > $max);
+}
+
+for my $i (0.. $max - 1) {
+ print $_->[$i].' ' for @arrays;
+ print $/;
+}
diff --git a/challenge-040/andrezgz/perl5/ch-2.pl b/challenge-040/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..ea3968e928
--- /dev/null
+++ b/challenge-040/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-040/
+# Task #2
+# Sort SubList
+# You are given a list of numbers and set of indices belong to the list.
+# Write a script to sort the values belongs to the indices.
+#
+# For example,
+#
+# List: [ 10, 4, 1, 8, 12, 3 ]
+# Indices: 0,2,5
+# We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3.
+#
+# Final List would look like below:
+#
+# List: [ 1, 4, 3, 8, 12, 10 ]
+
+use strict;
+use warnings;
+
+my @list = (10, 4, 1, 8, 12, 3);
+my @indexes = (0,2,5);
+
+my @sorted_values = sort {$a <=> $b} @list[@indexes];
+
+splice @list, $_, 1, shift @sorted_values for (@indexes);
+
+print join ',', @list;