diff options
| author | Daniel Mita <mienaikage@gmail.com> | 2019-05-25 02:02:19 +0100 |
|---|---|---|
| committer | Daniel Mita <mienaikage@gmail.com> | 2019-05-25 02:02:19 +0100 |
| commit | 24a5b7462d6aef66b690090d32c76d7888b15bd5 (patch) | |
| tree | 8833c60dceffb22dd44509a670294a926be212d1 /challenge-009 | |
| parent | cb8e123b768f5e299a6deee6576a6aa7250955ab (diff) | |
| download | perlweeklychallenge-club-24a5b7462d6aef66b690090d32c76d7888b15bd5.tar.gz perlweeklychallenge-club-24a5b7462d6aef66b690090d32c76d7888b15bd5.tar.bz2 perlweeklychallenge-club-24a5b7462d6aef66b690090d32c76d7888b15bd5.zip | |
Add week 9 challenge #2 for Perl 6
Diffstat (limited to 'challenge-009')
| -rw-r--r-- | challenge-009/daniel-mita/perl6/ch-2.p6 | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/challenge-009/daniel-mita/perl6/ch-2.p6 b/challenge-009/daniel-mita/perl6/ch-2.p6 new file mode 100644 index 0000000000..9124e75dd8 --- /dev/null +++ b/challenge-009/daniel-mita/perl6/ch-2.p6 @@ -0,0 +1,75 @@ +#!/usr/bin/env perl6 +use v6; + +my %*SUB-MAIN-OPTS = :named-anywhere; + +subset ScoreList of List where { .elems > 1 && $_ %% 2 && .[1,3…*].all ~~ Numeric }; + +#| "1224" Ranking. +multi MAIN ( + Bool :standard(:$s), #= Use Standard Ranking. Default if no ranking is provided. + *@scores where * ~~ ScoreList, +) { + my %table = create-table(@scores); + for %table.keys.sort(&infix:«<=>»).reverse -> $score { + state $place = 1; + "$place: $_".say for %table{$score}.values.sort; + $place += %table{$score}.elems; + } +} + +#| "1334" Ranking. +multi MAIN ( + Bool :modified(:$m)! where *.so, #= Use Modified Ranking. + *@scores where * ~~ ScoreList, +) { + my %table = create-table(@scores); + for %table.keys.sort(&infix:«<=>»).reverse -> $score { + state $place; + $place += %table{$score}.elems; + "$place: $_".say for %table{$score}.values.sort; + } +} + +#| "1223" Ranking. +multi MAIN ( + Bool :dense(:$d)! where *.so, #= Use Dense Ranking. + *@scores where * ~~ ScoreList, #= An even list of alternating names and scores. Higher score ranks higher. +) { + my %table = create-table(@scores); + for %table.keys.sort(&infix:«<=>»).reverse -> $score { + state $place = 1; + "$place: $_".say for %table{$score}.values.sort; + $place++; + } +} + +multi GENERATE-USAGE ( + &main, + Bool :standard(:$s), + Bool :modified(:$m), + Bool :dense(:$d) where { none(.one, .none) given @($s, $m, $d) }, +) { + "Error: Cannot use more than one rank type.\n\n" ~ GENERATE-USAGE(&main); +} + +multi GENERATE-USAGE(&main, |) { + $*USAGE ~ q:to/DOC/; + + + Example: + perl6 ch-2.p6 --dense foo 3 bar 8 baz 1 fizz 3 buzz 6 + + 1: bar + 2: buzz + 3: fizz + 3: foo + 4: baz + DOC +} + +sub create-table (Array $_) { + my %table; + %table{.[1]}.push(.[0]) for .rotor(2); + return %table; +} |
