Created
December 29, 2019 21:51
-
-
Save adamcrussell/6c47ed5349ebdf4c6db5114f13b3f361 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 040
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Show multiple arrays content. | |
## | |
use Readonly; | |
Readonly::Array my @A => qw/I L O V E Y O U/; | |
Readonly::Array my @B => qw/2 4 0 3 2 0 1 9/; | |
Readonly::Array my @C => qw/! ? £ $ % ^ & */; | |
sub display{ | |
my($arrays) = @_; | |
my $max_index = (sort {$a <=> $b} map {scalar @{$_}} @{$arrays})[-1] - 1; | |
for my $x (0 .. $max_index){ | |
for my $a (@{$arrays}){ | |
print $a->[$x] // ""; | |
print "\t"; | |
} | |
print "\n"; | |
} | |
} | |
MAIN:{ | |
display([\@A, \@B, \@C]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# You are given a list of numbers and a set of indices | |
# belonging to the list. Write a script to sort the values | |
# that belong to the indices. | |
## | |
use Readonly; | |
Readonly::Array my @A => qw/10 4 1 8 12 3/; | |
Readonly::Array my @INDICES => qw/0 2 5/; | |
my @sorted = @A; | |
my @sorted_values = sort {$a <=> $b} map {$A[$_]} @INDICES; | |
my @sorted_indices = sort {$a <=> $b} @INDICES; | |
for my $i (@sorted_indices){ | |
$sorted[$i] = shift @sorted_values; | |
} | |
print join(", ", @sorted) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment