aboutsummaryrefslogtreecommitdiff
path: root/challenge-279/kjetillll/perl/ch-1.pl
blob: 929ff0b1c7a64b5e6455a3037679078bcc619e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use strict; use warnings; use Test::More tests=>3;

sub sort_letters_by_weight {
    my @letters = @{ shift() };
    my @weights = @{ shift() };

    my %w; @w{@letters} = @weights;

    #do the Schwartzian:        https://en.wikipedia.org/wiki/Schwartzian_transform
    return join '',
    map $$_[0],
    sort{ $$a[1] <=> $$b[1] || $$a[0] cmp $$b[0] }
    map [$_, $w{$_}],
    @letters


    ##this return instead is a lot simpler but could be slower for large inputs
    ##since there would be more hash lookups:
    #return join '', sort{ $w{$a} <=> $w{$b} || $a cmp $b } @letters
}

is sort_letters_by_weight( $$_{letters}, $$_{weights} ), $$_{output} for
  { letters => ['R', 'E', 'P', 'L'],           weights => [3, 2, 1, 4],       output  => 'PERL' },
  { letters => ['A', 'U', 'R', 'K'],           weights => [2, 4, 1, 3],       output  => 'RAKU' },
  { letters => ['O', 'H', 'Y', 'N', 'P', 'T'], weights => [5, 4, 2, 6, 1, 3], output  => 'PYTHON' }