diff options
| author | Daniel Mita <mienaikage@gmail.com> | 2019-05-25 11:17:59 +0100 |
|---|---|---|
| committer | Daniel Mita <mienaikage@gmail.com> | 2019-05-25 11:17:59 +0100 |
| commit | ceea31f239edb68312eec04c1629c4629254fe62 (patch) | |
| tree | 43022cd18891e5bce92f081cd180c5eac4ec5562 /challenge-009/daniel-mita | |
| parent | 977220b917861b980877fd3fb3988f9159b437f8 (diff) | |
| download | perlweeklychallenge-club-ceea31f239edb68312eec04c1629c4629254fe62.tar.gz perlweeklychallenge-club-ceea31f239edb68312eec04c1629c4629254fe62.tar.bz2 perlweeklychallenge-club-ceea31f239edb68312eec04c1629c4629254fe62.zip | |
Refactor week 9 challenge #2 for Perl 6
Diffstat (limited to 'challenge-009/daniel-mita')
| -rw-r--r-- | challenge-009/daniel-mita/perl6/ch-2.p6 | 71 |
1 files changed, 33 insertions, 38 deletions
diff --git a/challenge-009/daniel-mita/perl6/ch-2.p6 b/challenge-009/daniel-mita/perl6/ch-2.p6 index 9124e75dd8..b4db7d8758 100644 --- a/challenge-009/daniel-mita/perl6/ch-2.p6 +++ b/challenge-009/daniel-mita/perl6/ch-2.p6 @@ -3,45 +3,42 @@ 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, + Bool :help(:$h)! where *.so, #= Print help. + |, ) { - 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; - } + say GENERATE-USAGE(&?ROUTINE); } -#| "1334" Ranking. multi MAIN ( - Bool :modified(:$m)! where *.so, #= Use Modified Ranking. - *@scores where * ~~ ScoreList, + Bool :standard(:$s), #= Use Standard Ranking (1224). Default if no ranking is provided. + Bool :dense(:$d), #= Use Dense Ranking (1223). + Bool :modified(:$m) #= Use Modified Ranking (1334). + where { any(.one, .none) given @($s, $m, $d) }, + *@scores where { + .elems > 1 && $_ %% 2 && .[1,3…*].all ~~ Numeric + }, #= An even list of alternating names with scores. Higher score ranks better. ) { - 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; - } + my %table; + %table{.[1]}.push(.[0]) for @scores.rotor(2); + + say join "\n", gather { + for %table.keys.sort(&infix:«<=>»).reverse -> $score { + state $rank = $m ?? 0 !! 1; # Start at 0 if modified, else 1 + $rank += %table{$score}.elems if $m; # Add number of players at rank (modified) + "$rank: $_".take for %table{$score}.values.sort; # Set all players at current rank + $rank += %table{$score}.elems if none($m, $d); # Next rank = players at current rank (standard) + $rank++ if $d; # Next rank is +1 (dense) + } + }; } -#| "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. +multi GENERATE-USAGE ( + &main, + *@ where * !%% 2, + |, ) { - my %table = create-table(@scores); - for %table.keys.sort(&infix:«<=>»).reverse -> $score { - state $place = 1; - "$place: $_".say for %table{$score}.values.sort; - $place++; - } + "Error:\n Odd number of elements for scores.\n\n" ~ GENERATE-USAGE(&main); } multi GENERATE-USAGE ( @@ -49,11 +46,15 @@ multi GENERATE-USAGE ( 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); + "Error:\n Cannot use more than one rank type.\n\n" ~ GENERATE-USAGE(&main); } -multi GENERATE-USAGE(&main, |) { +multi GENERATE-USAGE ( + &main, + |, +) { $*USAGE ~ q:to/DOC/; @@ -67,9 +68,3 @@ multi GENERATE-USAGE(&main, |) { 4: baz DOC } - -sub create-table (Array $_) { - my %table; - %table{.[1]}.push(.[0]) for .rotor(2); - return %table; -} |
