diff options
| author | Luis F. Uceta <uzluisf@protonmail.com> | 2019-05-25 14:22:16 -0400 |
|---|---|---|
| committer | Luis F. Uceta <uzluisf@protonmail.com> | 2019-05-25 14:22:16 -0400 |
| commit | 45b9f6d75bc69ab7af9638ff502cb19e0ab208f5 (patch) | |
| tree | 3ebf11741b9ee789904c584e215d8d59be91ecb8 | |
| parent | 92820cbe7a3f54afb517ca43fa033b9a2c033635 (diff) | |
| download | perlweeklychallenge-club-45b9f6d75bc69ab7af9638ff502cb19e0ab208f5.tar.gz perlweeklychallenge-club-45b9f6d75bc69ab7af9638ff502cb19e0ab208f5.tar.bz2 perlweeklychallenge-club-45b9f6d75bc69ab7af9638ff502cb19e0ab208f5.zip | |
Add challenge-009's files
| -rw-r--r-- | challenge-009/uzluisf/perl6/ModuleCH02.pm6 | 96 | ||||
| -rw-r--r-- | challenge-009/uzluisf/perl6/ch-01.p6 | 34 | ||||
| -rw-r--r-- | challenge-009/uzluisf/perl6/ch-02.p6 | 25 | ||||
| -rw-r--r-- | challenge-009/uzluisf/perl6/ch-02.t | 18 |
4 files changed, 173 insertions, 0 deletions
diff --git a/challenge-009/uzluisf/perl6/ModuleCH02.pm6 b/challenge-009/uzluisf/perl6/ModuleCH02.pm6 new file mode 100644 index 0000000000..270705f570 --- /dev/null +++ b/challenge-009/uzluisf/perl6/ModuleCH02.pm6 @@ -0,0 +1,96 @@ +unit module ModuleCH02; + +=begin pod + +=head1 Rankings + +=item B«Standard Ranking (1224)»: Items that compare equal receive the same + ranking number, and then a gap is left right after any set of equal-ranking + items. + +For instance, let (A, B, C, D) be a group of scores where A is ranked above +any other item, B and C compare equal and D follows the items comparing equal. + +"A", which ranks above any other items, receives ranking number 1. Then, B +and C receive the following ranking number, 2. Because D follows a group of +equal-ranking items, it receives the ranking number after the gap at 3. +Thus, D is ranked fourth (4). + +Therefore, we have the ranking 1224. + +=item B«Modified Ranking (1334)»: Items that compare equal receive the same + ranking number however, unlike standard ranking, a gap is left right before + any set of equal-ranking items. + +"A", which ranks above any other items, receives ranking number 1. Then, B +and C receive the following ranking number. However, their ranking number +is one plus the gap at 2. Thus, they receive the ranking number 3. D receives +the ranking number 4. + +Therefore, we have the ranking 1334. + +=item B«Dense Ranking (1223)»: Items that compare equally receive the same + ranking number, and the next item(s) receive the immediately following + ranking number. + +"A", which ranks above any other items, receives ranking number 1. Then, B and +C receive the immediately following ranking number, 2. Same for D, +which receives the ranking number 3. + +Therefore, we have the ranking 1223. + +=head1 Subroutines +=end pod + +=begin comment +Challenge 009#2: +Write a script to perform the different types of ranking described above. +=end comment + +proto ranking( *@items where *.elems > 0, | ) is export { * } + +#| Perform standard competition ranking with given @items. +multi ranking( *@items --> Array ) { + my Int:D @rankings = 1; + + for 1..@items.end -> $i { + @rankings[$i] = do + if @items[$i] cmp @items[$i-1] == Same { @rankings[$i - 1] } + else { $i + 1 } + } + return @rankings; +} + +#| Perform modified competition ranking with given @items. +multi ranking( *@items, :$modified! --> Array ) { + my Int:D @rankings = 1; + + for 1..@items.end -> $i { + @rankings[$i] = do + if @items[$i] cmp @items[$i-1] === Same { + @rankings[$i-1] + } + elsif @items[$i-2] and + @items[$i-1] cmp @items[$i-2] === Same and + @items[$i] ≠ (@items[$i-1], @items[$i-2]).all + { + @rankings[$i-1] + 1 + } + else { + @rankings[$i-1] + 2 + } + } + return @rankings; +} + +#| Perform dense competition ranking with given @items. +multi ranking( *@items, :$dense! --> Array ) { + my Int:D @rankings = 1; + + for 1..@items.end -> $i { + @rankings[$i] = do + if @items[$i] cmp @items[$i-1] === Same { @rankings[$i - 1] } + else { @rankings.tail + 1 } + } + return @rankings; +} diff --git a/challenge-009/uzluisf/perl6/ch-01.p6 b/challenge-009/uzluisf/perl6/ch-01.p6 new file mode 100644 index 0000000000..de4e23f55d --- /dev/null +++ b/challenge-009/uzluisf/perl6/ch-01.p6 @@ -0,0 +1,34 @@ +#!/usr/bin env perl6 + +=begin comment +Challenge 009#1: + +Write a script that finds the first square number that has at least 5 distinct +digits. +=end comment + +sub find-first-square( UInt:D $with-different-n-digits where * > 0 ) { + my @nums; + for 1..∞ { + my $square = $_ ** 2; + if has-at-least($square, $with-different-n-digits) { + @nums.push: $square; + return @nums if @nums == 5; + } + } + + sub has-at-least( Int:D $number, Int:D $num-of-digits ) { + my %digits = ($_ => True for $number.comb); + %digits == $num-of-digits; + } +} + +sub MAIN() { + put find-first-square(5); +} + +=begin comment +One-liner: + +(1..∞).map(* ** 2).grep(.comb.unique ≥ 5).head(5) +=end comment diff --git a/challenge-009/uzluisf/perl6/ch-02.p6 b/challenge-009/uzluisf/perl6/ch-02.p6 new file mode 100644 index 0000000000..3b1be86161 --- /dev/null +++ b/challenge-009/uzluisf/perl6/ch-02.p6 @@ -0,0 +1,25 @@ +#!/usr/bin env perl6 +use lib '.'; +use ModuleCH02; + +sub MAIN( + *@scores where *.elems > 0, #= Items to be ranked + Str :r(:$ranking) = "standard" #=«Ranking to be used. + Options: standard, modified and dense. + Default: standard» +) { + + unless $ranking ∈ <standard modified dense> { + note "Please provide an appropriate ranking option. " ~ + "See '$*PROGRAM -h' for help."; + return; + } + + my @rankings = do given $ranking { + when 'standard' { ranking @scores } + when 'modified' { ranking @scores, :modified } + when 'dense' { ranking @scores, :dense } + } + + put 'Rankings: ', @rankings.join('–'); +} diff --git a/challenge-009/uzluisf/perl6/ch-02.t b/challenge-009/uzluisf/perl6/ch-02.t new file mode 100644 index 0000000000..ac62a631d3 --- /dev/null +++ b/challenge-009/uzluisf/perl6/ch-02.t @@ -0,0 +1,18 @@ +#!/usr/bin env perl6 +use lib '.'; +use Test; +use ModuleCH02; + +my @scores = 5, 4, 4, 3; + +is-deeply ranking(@scores), + Array[Int:D].new(1, 2, 2, 4), + "Test standard ranking"; + +is-deeply ranking(@scores, :modified), + Array[Int:D].new(1, 3, 3, 4), + "Test modified ranking"; + +is-deeply ranking(@scores, :dense), + Array[Int:D].new(1, 2, 2, 3), + "Test dense ranking"; |
