aboutsummaryrefslogtreecommitdiff
path: root/challenge-262
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-03-29 22:52:14 +0100
committerpme <hauptadler@gmail.com>2024-03-29 22:52:14 +0100
commitdb2b43222a7ec7049d2608bac28e23fbfead166e (patch)
tree1af2d0e6adb9e683adefe877d3d135eb40436abf /challenge-262
parente868157a790774d3a8fe2dbc6046216094c3e33d (diff)
parent286052e238cd08089167db24b9bc2cb5c2f434d2 (diff)
downloadperlweeklychallenge-club-db2b43222a7ec7049d2608bac28e23fbfead166e.tar.gz
perlweeklychallenge-club-db2b43222a7ec7049d2608bac28e23fbfead166e.tar.bz2
perlweeklychallenge-club-db2b43222a7ec7049d2608bac28e23fbfead166e.zip
Merge branch 'challenge-262' of https://github.com/pme/perlweeklychallenge-club into challenge-262
Diffstat (limited to 'challenge-262')
-rw-r--r--challenge-262/lance-wicks/perl/ch-1.sh1
-rw-r--r--challenge-262/lance-wicks/perl/lib/MPN.pm41
-rw-r--r--challenge-262/lance-wicks/perl/t/mpn.t38
-rw-r--r--challenge-262/lance-wicks/roc/main.roc40
-rw-r--r--challenge-262/lubos-kolouch/perl/ch-1.pl20
-rw-r--r--challenge-262/lubos-kolouch/perl/ch-2.pl28
-rw-r--r--challenge-262/lubos-kolouch/python/ch-1.py13
-rw-r--r--challenge-262/lubos-kolouch/python/ch-2.py16
-rwxr-xr-xchallenge-262/nelo-tovar/bash/ch-1.sh40
-rwxr-xr-xchallenge-262/nelo-tovar/bash/ch-2.sh37
-rw-r--r--challenge-262/nelo-tovar/perl/ch-1.pl35
-rw-r--r--challenge-262/nelo-tovar/perl/ch-2.pl41
12 files changed, 350 insertions, 0 deletions
diff --git a/challenge-262/lance-wicks/perl/ch-1.sh b/challenge-262/lance-wicks/perl/ch-1.sh
new file mode 100644
index 0000000000..7f15aef3cf
--- /dev/null
+++ b/challenge-262/lance-wicks/perl/ch-1.sh
@@ -0,0 +1 @@
+perl -Ilib/ t/mpn.t
diff --git a/challenge-262/lance-wicks/perl/lib/MPN.pm b/challenge-262/lance-wicks/perl/lib/MPN.pm
new file mode 100644
index 0000000000..9a2190486b
--- /dev/null
+++ b/challenge-262/lance-wicks/perl/lib/MPN.pm
@@ -0,0 +1,41 @@
+package MPN;
+
+
+sub max_pos_or_neg {
+ my $self = shift;
+ my @ints = @_;
+
+ my $positives = $self->positives(@ints);
+ my $negatives = $self->negatives(@ints);
+
+ if ($positives > $negatives) { return $positives }
+ if ($positives < $negatives) { return $negatives }
+ return 0;
+
+}
+
+sub positives {
+ my $self = shift;
+ my @ints = @_;
+
+ my $count = 0;
+ for my $i (@ints) {
+ $count++ if $i > 0;
+ }
+
+ return $count;
+}
+
+sub negatives {
+ my $self = shift;
+ my @ints = @_;
+
+ my $count = 0;
+ for my $i (@ints) {
+ $count++ if $i < 0;
+ }
+
+ return $count;
+}
+
+1; \ No newline at end of file
diff --git a/challenge-262/lance-wicks/perl/t/mpn.t b/challenge-262/lance-wicks/perl/t/mpn.t
new file mode 100644
index 0000000000..4cda02cc02
--- /dev/null
+++ b/challenge-262/lance-wicks/perl/t/mpn.t
@@ -0,0 +1,38 @@
+use Test2::V0 -target => 'MPN';
+
+subtest "Example 1" => sub {
+ my @ints = (-3, 1, 2, -1, 3, -2, 4);
+ is $CLASS->positives(@ints), 4;
+ is $CLASS->negatives(@ints), 3;
+ is $CLASS->max_pos_or_neg(@ints), 4;
+};
+
+subtest "Example 2" => sub {
+ my @ints = (-1, -2, -3, 1);
+ is $CLASS->positives(@ints), 1;
+ is $CLASS->negatives(@ints), 3;
+ is $CLASS->max_pos_or_neg(@ints), 3;
+};
+
+subtest "Example 3" => sub {
+ my @ints = (1, 2);
+ is $CLASS->positives(@ints), 2;
+ is $CLASS->negatives(@ints), 0;
+ is $CLASS->max_pos_or_neg(@ints), 2;
+};
+
+subtest "Example 3" => sub {
+ my @ints = (1, 2);
+ is $CLASS->positives(@ints), 2;
+ is $CLASS->negatives(@ints), 0;
+ is $CLASS->max_pos_or_neg(@ints), 2;
+};
+
+subtest "Zero is neither positive or negative" => sub {
+ my @ints = (-0,+0,0);
+ is $CLASS->positives(@ints), 0;
+ is $CLASS->negatives(@ints), 0;
+ is $CLASS->max_pos_or_neg(@ints), 0;
+};
+
+done_testing; \ No newline at end of file
diff --git a/challenge-262/lance-wicks/roc/main.roc b/challenge-262/lance-wicks/roc/main.roc
new file mode 100644
index 0000000000..fe51e6e95e
--- /dev/null
+++ b/challenge-262/lance-wicks/roc/main.roc
@@ -0,0 +1,40 @@
+app "mpn"
+ packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br" }
+ imports [pf.Stdout]
+ provides [main] to pf
+
+main =
+ dbg positives [-3, 1, 2, -1, 3, -2, 4]
+ dbg negatives [-3, 1, 2, -1, 3, -2, 4]
+ dbg maxNegOrPos [-3, 1, 2, -1, 3, -2, 4]
+ dbg maxNegOrPos [-1, -2, -3, 1]
+ Stdout.line "run this with 'roc dev'"
+
+
+maxNegOrPos = \ints ->
+ if positives ints > negatives ints then
+ positives ints
+ else if negatives ints > positives ints then
+ negatives ints
+ else
+ 0
+
+positives = \ints ->
+ List.countIf ints Num.isPositive
+
+negatives = \ints ->
+ List.countIf ints Num.isNegative
+
+
+# Tests
+expect positives [-3, 1, 2, -1, 3, -2, 4] == 4
+expect negatives [-3, 1, 2, -1, 3, -2, 4] == 3
+expect maxNegOrPos [-3, 1, 2, -1, 3, -2, 4] == 4
+
+expect positives [-1, -2, -3, 1] == 1
+expect negatives [-1, -2, -3, 1] == 3
+expect maxNegOrPos [-1, -2, -3, 1] == 3
+
+expect positives [1,2] == 2
+expect negatives [1,2] == 0
+expect maxNegOrPos [1,2] == 2 \ No newline at end of file
diff --git a/challenge-262/lubos-kolouch/perl/ch-1.pl b/challenge-262/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..ab2df48556
--- /dev/null
+++ b/challenge-262/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+
+
+package Ch1;
+use Carp;
+
+sub max_positive_negative {
+ my @ints = @_;
+ my $positive_count = scalar(grep { $_ > 0 } @ints);
+ my $negative_count = scalar(grep { $_ < 0 } @ints);
+ return $positive_count > $negative_count ? $positive_count : $negative_count;
+}
+
+# Assert tests
+croak "Test failed!" unless max_positive_negative(-3, 1, 2, -1, 3, -2, 4) == 4;
+croak "Test failed!" unless max_positive_negative(-1, -2, -3, 1) == 3;
+croak "Test failed!" unless max_positive_negative(1, 2) == 2;
+
+1; \ No newline at end of file
diff --git a/challenge-262/lubos-kolouch/perl/ch-2.pl b/challenge-262/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..c2eb3354ee
--- /dev/null
+++ b/challenge-262/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+
+package Ch2;
+use Carp;
+
+sub count_equal_divisible {
+ my ($ints_ref, $k) = @_;
+ my @ints = @$ints_ref;
+ my $count = 0;
+ my $n = scalar @ints;
+
+ for my $i (0 .. $n - 1) {
+ for my $j ($i + 1 .. $n - 1) {
+ if ($ints[$i] == $ints[$j] && ($i * $j) % $k == 0) {
+ $count++;
+ }
+ }
+ }
+ return $count;
+}
+
+# Assert tests
+croak "Test failed!" unless count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4;
+croak "Test failed!" unless count_equal_divisible([1, 2, 3], 1) == 0;
+
+1; \ No newline at end of file
diff --git a/challenge-262/lubos-kolouch/python/ch-1.py b/challenge-262/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..f87605552f
--- /dev/null
+++ b/challenge-262/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,13 @@
+from typing import List
+
+
+def max_positive_negative(ints: List[int]) -> int:
+ positive_count = sum(1 for x in ints if x > 0)
+ negative_count = sum(1 for x in ints if x < 0)
+ return max(positive_count, negative_count)
+
+
+# Assert tests
+assert max_positive_negative([-3, 1, 2, -1, 3, -2, 4]) == 4
+assert max_positive_negative([-1, -2, -3, 1]) == 3
+assert max_positive_negative([1, 2]) == 2
diff --git a/challenge-262/lubos-kolouch/python/ch-2.py b/challenge-262/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..5d42adf2a0
--- /dev/null
+++ b/challenge-262/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,16 @@
+from typing import List
+
+
+def count_equal_divisible(ints: List[int], k: int) -> int:
+ count = 0
+ n = len(ints)
+ for i in range(n):
+ for j in range(i + 1, n):
+ if ints[i] == ints[j] and i * j % k == 0:
+ count += 1
+ return count
+
+
+# Assert tests
+assert count_equal_divisible([3, 1, 2, 2, 2, 1, 3], 2) == 4
+assert count_equal_divisible([1, 2, 3], 1) == 0
diff --git a/challenge-262/nelo-tovar/bash/ch-1.sh b/challenge-262/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..f0151dc053
--- /dev/null
+++ b/challenge-262/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 262 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/
+#
+# Task 1 : Max Positive Negative
+
+function max_positive_negative() {
+ local nums=("$@")
+ local positive=0
+ local negative=0
+
+ for i in "${nums[@]}"; do
+ if [ $i -ge 0 ]; then
+ ((positive++))
+ else
+ ((negative++))
+ fi
+ done
+
+ if [ $positive -gt $negative ]; then
+ echo $positive
+ else
+ echo $negative
+ fi
+}
+
+example1='-3 1 2 -1 3 -2 4'
+example2='-1 -2 -3 1'
+example3='1 2'
+
+for e in "$example1" "$example2" "$example3"; do
+ array=($e)
+ mpn=($(max_positive_negative "${array[@]}"))
+ echo "Input : ints = (${array[@]})"
+ echo "Output : $mpn"
+ echo ""
+done
+
diff --git a/challenge-262/nelo-tovar/bash/ch-2.sh b/challenge-262/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..1a2ebf4d9b
--- /dev/null
+++ b/challenge-262/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 262 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/
+#
+# Task 2 : Count Equal Divisible
+
+function count_equal_divisible() {
+ local k=$1
+ shift
+ local nums=("$@")
+ local len=${#nums[@]}
+ local count=0
+
+ for (( i = 0; i <= $len-1 ; i++ )); do
+ for (( j = $i+1; j < $len; j++ )); do
+ if [[ (${nums[$i]} -eq ${nums[$j]}) && $(($i * $j % $k)) -eq 0 ]]; then
+ ((count++))
+ fi
+ done
+ done
+
+ echo $count
+}
+
+example_ints=('3 1 2 2 2 1 3' '1 2 3')
+example_k=(2 1)
+
+for e in 0 1; do
+ array=(${example_ints[$e]})
+ k=${example_k[$e]}
+ ced=$(count_equal_divisible $k "${array[@]}")
+ echo "Input : ints = (${array[@]}), and K = $k"
+ echo -e "Output : $ced\n"
+done
+
diff --git a/challenge-262/nelo-tovar/perl/ch-1.pl b/challenge-262/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..ce3e9bf99c
--- /dev/null
+++ b/challenge-262/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 262 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/
+#
+# Task 1 - Max Positive Negative
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ -3, 1, 2, -1, 3, -2, 4 ],
+ [ -1, -2, -3, 1 ],
+ [ 1, 2 ],
+);
+
+sub max_positive_negative {
+ my $nums = shift;
+ my $positive = scalar grep {$_ >= 0} @$nums;
+ my $negative = scalar grep {$_ < 0 } @$nums;
+
+ return $positive > $negative ? $positive : $negative;
+}
+
+for my $elements (@examples) {
+ my $mpn = max_positive_negative $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $mpn;
+ say ' ';
+}
diff --git a/challenge-262/nelo-tovar/perl/ch-2.pl b/challenge-262/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..9c23b2ca6d
--- /dev/null
+++ b/challenge-262/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 262 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/
+#
+# Task 2 - Count Equal Divisible
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ { k => 2, ints => [3,1,2,2,2,1,3] },
+ { k => 1, ints => [1,2,3] },
+);
+
+sub count_equal_divisible {
+ my $input = shift;
+ my $ints = $input->{ints};
+ my $length = scalar @$ints;
+ my $count = 0;
+
+ for (my $i = 0; $i <= $length - 1; $i++) {
+ for (my $j = $i + 1; $j < $length; $j++) {
+ $count++ if (($ints->[$i] eq $ints->[$j]) and (($i * $j % $input->{k}) eq 0) );
+ }
+ }
+
+ return $count;
+}
+
+for my $elements (@examples) {
+ my $ced = count_equal_divisible $elements;
+
+ say 'Input : @ints = ', dump($elements->{ints}), ' and K = ', $elements->{k};
+ say 'Output : ', $ced;
+ say ' ';
+}