summaryrefslogtreecommitdiff
path: root/009/ch2.pl
blob: 336dda0a6cacee9015207e07b757570ae10c5c7e (plain) (blame)
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
#!/usr/bin/env perl

use strict;
use warnings;

sub rank {
	my $rank_type = shift;
	my @points = sort @_;

	my %ranks;
	my $rank = $rank_type eq "modified" ? 0 : 1;

	my $interval_begin = 0;
	my $i;
	for ($i = 0; $i < @points; $i++) {
		my $point = $points[$i];
		if ($point != $points[$interval_begin]) {
			my @points_to_add = @points[$interval_begin .. ($i-1)];
			$rank += @points_to_add if $rank_type eq "modified";
			$ranks{$rank} = \@points_to_add;
			if ($rank_type eq "standard") {
				$rank += @points_to_add;
			} elsif ($rank_type eq "dense") {
				$rank = $rank + 1;
			}
			$interval_begin = $i;
		}
	}
	my @points_to_add = @points[$interval_begin .. ($i-1)];
	$rank += @points_to_add if $rank_type eq "modified";
	$ranks{$rank} = \@points_to_add;
	return \%ranks;
}

# Get rank type and points as CLI arguments
my $rank_type = shift || "";
grep /^$rank_type$/, qw(modified standard dense)
	or die "Usage: $0 {modified, standard, dense} rank1 rank2 ...";
my @points = @ARGV;

# Compute rankings
my $ranks = rank($rank_type, @points);

# Display rankings
for my $rank (sort (keys %$ranks)) {
	my @positions = @{$ranks->{$rank}};
	print "$rank. @positions\n";
}