/
ch-1.pl
executable file
·86 lines (73 loc) · 2.17 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
use strict;
use warnings;
use English;
################################################################################
# Begin main execution
################################################################################
my @ranking_data_sets = (
[
[ 'perl', 'c', 'python' ],
[ 2, 1, 3 ]
],
[
[ 'c++', 'haskell', 'java' ],
[ 1, 3, 2 ]
]
);
print("\n");
foreach my $ranking_data (@ranking_data_sets){
printf(
"Input: \@lang = (%s)\n \@popularity = (%s)\nOutput: (%s)\n\n",
join(
", ",
map(
"'" . $_ . "'",
@{$ranking_data->[0]}
)
),
join(", ", @{$ranking_data->[1]}),
join(
", ",
map(
"'" . $_ . "'",
sort_languages_by_popularity($ranking_data)
)
)
);
}
exit(0);
################################################################################
# End main execution; subroutines follow
################################################################################
################################################################################
# Sort a list of languages by their popularity rankings
# Takes one argument:
# * A set of language rankings in the form of a ref to an array that contains
# a list of language names and a list of their respective popularity rankings
# (e.g.
# [
# [ "c++", "haskell", "java" ],
# [ 1, 3, 2 ]
# ]
# )
# Returns:
# * A list of language names sorted by their popularity (e.g.
# ( "c++", "java", "haskell" ) )
################################################################################
sub sort_languages_by_popularity{
return(
# 2: Create an array slice consisting of
# the language names, sorted in the order
# defined by the rankings
@{$ARG[0][0]}[
# 1: Create a set of array indices based on
# the language rankings sorted in ascending
# order
sort(
{ $ARG[0][1][$a] <=> $ARG[0][1][$b] }
0 .. $#{$ARG[0][0]}
)
]
);
}