aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-05 21:34:08 +0100
committerGitHub <noreply@github.com>2024-05-05 21:34:08 +0100
commit9b790ff60b2f2de6fd672516cffcbf3abcf0cbbe (patch)
treeeac8b0c0c2b22ea0d142de91bde3c333e863ddb2
parenta683088864740e55b7dfcacab3745886b88f58b7 (diff)
parent0278564217adba916238fec1b953397542b008ec (diff)
downloadperlweeklychallenge-club-9b790ff60b2f2de6fd672516cffcbf3abcf0cbbe.tar.gz
perlweeklychallenge-club-9b790ff60b2f2de6fd672516cffcbf3abcf0cbbe.tar.bz2
perlweeklychallenge-club-9b790ff60b2f2de6fd672516cffcbf3abcf0cbbe.zip
Merge pull request #10043 from BarrOff/barroff-267
feat: add solutions for challenge 267 from BarrOff
-rw-r--r--challenge-267/barroff/bqn/ch-1.bqn7
-rw-r--r--challenge-267/barroff/julia/ch-1.jl17
-rw-r--r--challenge-267/barroff/nim/ch_1.nim25
-rw-r--r--challenge-267/barroff/perl/ch-1.pl28
-rw-r--r--challenge-267/barroff/raku/ch-1.p624
5 files changed, 101 insertions, 0 deletions
diff --git a/challenge-267/barroff/bqn/ch-1.bqn b/challenge-267/barroff/bqn/ch-1.bqn
new file mode 100644
index 0000000000..06aeb03fd4
--- /dev/null
+++ b/challenge-267/barroff/bqn/ch-1.bqn
@@ -0,0 +1,7 @@
+#/usr/bin/env bqn
+
+productSign ← { (0<⊢)◶((0>⊢)◶0‿¯1)‿1 ×´ }
+
+•Show ProductSign ⟨-1, -2, -3, -4, 3, 2, 1⟩
+•Show ProductSign ⟨1, 2, 0, -2, -1⟩
+•Show ProductSign ⟨-1, -1, 1, -1, 2⟩
diff --git a/challenge-267/barroff/julia/ch-1.jl b/challenge-267/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..d847f6b7bf
--- /dev/null
+++ b/challenge-267/barroff/julia/ch-1.jl
@@ -0,0 +1,17 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function product_sign(ints::Vector{T})::Int where {T<:Integer}
+ if 0 in ints
+ return 0
+ end
+ return length(filter(x -> x < 0, ints)) % 2 == 0 ? 1 : -1
+
+end
+
+@testset "product sign" begin
+ @test product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1
+ @test product_sign([1, 2, 0, -2, -1]) == 0
+ @test product_sign([-1, -1, 1, -1, 2]) == -1
+end
diff --git a/challenge-267/barroff/nim/ch_1.nim b/challenge-267/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..fd1af785b1
--- /dev/null
+++ b/challenge-267/barroff/nim/ch_1.nim
@@ -0,0 +1,25 @@
+import std/unittest
+
+from std/sequtils import countIt
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+func product_sign[T: SomeInteger](ints: openArray[T]): int =
+ if 0 in ints:
+ return 0
+ let
+ negativeCount = countIt(ints, it < 0)
+ if negativeCount mod 2 == 0:
+ return 1
+ return -1
+
+suite "product sign":
+ test "[-1, -2, -3, -4, 3, 2, 1]":
+ check(product_sign([-1, -2, -3, -4, 3, 2, 1]) == 1)
+
+ test "[1, 2, 0, -2, -1]":
+ check(product_sign([1, 2, 0, -2, -1]) == 0)
+
+ test "[-1, -1, 1, -1, 2]":
+ check(product_sign([-1, -1, 1, -1, 2]) == -1)
diff --git a/challenge-267/barroff/perl/ch-1.pl b/challenge-267/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..0d2d21ab22
--- /dev/null
+++ b/challenge-267/barroff/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub product_sign (@ints) {
+ if (grep { $_ == 0 } @ints) {
+ return 0;
+ }
+ return grep( { $_ < 0 } @ints) % 2 == 0 ? 1 : -1;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say product_sign(@ARGV);
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 3;
+ is product_sign(-1, -2, -3, -4, 3, 2, 1), 1, 'works for (-1, -2, -3, -4, 3, 2, 1)';
+ is product_sign(1, 2, 0, -2, -1), 0, 'works for (1, 2, 0, -2, -1)';
+ is product_sign(-1, -1, 1, -1, 2), -1, 'works for (-1, -1, 1, -1, 2)';
+ }
+}
+
+MAIN();
diff --git a/challenge-267/barroff/raku/ch-1.p6 b/challenge-267/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..1633a7b61f
--- /dev/null
+++ b/challenge-267/barroff/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub product-sign(@ints --> Int) {
+ return 0 if 0 (elem) @ints;
+ return grep({ $_ < 0 }, @ints).elems % 2 == 0 ?? 1 !! -1;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is product-sign([-1, -2, -3, -4, 3, 2, 1]), 1,
+ 'works for (-1, -2, -3, -4, 3, 2, 1)';
+ is product-sign([1, 2, 0, -2, -1]), 0, 'works for (1, 2, 0, -2, -1)';
+ is product-sign([-1, -1, 1, -1, 2]), -1, 'works for (-1, -1, 1, -1, 2)';
+}
+
+#| Take user provided number like 10 1 111 24 1000
+multi sub MAIN(*@ints) {
+ say product-sign(@ints);
+}