aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-21 23:11:30 +0000
committerGitHub <noreply@github.com>2024-01-21 23:11:30 +0000
commitd58f57e2ed5036d9a2db66c9d656dbbad55d1566 (patch)
treefe6dc24ac058e2d7926e2f6ca32ee4002867fa09
parent899fd7e7fca7954906336197f81f6bef1b82143e (diff)
parent9e95efd240594c350588fe50cbd69cf9363490a4 (diff)
downloadperlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.tar.gz
perlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.tar.bz2
perlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.zip
Merge pull request #9439 from BarrOff/barroff-252
feat: add solutions for challenge 252 from BarrOff
-rw-r--r--challenge-252/barroff/julia/ch-1.jl12
-rw-r--r--challenge-252/barroff/julia/ch-2.jl20
-rw-r--r--challenge-252/barroff/nim/ch_1.nim16
-rw-r--r--challenge-252/barroff/nim/ch_2.nim28
-rw-r--r--challenge-252/barroff/perl/ch-1.pl27
-rw-r--r--challenge-252/barroff/perl/ch-2.pl31
-rw-r--r--challenge-252/barroff/raku/ch-1.p621
-rw-r--r--challenge-252/barroff/raku/ch-2.p630
-rwxr-xr-xchallenge-252/barroff/v/ch-2.v15
-rwxr-xr-xchallenge-252/barroff/v/ch-2_test.v21
10 files changed, 221 insertions, 0 deletions
diff --git a/challenge-252/barroff/julia/ch-1.jl b/challenge-252/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..95e2c8d541
--- /dev/null
+++ b/challenge-252/barroff/julia/ch-1.jl
@@ -0,0 +1,12 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function special_numbers(n::Vector{T}) where {T<:Integer}
+ return sum(map(x -> n[x]^2, filter(x -> length(n) % x == 0, 1:length(n))))
+end
+
+@testset "special numbers" begin
+ @test special_numbers([1, 2, 3, 4]) == 21
+ @test special_numbers([2, 7, 1, 19, 18, 3]) == 63
+end
diff --git a/challenge-252/barroff/julia/ch-2.jl b/challenge-252/barroff/julia/ch-2.jl
new file mode 100644
index 0000000000..1a99bc9456
--- /dev/null
+++ b/challenge-252/barroff/julia/ch-2.jl
@@ -0,0 +1,20 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function unique_sum_zero(n::T) where {T<:Integer}
+ ints = []
+ map(x -> append!(ints, [x, -x]), 1:Int(floor(n / 2)))
+ if n % 2 == 1
+ append!(ints, 0)
+ end
+ sort!(ints)
+ return ints
+end
+
+@testset "unique sum zero" begin
+ @test unique_sum_zero(5) == [-2, -1, 0, 1, 2]
+ @test unique_sum_zero(3) == [-1, 0, 1]
+ @test unique_sum_zero(1) == [0]
+ @test unique_sum_zero(0) == []
+end
diff --git a/challenge-252/barroff/nim/ch_1.nim b/challenge-252/barroff/nim/ch_1.nim
new file mode 100644
index 0000000000..d90d745333
--- /dev/null
+++ b/challenge-252/barroff/nim/ch_1.nim
@@ -0,0 +1,16 @@
+import std/unittest
+
+# run tests with following command:
+# nim c -r ch_1.nim
+
+proc special_numbers(n: openArray[int]): int =
+ for i in 1 .. len(n):
+ if len(n) mod i == 0:
+ result += n[i - 1] * n[i - 1]
+
+suite "special numbers":
+ test "[1, 2, 3, 4]":
+ check(special_numbers([1, 2, 3, 4]) == 21)
+
+ test "[2, 7, 1, 19, 18, 3]":
+ check(special_numbers([2, 7, 1, 19, 18, 3]) == 63)
diff --git a/challenge-252/barroff/nim/ch_2.nim b/challenge-252/barroff/nim/ch_2.nim
new file mode 100644
index 0000000000..a512208a2e
--- /dev/null
+++ b/challenge-252/barroff/nim/ch_2.nim
@@ -0,0 +1,28 @@
+import std/unittest
+
+from std/algorithm import sort
+from std/sequtils import map
+
+# run tests with following command:
+# nim c -r ch_2.nim
+
+proc unique_sum_zero(n: Natural): seq[int] =
+ result = newSeqOfCap[int](n)
+ for i in 1..n div 2:
+ add(result, [i, -i])
+ if n mod 2 == 1:
+ add(result, 0)
+ sort(result)
+
+suite "unique sum zero":
+ test "5":
+ check(unique_sum_zero(5) == @[-2, -1, 0, 1, 2])
+
+ test "3":
+ check(unique_sum_zero(3) == @[-1, 0, 1])
+
+ test "1":
+ check(unique_sum_zero(1) == @[0])
+
+ test "0":
+ check(unique_sum_zero(0) == newSeqOfCap[int](0))
diff --git a/challenge-252/barroff/perl/ch-1.pl b/challenge-252/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..209c549dbe
--- /dev/null
+++ b/challenge-252/barroff/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+use List::Util qw/sum/;
+
+sub special_numbers (@ints) {
+ sum( map( { $ints[ $_ - 1 ]**2 } grep( { @ints % $_ == 0 } 1 .. @ints ) ) );
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say special_numbers(@ARGV);
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 2;
+ is special_numbers( ( 1, 2, 3, 4 ) ), 21, 'Works for (1, 2, 3, 4)';
+ is special_numbers( ( 2, 7, 1, 19, 18, 3 ) ), 63,
+ 'Works for (2, 7, 1, 19, 18, 3)';
+ }
+}
+
+MAIN();
diff --git a/challenge-252/barroff/perl/ch-2.pl b/challenge-252/barroff/perl/ch-2.pl
new file mode 100644
index 0000000000..ecc45123ff
--- /dev/null
+++ b/challenge-252/barroff/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub unique_sum_zero ($n) {
+ my @ints;
+ map( { push( @ints, $_, 0 - $_ ) } 1 .. int( $n / 2 ) );
+ push( @ints, 0 ) if $n % 2 == 1;
+ @ints = sort( { $a <=> $b } @ints );
+ return \@ints;
+}
+
+sub MAIN() {
+ if (@ARGV) {
+
+ #| Run on command line argument
+ say @{ unique_sum_zero( $ARGV[0] ) };
+ }
+ else {
+ #| Run test cases
+ use Test2::V0 qw( is plan );
+ plan 4;
+
+ is unique_sum_zero(5), [ -2, -1, 0, 1, 2 ], 'works for 5';
+ is unique_sum_zero(3), [ -1, 0, 1 ], 'works for 3';
+ is unique_sum_zero(1), [0], 'works for 1';
+ is unique_sum_zero(0), [], 'works for 0';
+ }
+}
+
+MAIN();
diff --git a/challenge-252/barroff/raku/ch-1.p6 b/challenge-252/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..5ab3457b47
--- /dev/null
+++ b/challenge-252/barroff/raku/ch-1.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub special-numbers(@ints --> Int:D) {
+ sum(map({ @ints[$_ - 1] ** 2 }, grep({ @ints.elems % $_ == 0 }, 1..@ints.elems)));
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is special-numbers((1, 2, 3, 4)), 21, 'works for (1, 2, 3, 4)';
+ is special-numbers((2, 7, 1, 19, 18, 3)), 63, 'works for (2, 7, 1, 19, 18, 3)';
+}
+
+#| Take user provided list like 1 2 10
+multi sub MAIN(*@s) {
+ say special-numbers(@s);
+}
diff --git a/challenge-252/barroff/raku/ch-2.p6 b/challenge-252/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..be472ec23a
--- /dev/null
+++ b/challenge-252/barroff/raku/ch-2.p6
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+# multi sub unique-sum-zero(0) {
+# return ()
+# }
+
+multi sub unique-sum-zero(Int:D $n where $n >= 0 --> Seq) {
+ my @ints;
+ map({ @ints.append($_); @ints.append(-$_) }, 1..Int($n / 2));
+ @ints.append(0) if $n % 2 == 1;
+ return sort(@ints);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 4;
+
+ is unique-sum-zero(5), (-2, -1, 0, 1, 2), 'works for 5';
+ is unique-sum-zero(3), (-1, 0, 1), 'works for 3';
+ is unique-sum-zero(1), (0), 'works for 1';
+ is unique-sum-zero(0), (), 'works for 0';
+}
+
+#| Take user provided list like 1 2 10
+multi sub MAIN(Int:D $n) {
+ say unique-sum-zero($n);
+}
diff --git a/challenge-252/barroff/v/ch-2.v b/challenge-252/barroff/v/ch-2.v
new file mode 100755
index 0000000000..18c8b3c1b9
--- /dev/null
+++ b/challenge-252/barroff/v/ch-2.v
@@ -0,0 +1,15 @@
+#!/usr/bin/env -S v run
+
+module main
+
+fn unique_sum_zero(n int) []int {
+ mut ints := []int{}
+ for i in 1 .. (n / 2) + 1 {
+ ints << [i, -i]
+ }
+ if n % 2 == 1 {
+ ints << 0
+ }
+ ints.sort()
+ return ints
+}
diff --git a/challenge-252/barroff/v/ch-2_test.v b/challenge-252/barroff/v/ch-2_test.v
new file mode 100755
index 0000000000..4b260603d3
--- /dev/null
+++ b/challenge-252/barroff/v/ch-2_test.v
@@ -0,0 +1,21 @@
+#!/usr/bin/env -S v -stats test
+
+module main
+
+fn test_odd_number() {
+ assert unique_sum_zero(5) == [-2, -1, 0, 1, 2]
+ assert unique_sum_zero(3) == [-1, 0, 1]
+}
+
+fn test_even_number() {
+ assert unique_sum_zero(4) == [-2, -1, 1, 2]
+ assert unique_sum_zero(2) == [-1, 1]
+}
+
+fn test_zero() {
+ assert unique_sum_zero(0) == []
+}
+
+fn test_one() {
+ assert unique_sum_zero(1) == [0]
+}