aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-214/barroff/raku/ch-1.pl47
-rw-r--r--challenge-214/barroff/raku/ch-1.raku39
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-214/barroff/raku/ch-1.pl b/challenge-214/barroff/raku/ch-1.pl
new file mode 100644
index 0000000000..d83911b18e
--- /dev/null
+++ b/challenge-214/barroff/raku/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use v5.36;
+use strict;
+use warnings;
+
+use experimental 'switch';
+
+use List::Util qw( first );
+
+sub get_position ( $n, @numbers ) {
+ return ( first( sub { $numbers[$_] == $n }, keys(@numbers) ) ) + 1;
+}
+
+sub replace_position ($position) {
+ given ($position) {
+ when ( $_ == 1 ) { return 'G' }
+ when ( $_ == 2 ) { return 'S' }
+ when ( $_ == 3 ) { return 'B' }
+ default { return $position }
+ }
+}
+
+sub rank_score (@numbers) {
+ my @sorted_numbers = sort( { $b <=> $a } @numbers );
+ my @ranks = map( get_position( $_, @sorted_numbers ), @numbers );
+ my @result = map( &replace_position($_), @ranks );
+ return \@result;
+}
+
+#| Run test cases
+sub main() {
+
+ use Test2::V0 qw( is plan );
+
+ plan 4;
+
+ is rank_score( 1, 2, 4, 3, 5 ), [ 5, 4, 'S', 'B', 'G' ],
+ 'works for (1,2,4,3,5)';
+ is rank_score( 8, 5, 6, 7, 4 ), [ 'G', 4, 'B', 'S', 5 ],
+ 'works for (8,5,6,7,4)';
+ is rank_score( 3, 5, 4, 2 ), [ 'B', 'G', 'S', 4 ], 'works for (3,5,4,2)';
+ is rank_score( 2, 5, 2, 1, 7, 5, 1 ), [ 4, 'S', 4, 6, 'G', 'S', 6 ],
+ 'works for (2,5,2,1,7,5,1)';
+}
+
+main();
diff --git a/challenge-214/barroff/raku/ch-1.raku b/challenge-214/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..90dcfec39c
--- /dev/null
+++ b/challenge-214/barroff/raku/ch-1.raku
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub get-position(Int @numbers, Int $n --> Int) {
+ @numbers.keys.first({ @numbers[$_] == $n }) + 1;
+}
+
+sub replace-position(Int $position) {
+ given $position {
+ when 1 { return 'G' }
+ when 2 { return 'S' }
+ when 3 { return 'B' }
+ default { return $position }
+ }
+}
+
+sub rank-score(Int @numbers --> List) {
+ my Int @sorted-numbers = sort(@numbers).reverse;
+ my @ranks = map({ get-position(@sorted-numbers, $_) }, @numbers);
+ return map( &replace-position, @ranks).list;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 4;
+
+ is rank-score(Array[Int].new(1,2,4,3,5)), (5,4,'S','B','G'), 'works for (1,2,4,3,5)';
+ is rank-score(Array[Int].new(8,5,6,7,4)), ('G',4,'B','S',5), 'works for (8,5,6,7,4)';
+ is rank-score(Array[Int].new(3,5,4,2)), ('B','G','S',4), 'works for (3,5,4,2)';
+ is rank-score(Array[Int].new(2,5,2,1,7,5,1)), (4,'S',4,6,'G','S',6), 'works for (2,5,2,1,7,5,1)';
+}
+
+#| Take user provided list like 1 2 2 3
+multi sub MAIN(*@elements where @elements.elems ≥ 1 && all(@elements) ~~ /^<[+]>?<[0..9]>+$/) {
+ my Int @int_elements = @elements;
+ say rank-score(@int_elements);
+}