summaryrefslogtreecommitdiff
path: root/009/ch2.pl
diff options
context:
space:
mode:
authorGuillermo Ramos2019-07-01 13:53:37 +0200
committerGuillermo Ramos2019-07-01 13:53:37 +0200
commitd916ca90c354b29cc1b0953508e9601576a4fd1e (patch)
treef1997d85fb0736cfb0abdf4cb534f81df8220df3 /009/ch2.pl
parent85d335507fbb74df316bd5831e621e1a37884c35 (diff)
downloadperlweekly-d916ca90c354b29cc1b0953508e9601576a4fd1e.tar.gz
[all] Formatting, missing comments in header, etc
Diffstat (limited to '009/ch2.pl')
-rwxr-xr-x009/ch2.pl66
1 files changed, 39 insertions, 27 deletions
diff --git a/009/ch2.pl b/009/ch2.pl
index 336dda0..d20def5 100755
--- a/009/ch2.pl
+++ b/009/ch2.pl
@@ -1,41 +1,53 @@
#!/usr/bin/env perl
+#
+# Write a script to perform different types of ranking as described below:
+#
+# 1. Standard Ranking (1224): Items that compare equal receive the same ranking
+# number, and then a gap is left in the ranking numbers.
+# 2. Modified Ranking (1334): It is done by leaving the gaps in the ranking
+# numbers before the sets of equal-ranking items.
+# 3. Dense Ranking (1223): Items that compare equally receive the same ranking
+# number, and the next item(s) receive the immediately following ranking number.
+#
+# (https://en.wikipedia.org/wiki/Ranking)
+##############################################################################
use strict;
use warnings;
sub rank {
- my $rank_type = shift;
- my @points = sort @_;
+ my $rank_type = shift;
+ my @points = sort @_;
- my %ranks;
- my $rank = $rank_type eq "modified" ? 0 : 1;
+ 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;
+ 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 ...";
+ or die "Usage: $0 {modified, standard, dense} rank1 rank2 ...";
my @points = @ARGV;
# Compute rankings
@@ -43,6 +55,6 @@ my $ranks = rank($rank_type, @points);
# Display rankings
for my $rank (sort (keys %$ranks)) {
- my @positions = @{$ranks->{$rank}};
- print "$rank. @positions\n";
+ my @positions = @{$ranks->{$rank}};
+ print "$rank. @positions\n";
}