/
ch-1.pl
executable file
·44 lines (36 loc) · 1.02 KB
/
ch-1.pl
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl
#
# You are given two array of languages and its popularity.
#
# Write a script to sort the language based on popularity.
# Example 1
#
# Input: @lang = ('perl', 'c', 'python')
# @popularity = (2, 1, 3)
# Output: ('c', 'perl', 'python')
#
# Example 2
#
# Input: @lang = ('c++', 'haskell', 'java')
# @popularity = (1, 3, 2)
# Output: ('c++', 'java', 'haskell')
#
use strict;
use warnings;
use Test::More;
use Data::Dumper;
my $cases = [
[ ['perl', 'c', 'python'], [2, 1, 3] ],
[ ['c++', 'haskell', 'java'], [1, 3, 2] ],
];
sub sort_language
{
my $lang = shift;
my $popularity = shift;
my %langpop = map { $lang->[$_] => $popularity->[$_] } (0..$#$lang);
return [ sort { $langpop{$a} <=> $langpop{$b}} keys %langpop ];
}
is_deeply(sort_language($cases->[0]->@*), ['c', 'perl', 'python'], "['perl', 'c', 'python'], [2, 1, 3]");
is_deeply(sort_language($cases->[1]->@*), ['c++', 'java', 'haskell'], "['c++', 'haskell', 'java'], [1, 3, 2]");
done_testing();
exit 0;