From 0278564217adba916238fec1b953397542b008ec Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Sun, 5 May 2024 21:56:16 +0200 Subject: feat: add solutions for challenge 267 from BarrOff --- challenge-267/barroff/bqn/ch-1.bqn | 7 +++++++ challenge-267/barroff/julia/ch-1.jl | 17 +++++++++++++++++ challenge-267/barroff/nim/ch_1.nim | 25 +++++++++++++++++++++++++ challenge-267/barroff/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-267/barroff/raku/ch-1.p6 | 24 ++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 challenge-267/barroff/bqn/ch-1.bqn create mode 100644 challenge-267/barroff/julia/ch-1.jl create mode 100644 challenge-267/barroff/nim/ch_1.nim create mode 100644 challenge-267/barroff/perl/ch-1.pl create mode 100644 challenge-267/barroff/raku/ch-1.p6 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); +} -- cgit