diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-27 16:44:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-27 16:44:03 +0100 |
| commit | 9c4ccb2621871bc1b99cd7ffb673f040f1b8307f (patch) | |
| tree | 452c1106eb1a21ee1065d2697019f39bf57ead95 | |
| parent | f1e76b84d182ad09fe83832ce8a598a7d4aa77ee (diff) | |
| parent | 7e024efdae17d8000dd9ab43be60f6e642f48227 (diff) | |
| download | perlweeklychallenge-club-9c4ccb2621871bc1b99cd7ffb673f040f1b8307f.tar.gz perlweeklychallenge-club-9c4ccb2621871bc1b99cd7ffb673f040f1b8307f.tar.bz2 perlweeklychallenge-club-9c4ccb2621871bc1b99cd7ffb673f040f1b8307f.zip | |
Merge pull request #7971 from PerlBoy1967/branch-for-challenge-214
w214 - Task 1
| -rwxr-xr-x | challenge-214/perlboy1967/perl/ch1.pl | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-214/perlboy1967/perl/ch1.pl b/challenge-214/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..0f6027a815 --- /dev/null +++ b/challenge-214/perlboy1967/perl/ch1.pl @@ -0,0 +1,56 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 214 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-214 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Rank Score +Submitted by: Mohammad S Anwar + +You are given a list of scores (>=1). + +Write a script to rank each score in descending order. First three will get medals +i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get the ranking number. + +|| Using the standard model of giving equal scores equal rank, then advancing that +|| number of ranks. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; +use Test::Deep; + +use List::MoreUtils qw(uniq indexes); + +sub rankScore { + state $medals = {1=>'G', 2=>'S', 3=>'B'}; + my @uniqScores = sort { $b <=> $a } uniq @_; + my ($rank,@ranks) = (1); + + for my $score (@uniqScores) { + my @indexes = indexes { $_ == $score } @_; + map { $ranks[$_] = $medals->{$rank} // $rank } @indexes; + $rank += scalar(@indexes); + } + + return @ranks; + +} + +cmp_deeply([rankScore(1,2,4,3,5)],[qw(5 4 S B G)]); +cmp_deeply([rankScore(8,5,6,7,4)],[qw(G 4 B S 5)]); +cmp_deeply([rankScore(3,5,4,2)],[qw(B G S 4)]); +cmp_deeply([rankScore(2,5,2,1,7,5,1)],[qw(4 S 4 6 G S 6)]); +cmp_deeply([rankScore(1,2,3,3,3,3)],[qw(6 5 G G G G)]); +cmp_deeply([rankScore(1,2,2,3,3)],[qw(5 B B G G)]); + +done_testing; + + |
