Skip to content

Add week 9 challenge #2 for Perl 6 #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions challenge-009/daniel-mita/perl6/ch-2.p6
Original file line number Diff line number Diff line change
@@ -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;
}