diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2019-12-24 19:21:11 +0100 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2019-12-24 19:21:11 +0100 |
| commit | 2b602851a25b2cf3e34892d4d93daa42c88c08a3 (patch) | |
| tree | 4997e27a9e94dff8824439c34c666dea8d0f97a9 | |
| parent | d75a19313d0b6a045737abd05e20a56f5bf8ac0c (diff) | |
| download | perlweeklychallenge-club-2b602851a25b2cf3e34892d4d93daa42c88c08a3.tar.gz perlweeklychallenge-club-2b602851a25b2cf3e34892d4d93daa42c88c08a3.tar.bz2 perlweeklychallenge-club-2b602851a25b2cf3e34892d4d93daa42c88c08a3.zip | |
challenge-040 wanderdoc
| -rw-r--r-- | challenge-040/wanderdoc/perl5/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-040/wanderdoc/perl5/ch-2.pl | 56 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-040/wanderdoc/perl5/ch-1.pl b/challenge-040/wanderdoc/perl5/ch-1.pl index e69de29bb2..4b4a1c6214 100644 --- a/challenge-040/wanderdoc/perl5/ch-1.pl +++ b/challenge-040/wanderdoc/perl5/ch-1.pl @@ -0,0 +1,42 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 * +=cut +use utf8; # Source file encoding. + +binmode (STDOUT, ":encoding(cp850)"); # '£' in Windows cmd. + +my @arr_1 = qw(I L O V E Y O U); +my @arr_2 = qw(2 4 0 3 2 0 1 9); +my @arr_3 = qw(! ? £ $ % ^ & *); + + +for my $i ( 0 .. $#arr_1 ) +{ + + print join(' ', $arr_1[$i], $arr_2[$i], $arr_3[$i]), $/; +} +print "Now cheating with module: $/"; + + + +use List::MoreUtils qw(zip6); +print "@$_$/"for zip6 @arr_1, @arr_2, @arr_3;
\ No newline at end of file diff --git a/challenge-040/wanderdoc/perl5/ch-2.pl b/challenge-040/wanderdoc/perl5/ch-2.pl index e69de29bb2..e681abb85f 100644 --- a/challenge-040/wanderdoc/perl5/ch-2.pl +++ b/challenge-040/wanderdoc/perl5/ch-2.pl @@ -0,0 +1,56 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 ] + +=cut + + +my @array = (10, 4, 1, 8, 12, 3); +my @idx2sort = (0, 2, 5); + +my %val2sort; + +@val2sort{@idx2sort} = @array[@idx2sort]; + +my @idx_stack = sort {$a <=> $b } values %val2sort; + +my @new_array; +for my $i ( 0 .. $#array ) +{ + $new_array[$i] = exists $val2sort{$i} ? + shift @idx_stack : $array[$i]; + +} + +print join(', ', @new_array), $/; + + + +print "Now saving indices not values:", $/; + +@new_array = (); + +my @idx_sorted = + sort {$val2sort{$a} <=> $val2sort{$b}} keys %val2sort; + + + +for my $i ( 0 .. $#array ) +{ + $new_array[$i] = exists $val2sort{$i} ? + $array[shift @idx_sorted] : $array[$i]; +} + +print join(', ', @new_array), $/;
\ No newline at end of file |
