aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-05 20:29:01 +0000
committerGitHub <noreply@github.com>2024-02-05 20:29:01 +0000
commit3d828341f18a81d0a15728443bde4bf64f9950ef (patch)
tree1039f24e08d5403adfdb7707e9181b74cb015911
parent04c92115f9c30fedf9839a7d0c2e0705914a35a8 (diff)
parent58f310b6a8abc7f65d6690ea071844f97ad00d75 (diff)
downloadperlweeklychallenge-club-3d828341f18a81d0a15728443bde4bf64f9950ef.tar.gz
perlweeklychallenge-club-3d828341f18a81d0a15728443bde4bf64f9950ef.tar.bz2
perlweeklychallenge-club-3d828341f18a81d0a15728443bde4bf64f9950ef.zip
Merge pull request #9529 from BarrOff/barroff-254
feat: add solutions for challenge 254 from BarrOff
-rw-r--r--challenge-254/barroff/d/ch_1.d32
-rw-r--r--challenge-254/barroff/julia/ch-1.jl14
-rw-r--r--challenge-254/barroff/nim/ch_1.nim22
-rw-r--r--challenge-254/barroff/perl/ch-1.pl28
-rw-r--r--challenge-254/barroff/raku/ch-1.p624
-rw-r--r--challenge-254/barroff/raku/ch-2.p624
-rw-r--r--challenge-254/barroff/v/ch-1.v20
-rwxr-xr-xchallenge-254/barroff/v/ch-1_test.v15
8 files changed, 179 insertions, 0 deletions
diff --git a/challenge-254/barroff/d/ch_1.d b/challenge-254/barroff/d/ch_1.d
new file mode 100644
index 0000000000..d01771cf1d
--- /dev/null
+++ b/challenge-254/barroff/d/ch_1.d
@@ -0,0 +1,32 @@
+#!/usr/bin/env -S rdmd -unittest
+
+import std.conv, std.math;
+
+bool three_power(int n)
+{
+ foreach (x; 0 .. to!int(sqrt(to!float(n))) + 1)
+ {
+ int tripple = x ^^ 3;
+ if (tripple > n)
+ {
+ break;
+ }
+ if (n == tripple)
+ {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+unittest
+{
+ assert(three_power(27));
+ assert(three_power(0));
+ assert(three_power(6) == false);
+}
+
+void main()
+{
+}
diff --git a/challenge-254/barroff/julia/ch-1.jl b/challenge-254/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..32b93c020d
--- /dev/null
+++ b/challenge-254/barroff/julia/ch-1.jl
@@ -0,0 +1,14 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function three_power(n::T) where {T<:Integer}
+ power_set = Set([x^3 for x = 0:ceil(sqrt(n))])
+ return n in power_set
+end
+
+@testset "three power" begin
+ @test three_power(27) == true
+ @test three_power(0) == true
+ @test three_power(6) == false
+end
diff --git a/challenge-254/barroff/nim/ch_1.nim b/challenge-254/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..ff10bad8aa
--- /dev/null
+++ b/challenge-254/barroff/nim/ch_1.nim
@@ -0,0 +1,22 @@
+import std/[sugar, unittest]
+
+from std/math import sqrt
+from std/sequtils import map, toSeq
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+proc three_power(n: Natural): bool =
+ let
+ powers = map(toSeq(0..int(sqrt(to!float(n)))), x => x * x * x)
+ return n in powers
+
+suite "three power":
+ test "27":
+ check(three_power(27) == true)
+
+ test "0":
+ check(three_power(0) == true)
+
+ test "6":
+ check(three_power(6) == false)
diff --git a/challenge-254/barroff/perl/ch-1.pl b/challenge-254/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..36de3e1a51
--- /dev/null
+++ b/challenge-254/barroff/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+# use Math qw/sqrt/;
+
+sub three_power ($n) {
+ my @powers = map( { $_**3 } 0 .. int( sqrt($n) ) );
+ return grep( { $n == $_ } @powers ) ? 1 : 0;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say three_power( $ARGV[1] );
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 3;
+ is three_power(27), 1, 'Works for 27';
+ is three_power(0), 1, 'Works for 0';
+ is three_power(6), 0, 'Works for 6';
+ }
+}
+
+MAIN();
diff --git a/challenge-254/barroff/raku/ch-1.p6 b/challenge-254/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..ab89140637
--- /dev/null
+++ b/challenge-254/barroff/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub three-power(Int:D $n where $n >= 0 --> Bool) {
+ my $powers = Set(map({ $_ ** 3 }, 0 .. Int(sqrt($n))));
+ return True if $n (elem) $powers;
+ return False;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is three-power(27), True, 'works for 27';
+ is three-power(0), True, 'works for 0';
+ is three-power(6), False, 'works for 6';
+}
+
+#| Take user provided number like 10
+multi sub MAIN(Int:D $n) {
+ say three-power($n);
+}
diff --git a/challenge-254/barroff/raku/ch-2.p6 b/challenge-254/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..4f9162457f
--- /dev/null
+++ b/challenge-254/barroff/raku/ch-2.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub reverse-vowels(Str:D $s --> Str:D) {
+ my @vowels = grep({ $_ ~~ m:g:i/<[aeiou]>/ }, $s.comb);
+ $s.subst(/:i <[aeiou]>/, { @vowels.pop()}, :g, :ii);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 4;
+
+ is reverse-vowels("Raku"), "Ruka", 'works for "Raku"';
+ is reverse-vowels("Perl"), "Perl", 'works for "Perl"';
+ is reverse-vowels("Julia"), "Jaliu", 'works for "Julia"';
+ is reverse-vowels("Uiua"), "Auiu", 'works for "Uiua"';
+}
+
+#| Take user provided word like aba
+multi sub MAIN(Str:D $s) {
+ say reverse-vowels($s);
+}
diff --git a/challenge-254/barroff/v/ch-1.v b/challenge-254/barroff/v/ch-1.v
new file mode 100644
index 0000000000..fdad0a833c
--- /dev/null
+++ b/challenge-254/barroff/v/ch-1.v
@@ -0,0 +1,20 @@
+#!/usr/bin/env -S v run
+
+module main
+
+import math
+
+fn three_power(n int) bool {
+ mut res := false
+ for i in 0 .. math.sqrti(n) {
+ tripple := i * i * i
+ if n == tripple {
+ res = true
+ break
+ }
+ if n > tripple {
+ break
+ }
+ }
+ return res
+}
diff --git a/challenge-254/barroff/v/ch-1_test.v b/challenge-254/barroff/v/ch-1_test.v
new file mode 100755
index 0000000000..d654e3a3c1
--- /dev/null
+++ b/challenge-254/barroff/v/ch-1_test.v
@@ -0,0 +1,15 @@
+#!/usr/bin/env -S v -stats test
+
+module main
+
+fn test_27() {
+ assert three_power(27) == true
+}
+
+fn test_0() {
+ assert three_power(0) == true
+}
+
+fn test_6() {
+ assert three_power(6) == true
+}