aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-25 23:45:39 +0000
committerGitHub <noreply@github.com>2024-02-25 23:45:39 +0000
commit69f164867f30509fea0f6a55e6210e9d9ce9624b (patch)
treeae73a06b95f8dde63b3ccd6c157cb4b0d13a41a0
parente911cdefef631fef34593303b0e6c399eaa870d6 (diff)
parent8bcca9d60c78913ea3a4ed80d04a52f999363135 (diff)
downloadperlweeklychallenge-club-69f164867f30509fea0f6a55e6210e9d9ce9624b.tar.gz
perlweeklychallenge-club-69f164867f30509fea0f6a55e6210e9d9ce9624b.tar.bz2
perlweeklychallenge-club-69f164867f30509fea0f6a55e6210e9d9ce9624b.zip
Merge pull request #9646 from BarrOff/barroff-257
feat: add solutions for challenge 257 from BarrOff
-rw-r--r--challenge-257/barroff/bqn/ch-1.bqn8
-rw-r--r--challenge-257/barroff/julia/ch-1.jl14
-rw-r--r--challenge-257/barroff/nim/ch_1.nim26
-rw-r--r--challenge-257/barroff/perl/ch-1.pl34
-rw-r--r--challenge-257/barroff/raku/ch-1.p630
5 files changed, 112 insertions, 0 deletions
diff --git a/challenge-257/barroff/bqn/ch-1.bqn b/challenge-257/barroff/bqn/ch-1.bqn
new file mode 100644
index 0000000000..23f86f670d
--- /dev/null
+++ b/challenge-257/barroff/bqn/ch-1.bqn
@@ -0,0 +1,8 @@
+#/usr/bin/env bqn
+
+SmallerThanCurrent ← +˝<⌜˜
+
+•Show SmallerThanCurrent 5‿2‿1‿6
+•Show SmallerThanCurrent 1‿2‿0‿3
+•Show SmallerThanCurrent 0‿1
+•Show SmallerThanCurrent 9‿4‿9‿2
diff --git a/challenge-257/barroff/julia/ch-1.jl b/challenge-257/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..2185ea9e72
--- /dev/null
+++ b/challenge-257/barroff/julia/ch-1.jl
@@ -0,0 +1,14 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function smaller_than_current(ints::Vector{T}) where {T<:Integer}
+ map(x -> length(filter(y -> y < x, ints)), ints)
+end
+
+@testset "smaller than current" begin
+ @test smaller_than_current([5, 2, 1, 6]) == [2, 1, 0, 3]
+ @test smaller_than_current([1, 2, 0, 3]) == [1, 2, 0, 3]
+ @test smaller_than_current([0, 1]) == [0, 1]
+ @test smaller_than_current([9, 4, 9, 2]) == [2, 1, 2, 0]
+end
diff --git a/challenge-257/barroff/nim/ch_1.nim b/challenge-257/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..a2c799b91f
--- /dev/null
+++ b/challenge-257/barroff/nim/ch_1.nim
@@ -0,0 +1,26 @@
+import std/[sugar, unittest]
+
+from std/math import sqrt
+from std/sequtils import filter, map, toSeq
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+func smaller_than_current[T: SomeInteger](ints: openArray[T]): seq[int] =
+ for i in ints:
+ let
+ j: T = i
+ add(result, len(filter(ints, x => x < j)))
+
+suite "smaller than current":
+ test "[5, 2, 1, 6]":
+ check(smaller_than_current([5, 2, 1, 6]) == @[2, 1, 0, 3])
+
+ test "[1, 2, 0, 3]":
+ check(smaller_than_current([1, 2, 0, 3]) == @[1, 2, 0, 3])
+
+ test "[0, 1]":
+ check(smaller_than_current([0, 1]) == @[0, 1])
+
+ test "[9, 4, 9, 2]":
+ check(smaller_than_current([9, 4, 9, 2]) == @[2, 1, 2, 0])
diff --git a/challenge-257/barroff/perl/ch-1.pl b/challenge-257/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..2ab9798d5b
--- /dev/null
+++ b/challenge-257/barroff/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub smaller_than_n( $n, @ints ) {
+ scalar grep { $_ < $n } @ints;
+}
+
+sub smaller_than_current (@ints) {
+ my @result = map( { smaller_than_n( $_, @ints ) } @ints );
+ return \@result;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say smaller_than_current(@ARGV);
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 4;
+ is smaller_than_current( 5, 2, 1, 6 ), [ 2, 1, 0, 3 ],
+ 'works for (5, 2, 1, 6)';
+ is smaller_than_current( 1, 2, 0, 3 ), [ 1, 2, 0, 3 ],
+ 'works for (1, 2, 0, 3)';
+ is smaller_than_current( 0, 1 ), [ 0, 1 ], 'works for (0, 1)';
+ is smaller_than_current( 9, 4, 9, 2 ), [ 2, 1, 2, 0 ],
+ 'works for (9, 4, 9, 2)';
+ }
+}
+
+MAIN();
diff --git a/challenge-257/barroff/raku/ch-1.p6 b/challenge-257/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..e8dbd7a610
--- /dev/null
+++ b/challenge-257/barroff/raku/ch-1.p6
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub smaller-than-n(Int:D $n, @ints --> Int:D) {
+ grep({ $_ < $n }, @ints).elems;
+}
+
+sub smaller-than-current(@ints--> Seq) {
+ map({ smaller-than-n($_, @ints) }, @ints);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 4;
+
+ is smaller-than-current(( 5, 2, 1, 6 )), (2, 1, 0, 3),
+ 'works for (5, 2, 1, 6)';
+ is smaller-than-current(( 1, 2, 0, 3 )), (1, 2, 0, 3),
+ 'works for (1, 2, 0, 3)';
+ is smaller-than-current(( 0, 1 )), (0, 1), 'works for (0, 1)';
+ is smaller-than-current(( 9, 4, 9, 2 )), (2, 1, 2, 0),
+ 'works for (9, 4, 9, 2)';
+}
+
+#| Take user provided number like 10
+multi sub MAIN(Int:D @ints) {
+ say smaller-than-current(@ints);
+}