diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-24 19:41:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-24 19:41:33 +0000 |
| commit | bb89bfcf62e181a76b78bcd423e92d2cbb0291fa (patch) | |
| tree | 7ea859a919f1f8a13ee77131c0227223d3cea4dd /challenge-040 | |
| parent | 2b6fb69fe9e9deca2d6e9e4460ffc0544b58a192 (diff) | |
| parent | b11c49f8caa6bf68a235283a7b7aaa5aa7db731f (diff) | |
| download | perlweeklychallenge-club-bb89bfcf62e181a76b78bcd423e92d2cbb0291fa.tar.gz perlweeklychallenge-club-bb89bfcf62e181a76b78bcd423e92d2cbb0291fa.tar.bz2 perlweeklychallenge-club-bb89bfcf62e181a76b78bcd423e92d2cbb0291fa.zip | |
Merge pull request #1069 from wanderdoc/master
Perl Weekly Challenge 040
Diffstat (limited to 'challenge-040')
| -rw-r--r-- | challenge-040/wanderdoc/README | 1 | ||||
| -rw-r--r-- | challenge-040/wanderdoc/perl5/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-040/wanderdoc/perl5/ch-2.pl | 56 |
3 files changed, 121 insertions, 0 deletions
diff --git a/challenge-040/wanderdoc/README b/challenge-040/wanderdoc/README new file mode 100644 index 0000000000..3a326bf66c --- /dev/null +++ b/challenge-040/wanderdoc/README @@ -0,0 +1 @@ +Solution by wanderdoc.
\ No newline at end of file diff --git a/challenge-040/wanderdoc/perl5/ch-1.pl b/challenge-040/wanderdoc/perl5/ch-1.pl new file mode 100644 index 0000000000..6609b7d062 --- /dev/null +++ b/challenge-040/wanderdoc/perl5/ch-1.pl @@ -0,0 +1,64 @@ +#!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 + + +# Correction: there should be n arrays not just three. + +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(! ? £ $ % ^ & *); + + +print "@$_$/" for my_zip6(\@arr_1, \@arr_2, \@arr_3); + + +print "Now cheating with module: $/"; + + +use List::MoreUtils qw(zip6); +print "@$_$/" for zip6 @arr_1, @arr_2, @arr_3; + + +sub my_zip6 +{ + my @arefs = @_; + + my @result; + my $imax = $#{$arefs[0]}; + + for my $i ( 0 .. $imax ) + { + my @row; + for my $ar ( @arefs ) + { + push @row, $ar->[$i]; + + } + push @result, [@row]; + + } + return @result; +}
\ No newline at end of file diff --git a/challenge-040/wanderdoc/perl5/ch-2.pl b/challenge-040/wanderdoc/perl5/ch-2.pl new file mode 100644 index 0000000000..e681abb85f --- /dev/null +++ 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 |
