The weekly challenge 279 - Task 1: Sort Letters
1 #!/usr/bin/env perl
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 use strict;
39 use warnings;
40
41 sort_letters( ['R', 'E', 'P', 'L'], [3, 2, 1, 4] );
42 sort_letters( ['A', 'U', 'R', 'K'], [2, 4, 1, 3] );
43 sort_letters( ['O', 'H', 'Y', 'N', 'P', 'T'], [5, 4, 2, 6, 1, 3] );
44
45 sub sort_letters {
46 my ($letters, $weights) = @_;
47 my @letters = @$letters;
48 print "Input: ('", join("', '", @$letters), "'), (", join(", ", @$weights), ")\n";
49 my @combined;
50 foreach my $i (0..$#letters) {
51 push @combined, [ $letters->[$i], $weights->[$i] ];
52 }
53 my $output = "";
54 foreach my $entry ( sort { $a->[1] <=> $b->[1] } @combined ) {
55 $output .= $entry->[0];
56 }
57 print "Output: $output\n";
58 }