aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-05-25 19:31:26 +0100
committerGitHub <noreply@github.com>2019-05-25 19:31:26 +0100
commitbeee09f8e5e2ecd71ce37d8b53d42d0ac18a591e (patch)
tree3ebf11741b9ee789904c584e215d8d59be91ecb8
parent92820cbe7a3f54afb517ca43fa033b9a2c033635 (diff)
parent45b9f6d75bc69ab7af9638ff502cb19e0ab208f5 (diff)
downloadperlweeklychallenge-club-beee09f8e5e2ecd71ce37d8b53d42d0ac18a591e.tar.gz
perlweeklychallenge-club-beee09f8e5e2ecd71ce37d8b53d42d0ac18a591e.tar.bz2
perlweeklychallenge-club-beee09f8e5e2ecd71ce37d8b53d42d0ac18a591e.zip
Merge pull request #177 from uzluisf/uzluisf-009
Add challenge-009's files
-rw-r--r--challenge-009/uzluisf/perl6/ModuleCH02.pm696
-rw-r--r--challenge-009/uzluisf/perl6/ch-01.p634
-rw-r--r--challenge-009/uzluisf/perl6/ch-02.p625
-rw-r--r--challenge-009/uzluisf/perl6/ch-02.t18
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";