DEV Community

oldtechaa
oldtechaa

Posted on

Perl Weekly Challenge #214 - Rank Score

Just one weekly challenge entry this week, because I am lacking in time and have no idea how to efficiently solve the second challenge.

So here goes:

Rank Score

First, the code:

#!/usr/bin/perl

use strict;
use v5.24;

my @sorted = reverse sort @ARGV;
my %hash;
my @table = ('G', 'S', 'B');
my $curr;

foreach (@sorted) {
    if ($curr <= 2) {
        $hash{$_} //= $table[$curr];
    } else {
        $hash{$_} //= $curr + 1;
    }
    $curr++;
}

say $hash{$_} for @ARGV;
Enter fullscreen mode Exit fullscreen mode

At first, I thought this challenge was something totally different, I don't know why, but it still turned out quite simply. We want to keep the output in the same order as the input, so we obviously can't just sort and replace the inputs. The easiest way is then to use each score as the key of a hash, where the value is the rank. Since all equal scores will be equally ranked, this sorts the scores from highest to lowest and iteratively assigns ranks. For any duplicates, the index counter continues counting but the defined-or ranks that entry in the hash as the first appropriate rank. For the first 3 ranks, the table of podium winners is used.

That's all for this week! As usual, if you have any comments by all means post them. I'll look forward to seeing the other solutions to challenge 2.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more