aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-11 14:14:12 +0100
committerGitHub <noreply@github.com>2024-05-11 14:14:12 +0100
commit54d549c61f80e8a98104290ce981779e25d6b75e (patch)
treebb9d26f2021ece1735ed7aab28b20b2e651061a1
parent24e65759109412c4cfd10183a3910c8127c15a0c (diff)
parenta35b0d39766633824631a86a5ae7dc61085f08c7 (diff)
downloadperlweeklychallenge-club-54d549c61f80e8a98104290ce981779e25d6b75e.tar.gz
perlweeklychallenge-club-54d549c61f80e8a98104290ce981779e25d6b75e.tar.bz2
perlweeklychallenge-club-54d549c61f80e8a98104290ce981779e25d6b75e.zip
Merge pull request #10073 from ntovar/branch-268
Challenge 268. Add Perl and Bash solutions. By Nelo Tovar
-rw-r--r--challenge-268/nelo-tovar/bash/21
-rwxr-xr-xchallenge-268/nelo-tovar/bash/ch-1.sh33
-rwxr-xr-xchallenge-268/nelo-tovar/bash/ch-2.sh30
-rw-r--r--challenge-268/nelo-tovar/perl/ch-1.pl37
-rw-r--r--challenge-268/nelo-tovar/perl/ch-2.pl42
5 files changed, 143 insertions, 0 deletions
diff --git a/challenge-268/nelo-tovar/bash/2 b/challenge-268/nelo-tovar/bash/2
new file mode 100644
index 0000000000..ac098b11e8
--- /dev/null
+++ b/challenge-268/nelo-tovar/bash/2
@@ -0,0 +1 @@
+i=2
diff --git a/challenge-268/nelo-tovar/bash/ch-1.sh b/challenge-268/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..54ac273bcd
--- /dev/null
+++ b/challenge-268/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,33 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 268 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+#
+# Task 1 : Magic Number
+
+function magic_number() {
+ local x=($1)
+ local y=($2)
+ IFS=$'\n' xx=($(sort -n <<<"${x[*]}"))
+ IFS=$'\n' yy=($(sort -n <<<"${y[*]}"))
+ unset IFS
+
+ b=$((${xx[0]}-${yy[0]}))
+
+ echo ${b#-}
+}
+
+examples_x=('3 7 5' '1 2 1' '2')
+examples_y=('9 5 7' '5 4 4' '5')
+
+for e in ${!examples_x[@]}; do
+ x=${examples_x[$e]}
+ y=${examples_y[$e]}
+ mn=$(magic_number "$x" "$y")
+ echo "Input : x = ($x)"
+ echo " y = ($y)"
+ echo "Output : ($mn)"
+ echo ""
+done
+
diff --git a/challenge-268/nelo-tovar/bash/ch-2.sh b/challenge-268/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..eaefa13503
--- /dev/null
+++ b/challenge-268/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 268 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+#
+# Task 2 : Number Game
+
+function number_game() {
+ local nums=("$@")
+ local len=${#nums[@]}
+ local result=()
+ IFS=$'\n' xx=($(sort -n <<<"${nums[*]}"))
+
+ for (( i = 0; i < $len; i+=2 )); do
+ result+=(${xx[$i+1]} ${xx[$i]})
+ done
+
+ echo ${result[@]}
+}
+
+examples=('2 5 3 4' '9 4 1 3 6 4 6 1' '1 2 2 3')
+
+for e in "${examples[@]}"; do
+ array=($e)
+ ng=$(number_game "${array[@]}")
+ echo "Input : nums = (${array[@]})"
+ echo -e "Output : $ng\n"
+done
+
diff --git a/challenge-268/nelo-tovar/perl/ch-1.pl b/challenge-268/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..6765bef817
--- /dev/null
+++ b/challenge-268/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 268 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+#
+# Task 1 - Magic Number
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ { x => [3, 7, 5], y => [9, 5, 7] },
+ { x => [1, 2, 1], y => [5, 4, 4] },
+ { x => [2], y => [5] },
+);
+
+sub magic_number {
+ my $x = shift;
+ my $y = shift;
+ my @xx = sort @$x;
+ my @yy = sort @$y;
+
+ return abs($xx[0] - $yy[0])
+}
+
+for my $elements (@examples) {
+ my $mn = magic_number $elements->{x}, $elements->{y};
+
+ say 'Input : @x = ', dump($elements->{x});
+ say ' @y = ', dump($elements->{y});
+ say 'Output : ', $mn;
+ say ' ';
+}
diff --git a/challenge-268/nelo-tovar/perl/ch-2.pl b/challenge-268/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..13ab73c1df
--- /dev/null
+++ b/challenge-268/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 268 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+#
+# Task 2 - Number Game
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw (min max);
+use Algorithm::Combinatorics qw(combinations);
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 2, 5, 3, 4 ],
+ [ 9, 4, 1, 3, 6, 4, 6, 1 ],
+ [1, 2, 2, 3]
+);
+
+sub number_game {
+ my $nums = shift;
+ my @ints = sort @$nums;
+ my $length = scalar @ints;
+ my @result =();
+
+ for (my $i = 0; $i < $length; $i+=2) {
+ push @result,($ints[$i+1], $ints[$i])
+ }
+
+ return \@result;
+}
+
+for my $elements (@examples) {
+ my $ng = number_game $elements;
+
+ say 'Input : @nums = ', dump(@$elements);
+ say 'Output : ', dump(@$ng);
+ say ' ';
+}