diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-24 19:52:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-24 19:52:08 +0000 |
| commit | e7ae3f978bcc2736248c61ca12c3204cec891931 (patch) | |
| tree | 6a6ad31c038ad807ff61776f64900c072bcf24c9 | |
| parent | 3a25125e16395e31247c5ced183fd0d1ff23a33d (diff) | |
| parent | 8a2e3a05b2e105177977cb5392835a0b0f0a0ed6 (diff) | |
| download | perlweeklychallenge-club-e7ae3f978bcc2736248c61ca12c3204cec891931.tar.gz perlweeklychallenge-club-e7ae3f978bcc2736248c61ca12c3204cec891931.tar.bz2 perlweeklychallenge-club-e7ae3f978bcc2736248c61ca12c3204cec891931.zip | |
Merge pull request #1070 from andrezgz/challenge-040
challenge-040 andrezgz solution
| -rw-r--r-- | challenge-040/andrezgz/perl5/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-040/andrezgz/perl5/ch-2.pl | 29 |
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; |
