diff options
| -rw-r--r-- | challenge-262/barroff/bqn/ch-1.bqn | 7 | ||||
| -rw-r--r-- | challenge-262/barroff/julia/ch-1.jl | 13 | ||||
| -rw-r--r-- | challenge-262/barroff/nim/ch_1.nim | 19 | ||||
| -rw-r--r-- | challenge-262/barroff/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-262/barroff/raku/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-262/barroff/raku/ch-2.p6 | 25 |
6 files changed, 118 insertions, 0 deletions
diff --git a/challenge-262/barroff/bqn/ch-1.bqn b/challenge-262/barroff/bqn/ch-1.bqn new file mode 100644 index 0000000000..a91ac0ade3 --- /dev/null +++ b/challenge-262/barroff/bqn/ch-1.bqn @@ -0,0 +1,7 @@ +#/usr/bin/env bqn + +MaxPositiveNegative ← (+´0⊸<)⌈(+´0⊸>) + +•Show MaxPositiveNegative ⟨-3, 1, 2, -1, 3, -2, 4⟩ +•Show MaxPositiveNegative ⟨-1, -2, -3, 1⟩ +•Show MaxPositiveNegative ⟨1, 2⟩ diff --git a/challenge-262/barroff/julia/ch-1.jl b/challenge-262/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..210889d1de --- /dev/null +++ b/challenge-262/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function max_positive_negative(ints::Vector{T})::Int where {T<:Integer} + maximum([count(x -> x > 0, ints), count(x -> x < 0, ints)]) +end + +@testset "count even digits number" begin + @test max_positive_negative([-3, 1, 2, -1, 3, -2, 4]) == 4 + @test max_positive_negative([-1, -2, -3, 1]) == 3 + @test max_positive_negative([1, 2]) == 2 +end diff --git a/challenge-262/barroff/nim/ch_1.nim b/challenge-262/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..1acb7b5d00 --- /dev/null +++ b/challenge-262/barroff/nim/ch_1.nim @@ -0,0 +1,19 @@ +import std/unittest + +from std/sequtils import countIt + +# run tests with following command: +# nim c -r ch_1.nim + +func max_positive_negative[T: SomeInteger](ints: openArray[T]): T = + max(countIt(ints, it > 0), countIt(ints, it < 0)) + +suite "max positive negative": + test "[-3, 1, 2, -1, 3, -2, 4]": + check(max_positive_negative([-3, 1, 2, -1, 3, -2, 4]) == 4) + + test "[-1, -2, -3, 1]": + check(max_positive_negative([-1, -2, -3, 1]) == 3) + + test "[1, 2]": + check(max_positive_negative([1, 2]) == 2) diff --git a/challenge-262/barroff/perl/ch-1.pl b/challenge-262/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..b501411af5 --- /dev/null +++ b/challenge-262/barroff/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use v5.38; + +sub max_positive_negative (@ints) { + use List::Util qw /max/; + max( + scalar( grep( { $_ > 0 } @ints ) ), + scalar( grep( { $_ < 0 } @ints ) ) + ); +} + +sub MAIN() { + if (@ARGV) { + + #| Run on command line argument + say max_positive_negative(@ARGV); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + is max_positive_negative( -3, 1, 2, -1, 3, -2, 4 ), 4, + 'works for (-3, 1, 2, -1, 3, -2, 4)'; + is max_positive_negative( -1, -2, -3, 1 ), 3, + 'works for (-1, -2, -3, 1)'; + is max_positive_negative( 1, 2 ), 2, 'works for (1, 2)'; + } +} + +MAIN(); diff --git a/challenge-262/barroff/raku/ch-1.p6 b/challenge-262/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..90a0eddd06 --- /dev/null +++ b/challenge-262/barroff/raku/ch-1.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +use v6.d; + +sub max-positive-negative(@ints --> Int:D) { + max(grep({ $_ < 0 }, @ints).elems, grep({ $_ > 0 }, @ints).elems) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is max-positive-negative([-3, 1, 2, -1, 3, -2, 4]), 4, + 'works for (-3, 1, 2, -1, 3, -2, 4)'; + is max-positive-negative([-1, -2, -3, 1]), 3, 'works for (-1, -2, -3, 1)'; + is max-positive-negative([1, 2]), 2, 'works for (1, 2)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(*@ints) { + say max-positive-negative(@ints); +} diff --git a/challenge-262/barroff/raku/ch-2.p6 b/challenge-262/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..c17de3b0c5 --- /dev/null +++ b/challenge-262/barroff/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub count-equal-divisible(Int:D $k, @ints --> Int:D) { + grep( + { @ints[$_[0]] == @ints[$_[1]] and ($_[0] * $_[1]) mod $k == 0 }, + combinations(0..@ints.elems - 1, 2) + ).elems +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is count-equal-divisible(2, [3, 1, 2, 2, 2, 1, 3]), 4, + 'works for 2 and (3, 1, 2, 2, 2, 1, 3)'; + is count-equal-divisible(1, [1,2,3]), 0, 'works for 1 and (1,2,3)'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Int:D $k, *@ints) { + say count-equal-divisible($k, @ints); +} |
