aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Wilson <steven1170@zoho.eu>2019-12-24 15:23:33 +0000
committerSteven Wilson <steven1170@zoho.eu>2019-12-24 15:23:33 +0000
commite2e520060b3b6b0e677ef39cf3a6f682d4b73d68 (patch)
tree3d100e6951df7b7b55172cbec3180614effb77d0
parentba19570c2b953edb5816d412e9079ce8ddd9403d (diff)
downloadperlweeklychallenge-club-e2e520060b3b6b0e677ef39cf3a6f682d4b73d68.tar.gz
perlweeklychallenge-club-e2e520060b3b6b0e677ef39cf3a6f682d4b73d68.tar.bz2
perlweeklychallenge-club-e2e520060b3b6b0e677ef39cf3a6f682d4b73d68.zip
add week 40 solutions
-rw-r--r--challenge-040/steven-wilson/perl5/ch-1.pl63
-rw-r--r--challenge-040/steven-wilson/perl5/ch-2.pl37
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-040/steven-wilson/perl5/ch-1.pl b/challenge-040/steven-wilson/perl5/ch-1.pl
new file mode 100644
index 0000000000..dc92f51ede
--- /dev/null
+++ b/challenge-040/steven-wilson/perl5/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# Author: Steven Wilson
+# Date: 2019-12-24
+# Week: 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;
+
+my @array_1 = qw[ I L O V E Y O U ];
+my @array_2 = qw[ 2 4 0 3 2 0 1 9 ];
+my @array_3 = qw[ ! ? £ $ % ^ & * ];
+my @arrays = [ \@array_1, \@array_2, \@array_3 ];
+
+display_arrays( \@arrays );
+
+sub display_arrays {
+ my $arrays_ref = shift;
+ my @arrays = @$arrays_ref;
+ for my $array_ref (@arrays) {
+ my @array = @$array_ref;
+ my $max_index = get_max_index($array_ref);
+ my $no_of_arrays = scalar @array;
+ for ( my $index = 0; $index < $max_index; $index++ ) {
+ for ( my $array_no = 0; $array_no < $no_of_arrays; $array_no++ ) {
+ defined @{$array[$array_no]}[$index]
+ ? print @{$array[$array_no]}[$index]
+ : print " ";
+ print " ";
+ }
+ print "\n";
+ }
+ }
+}
+
+sub get_max_index {
+ my $array_ref = shift;
+ my @arrays = @$array_ref;
+ my $max = 0;
+ for my $array_ref (@arrays) {
+ my @array = @$array_ref;
+ if ( scalar @array > $max ) {
+ $max = scalar @array;
+ }
+ }
+ return $max;
+}
diff --git a/challenge-040/steven-wilson/perl5/ch-2.pl b/challenge-040/steven-wilson/perl5/ch-2.pl
new file mode 100644
index 0000000000..d173e1c7e5
--- /dev/null
+++ b/challenge-040/steven-wilson/perl5/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+# Author: Steven Wilson
+# Date: 2019-12-24
+# Week: 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;
+use feature qw/ say /;
+
+my @list_of_numbers = ( 10, 4, 1, 8, 12, 3 );
+my @set_of_indices = ( 0, 2, 5 );
+
+say "Initial List:\t", join " ", @list_of_numbers;
+say "Indices:\t", join " ", @set_of_indices;
+
+my @sub_list = map { $list_of_numbers[$_] } @set_of_indices;
+my @sorted_sub_list = sort { $a <=> $b } @sub_list;
+
+# Put sorted values back into list at indices
+map { $list_of_numbers[ $set_of_indices[$_] ] = $sorted_sub_list[$_] }
+ ( 0 .. ( scalar @set_of_indices - 1 ) );
+
+say "Final List:\t", join " ", @list_of_numbers;