aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-254/bruce-gray/perl/ch-1.pl21
-rw-r--r--challenge-254/bruce-gray/perl/ch-2.pl27
-rw-r--r--challenge-254/bruce-gray/raku/ch-1.raku59
-rw-r--r--challenge-254/bruce-gray/raku/ch-2.raku49
4 files changed, 156 insertions, 0 deletions
diff --git a/challenge-254/bruce-gray/perl/ch-1.pl b/challenge-254/bruce-gray/perl/ch-1.pl
new file mode 100644
index 0000000000..7e3583d633
--- /dev/null
+++ b/challenge-254/bruce-gray/perl/ch-1.pl
@@ -0,0 +1,21 @@
+use v5.36;
+use ntheory qw<rootint logint>;
+
+sub task1_Cube ($n) { $n==0 ? 1 : $n == rootint($n, 3) ** 3 }
+sub task1_PO3 ($n) { $n==0 ? 0 : $n == 3 ** logint($n, 3) }
+
+
+sub pm ($n) { [$n - 1, 0], [$n, 1], [$n + 1, 0] }
+my @tests_Cubes = ( [27,1],[0,1],[6,0], map { pm( $_ ** 3 ) } 2..100, 2**16 );
+my @tests_PO3 = ( [27,1],[0,0],[6,0], map { pm( 3 ** $_ ) } 2..32 );
+
+use Test::More;
+plan tests => @tests_Cubes + @tests_PO3;
+for (@tests_Cubes) {
+ my ($in, $expected) = @{$_};
+ is 0+task1_Cube($in), $expected, "Cubes : $in -> $expected";
+}
+for (@tests_PO3) {
+ my ($in, $expected) = @{$_};
+ is 0+task1_PO3($in), $expected, "PO3 : $in -> $expected";
+}
diff --git a/challenge-254/bruce-gray/perl/ch-2.pl b/challenge-254/bruce-gray/perl/ch-2.pl
new file mode 100644
index 0000000000..e836933519
--- /dev/null
+++ b/challenge-254/bruce-gray/perl/ch-2.pl
@@ -0,0 +1,27 @@
+use v5.36;
+sub task2 ( $word ) {
+ my @c = split '', $word;
+
+ my @k = grep { $c[$_] =~ /[aeiou]/i } keys @c;
+
+ @c[@k] = reverse @c[@k];
+
+ return ucfirst lc join '', @c;
+}
+
+
+my @tests = (
+ [ qw<Raku Ruka> ],
+ [ qw<Perl Perl> ],
+ [ qw<Julia Jaliu> ],
+ [ qw<Uiua Auiu> ],
+
+ [ qw<Bcdf Bcdf> ],
+ [ qw<Alphabet Elphabat> ],
+ [ qw<Zoologicoarchaeologist Ziologecaarchoiologost> ],
+);
+use Test::More; plan tests => 0+@tests;
+for (@tests) {
+ my ($in, $expected) = @{$_};
+ is task2($in), $expected, "$in -> $expected";
+}
diff --git a/challenge-254/bruce-gray/raku/ch-1.raku b/challenge-254/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..9c72e6590d
--- /dev/null
+++ b/challenge-254/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,59 @@
+multi sub task1_Cubes ( UInt $n --> Bool ) { $n == ( $n ** ⅓ ).round³ }
+multi sub task1_Cubes ( Int $n --> Bool ) { -$n == ( (-$n) ** ⅓ ).round³ }
+
+multi sub task1_PO3_log ( 0 --> Bool ) { False } # XXX 3 ** -Inf == 0
+multi sub task1_PO3_log ( UInt $n --> Bool ) { $n == 3 ** $n.log(3).round }
+
+sub task1_PO3_div ( UInt $n is copy --> Bool ) {
+ $n div= 3 while $n and $n %% 3;
+
+ return $n == 1;
+}
+
+use ntheory:from<Perl5> <rootint logint>;
+multi sub task1_Perl_Cube (UInt $n) { return $n == rootint( $n, 3)³ }
+multi sub task1_Perl_Cube (Int $n) { return (-$n) == rootint(-$n, 3)³ }
+multi sub task1_Perl_PO3 ( $n) { return $n == logint( $n, 3) R** 3 }
+multi sub task1_Perl_PO3 ( 0) { False }
+
+
+my @tests_Cubes =
+ ( 27, True ),
+ ( 0, True ),
+ ( 6, False ),
+
+ # Cubes could be negative; (-3)³ == -27
+ ( -6, False ),
+ ( -9, False ),
+ ( -27, True ),
+;
+my @tests_PO3 =
+ ( 27, True ),
+ ( 0, False ), # Different than the 0 test in Cubes, since 3**n == 0 for no `n`
+ ( 6, False ),
+;
+append @tests_Cubes, map { |(($_ - 1, False), ($_, True), ($_ + 1, False)) }, map { $_ ** 3 }, flat(2..100, 2**20);
+append @tests_PO3 , map { |(($_ - 1, False), ($_, True), ($_ + 1, False)) }, map { 3 ** $_ }, 2..39;
+
+my @subs_Cubes =
+ :&task1_Cubes,
+ :&task1_Perl_Cube,
+;
+my @subs_PO3 =
+ :&task1_PO3_log,
+ :&task1_PO3_div,
+ :&task1_Perl_PO3,
+;
+use Test;
+plan ( +@subs_PO3 * +@tests_PO3 )
+ + ( +@subs_Cubes * +@tests_Cubes );
+for @subs_PO3 -> ( :key($sub_name), :value(&task1) ) {
+ for @tests_PO3 -> ( Int $in, Bool $expected ) {
+ is task1($in), $expected, "PO3 : $sub_name $in -> $expected";
+ }
+}
+for @subs_Cubes -> ( :key($sub_name), :value(&task1) ) {
+ for @tests_Cubes -> ( Int $in, Bool $expected ) {
+ is task1($in), $expected, "Cubes : $sub_name $in -> $expected";
+ }
+}
diff --git a/challenge-254/bruce-gray/raku/ch-2.raku b/challenge-254/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..e0d169c318
--- /dev/null
+++ b/challenge-254/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,49 @@
+sub task2_substr ( Str $word is copy --> Str ) {
+ my @fs = $word.match(:g, /:i <[aeiou]>/)
+ .map({ .from, .Str });
+
+ for @fs Z @fs.reverse -> ( ($i, $ic), ($j, $jc) ) {
+ $word.substr-rw($i,1) = $jc.lc;
+ }
+
+ return $word.tclc;
+}
+sub task2_array ( Str $word --> Str ) {
+ my @a = $word.comb;
+
+ @a[ @a.grep(:k, /:i <[aeiou]>/) ] .= reverse;
+
+ return @a.join.tclc;
+}
+sub task2_split ( Str $word --> Str ) {
+ # .split() with `:v` captures the seperator in the even positions.
+ my @parts = $word.split(:v, /:i <[aeiou]>/);
+
+ my @k = @parts.keys.grep(* !%% 2);
+
+ @parts[@k] .= reverse;
+
+ return @parts.join.tclc;
+}
+
+my @tests =
+ <Raku Ruka>,
+ <Perl Perl>,
+ <Julia Jaliu>,
+ <Uiua Auiu>,
+
+ <Bcdf Bcdf>,
+ <Alphabet Elphabat>,
+ <Zoologicoarchaeologist Ziologecaarchoiologost>;
+;
+my @subs =
+ :&task2_substr,
+ :&task2_array,
+ :&task2_split,
+;
+use Test; plan +@tests * +@subs;
+for @subs -> ( :key($sub_name), :value(&task2) ) {
+ for @tests -> ( Str $in, Str $expected ) {
+ is task2($in), $expected, "$sub_name $in -> $expected";
+ }
+}