aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-02-02 23:34:59 -0500
committerrir <rirans@comcast.net>2024-02-02 23:36:11 -0500
commit7b687542caa8eae5659709a8ca6e63779d9b5197 (patch)
tree9f29c1283fc981b78f3b386e6c931d0cf234a6d3
parent5e224c8e3dd0d5cd02436fd97337c9ac8add03e8 (diff)
downloadperlweeklychallenge-club-7b687542caa8eae5659709a8ca6e63779d9b5197.tar.gz
perlweeklychallenge-club-7b687542caa8eae5659709a8ca6e63779d9b5197.tar.bz2
perlweeklychallenge-club-7b687542caa8eae5659709a8ca6e63779d9b5197.zip
254
-rw-r--r--challenge-254/0rir/raku/ch-1.raku68
-rw-r--r--challenge-254/0rir/raku/ch-2.raku66
2 files changed, 134 insertions, 0 deletions
diff --git a/challenge-254/0rir/raku/ch-1.raku b/challenge-254/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..7081fb7e89
--- /dev/null
+++ b/challenge-254/0rir/raku/ch-1.raku
@@ -0,0 +1,68 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+254-1: Three Power Submitted by: Mohammad S Anwar
+Given a positive integer, $n, write a script to return true if the
+given integer is a power of three otherwise return false.
+
+Example 1
+Input: $n = 27
+Output: true
+
+=end comment
+
+constant \Part-power = 40; # partitioning power
+constant \Part = 3**Part-power; # " value
+constant \Test-to-power = 100; # rough range for tests
+constant @test-powers = 3, * × 3 … 3**Test-to-power; # passing test values
+
+constant @standard = gather { # create standard for look up in the partition
+ state $count = 1;
+ state $value = 1;
+ while $count < Part-power {
+ take $value ×=3;
+ ++$count;
+ }
+}
+
+sub power3 ( Int $i is copy -->Bool) {
+ if $i ≤ @standard[*-1] {
+ if $i == @standard.any {
+ return True if $i == @standard.any;
+ } else {
+ return False;
+ }
+ } else {
+ my $shrunk = $i div @standard[*-1]; # shrink by a partition
+ return False if $i ÷ @standard[*-1] ≠ $shrunk; # not integer
+ return power3 $shrunk;
+ }
+ die "reached but did not grasp";
+}
+
+my @Test =
+ [ True, @test-powers, ],
+ [ False, @test-powers».&{ $_ + 1} ],
+ [ False, @test-powers».&{ $_ + 2} ],
+ [ False, @test-powers».&{ $_ + 3} ],
+;
+
+plan +@Test × @Test[0][1];
+
+for @Test -> @sub-test {
+ for @sub-test -> $exp, @t {
+ for @t -> $in {
+ is power3($in), $exp, "$exp <- power3( $in)";
+ }
+ }
+}
+
+done-testing;
+
+my $n = 3**1267;
+say "\nInput: \$n = $n\nOutput: ", power3( $n).Str.lc;
+$n += 9;
+say "\nInput: \$n = $n\nOutput: ", power3( $n).Str.lc;
diff --git a/challenge-254/0rir/raku/ch-2.raku b/challenge-254/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..831d20d632
--- /dev/null
+++ b/challenge-254/0rir/raku/ch-2.raku
@@ -0,0 +1,66 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+254-2: Reverse Vowels Submitted by: Mohammad S Anwar
+You are given a string, $s, reverse all the vowels (a, e, i, o, u) in
+the given string.
+
+Example 1
+Input: $s = "Raku"
+Output: "Ruka"
+Example 2
+Input: $s = "Perl"
+Output: "Perl"
+Example 3
+Input: $s = "Julia"
+Output: "Jaliu"
+Example 4
+Input: $s = "Uiua"
+Output: "Auiu"
+=end comment
+
+my @Test = [
+ "", "",
+ "' '", "' '",
+ "Url", "Url",
+ "url", "url",
+ "Raku", "Ruka",
+ "RAku", "RUka",
+ "RAkU", "RUkA",
+ "RakU", "RukA",
+ "Perl", "Perl",
+ "Julia", "Jaliu",
+ "Uiua", "Auiu",
+ "Juniper tea", "Janeper tiu"
+];
+
+plan @Test ÷ 2;
+
+sub vowel-reverse( Str $a --> Str) {
+ my @word = $a.comb;
+ my @vowel = @word.grep( / <[aAeEiIoOuU]>/)».lc;
+ return $a if +@vowel.chars <= 1;
+
+ my @uc-pos = @word.grep( /<[AEIOU]>/, :k);
+ my @v-pos = @word.grep: / <[aAeEiIoOuU]>/, :k ;
+ @word[@v-pos] = @vowel.reverse;
+ @word[@uc-pos]».=uc;
+ return @word.join;
+}
+
+for @Test -> $in, $exp {
+ is vowel-reverse($in), $exp, "$in -> $exp";
+}
+done-testing;
+
+my $s = qq{Et's nit POERL at's Paarl, but that wes tikan se ot's Pirl.};
+
+say "\nInput: \$s = $s\nOutput: &vowel-reverse( $s)";
+
+my $r = qq{It's not PEARL it's Pearl, but that was taken so it's Perl.};
+die unless vowel-reverse($s) eq $r;
+exit;
+