aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorntovar <tovar.nelo@gmail.com>2024-03-28 17:49:45 -0500
committerntovar <tovar.nelo@gmail.com>2024-03-28 17:49:45 -0500
commit78cdc97226f16667a32c7df7046bb8fc17c7c3f5 (patch)
treeb3d384b8e9a2a5aad07fc62c58fb7ab77b4efac5
parent041fe9129e3ef4d86df461a0feeee1b3740d5758 (diff)
downloadperlweeklychallenge-club-78cdc97226f16667a32c7df7046bb8fc17c7c3f5.tar.gz
perlweeklychallenge-club-78cdc97226f16667a32c7df7046bb8fc17c7c3f5.tar.bz2
perlweeklychallenge-club-78cdc97226f16667a32c7df7046bb8fc17c7c3f5.zip
Challenge 262. Add Perl and Bash solutions. By Nelo Tovar.
-rwxr-xr-xchallenge-262/nelo-tovar/bash/ch-1.sh34
-rwxr-xr-xchallenge-262/nelo-tovar/bash/ch-2.sh37
-rw-r--r--challenge-262/nelo-tovar/perl/ch-1.pl36
-rw-r--r--challenge-262/nelo-tovar/perl/ch-2.pl41
4 files changed, 148 insertions, 0 deletions
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..c88245da06
--- /dev/null
+++ b/challenge-262/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,34 @@
+#!/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=("$@")
+ locali max=0
+
+ for i in "${nums[@]}"; do
+ i=${i#-}
+ if [ $i -gt $max ]; then
+ max=$i
+ fi
+ done
+
+ echo $max
+}
+
+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..204fe9d74c
--- /dev/null
+++ b/challenge-262/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/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 List::Util qw /max min/;
+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 = max(grep {$_ >= 0} @$nums);
+ my $negative = abs(min(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 ' ';
+}