diff options
| -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; -} |
