aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorntovar <tovar.nelo@gmail.com>2024-07-13 12:27:16 -0500
committerntovar <tovar.nelo@gmail.com>2024-07-13 12:27:16 -0500
commit5e20ce2276a75827ce6021723bce98347dee0656 (patch)
treeb1c60b29abc65f124f25c110448058c5b7999873
parentfdec0681cd0d80c15bf6bf6d8fbbaa3886bd02d5 (diff)
downloadperlweeklychallenge-club-5e20ce2276a75827ce6021723bce98347dee0656.tar.gz
perlweeklychallenge-club-5e20ce2276a75827ce6021723bce98347dee0656.tar.bz2
perlweeklychallenge-club-5e20ce2276a75827ce6021723bce98347dee0656.zip
Challenge 277. Add and Bash solutions. By Nelo Tovar
-rw-r--r--challenge-277/nelo-tovar/bash/10
-rwxr-xr-xchallenge-277/nelo-tovar/bash/ch-1.sh56
-rwxr-xr-xchallenge-277/nelo-tovar/bash/ch-2.sh75
-rw-r--r--challenge-277/nelo-tovar/perl/ch-1.pl42
-rw-r--r--challenge-277/nelo-tovar/perl/ch-2.pl43
5 files changed, 216 insertions, 0 deletions
diff --git a/challenge-277/nelo-tovar/bash/1 b/challenge-277/nelo-tovar/bash/1
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-277/nelo-tovar/bash/1
diff --git a/challenge-277/nelo-tovar/bash/ch-1.sh b/challenge-277/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..c247ab5d82
--- /dev/null
+++ b/challenge-277/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 277 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+#
+# Task 1 - Count Common
+
+function singleton() {
+ local array=("$@")
+ declare -A local temp
+ local single=()
+
+ for n in ${array[@]}; do
+ ((temp[$n]++))
+ done
+
+ for i in ${!array[@]}; do
+ key=${array[$i]}
+ if [ ${temp[$key]} -eq 1 ]; then
+ single+=($key)
+ unset temp[$key]
+ fi
+ done
+
+ echo ${single[@]}
+}
+function count_common() {
+ local temp=($1)
+ local words1=($(singleton ${temp[@]}))
+ temp=($2)
+ local words2=($(singleton ${temp[@]}))
+ local count=0
+
+ for word in ${words1[@]}; do
+ if [[ "${words2[@]}" == *"$word"* ]]; then
+ ((count++))
+ fi
+ done
+
+ echo $count
+}
+
+examples_w1=('Perl is my friend' 'Perl and Python are very similar' 'Perl is imperative lisp is functional')
+examples_w2=('Perl and Raku are friends' 'Python is top in guest languages' 'Crystal is similar to Ruby')
+
+for e in ${!examples_w1[@]}; do
+ words1=${examples_w1[$e]}
+ words2=${examples_w2[$e]}
+ cc=($(count_common "$words1" "$words2"))
+ echo "Input : words1 = ($words1)"
+ echo " : words2 = ($words2)"
+ echo "Output : $cc"
+ echo ""
+done
+
diff --git a/challenge-277/nelo-tovar/bash/ch-2.sh b/challenge-277/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..ff19f15913
--- /dev/null
+++ b/challenge-277/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,75 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 277 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+#
+# Task 2 - Strong Pair
+
+function min() {
+ local array=("$@")
+ local min=${array[0]}
+
+ for n in ${array[@]}; do
+ if [[ n -lt $min ]]; then
+ min=$n
+ fi
+ done
+
+ echo $min
+}
+
+function uniq() {
+ local array=("$@")
+ declare -A local temp
+ local unique=()
+
+ for n in ${array[@]}; do
+ ((temp[$n]++))
+ done
+
+ for i in ${!array[@]}; do
+ key=${array[$i]}
+ if [ -n "${temp[$key]}" ]; then
+ unique+=($key)
+ unset temp[$key]
+ fi
+ done
+
+ echo ${unique[@]}
+}
+
+function strong_pair() {
+ local elements=("$@")
+ local ints=($(uniq ${elements[@]}))
+ local length=${#ints[@]}
+ local count=0
+
+
+ for (( i = 0; i < $length - 1; i++ )); do
+ for (( j = $i + 1; j < $length; j++ )); do
+ ((diff=${ints[$i]} - ${ints[$j]}))
+ diff=${diff#-}
+
+ t[0]=${ints[$i]}
+ t[1]=${ints[$j]}
+ min=$(min "${t[@]}")
+
+ if [ $diff -gt 0 ] && [ $diff -lt $min ]; then
+ ((count++))
+ fi
+ done
+ done
+
+ echo $count
+}
+
+examples=('1 2 3 4 5' '5 7 1 7')
+
+for e in "${examples[@]}"; do
+ array=($e)
+ sp=$(strong_pair "${array[@]}")
+ echo "Input : ints = (${array[@]})"
+ echo -e "Output : $sp\n"
+done
+
diff --git a/challenge-277/nelo-tovar/perl/ch-1.pl b/challenge-277/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..ae5783c50b
--- /dev/null
+++ b/challenge-277/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 277 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+#
+# Task 1 - Count Common
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::MoreUtils qw /singleton one/;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ ["Perl", "is", "my", "friend"], ["Perl", "and", "Raku", "are", "friend"] ],
+ [ ["Perl", "and", "Python", "are", "very", "similar"], ["Python", "is", "top", "in", "guest", "languages"] ],
+ [ ["Perl", "is", "imperative", "Lisp", "is", "functional"], ["Crystal", "is", "similar", "to", "Ruby"] ],
+);
+
+sub count_common {
+ my $elements=shift;
+ my @words1 = singleton $elements->[0]->@*;
+ my @words2 = singleton $elements->[1]->@*;
+ my $count = 0;
+
+ foreach my $word (@words1) {
+ $count++ if one { $word eq $_ } @words2;
+ }
+
+ return $count;
+}
+
+for my $elements (@examples) {
+ my $cc = count_common $elements;
+
+ say 'Input : @words1 = ', dump(@$elements[0]);
+ say ' @words2 = ', dump(@$elements[1]);
+ say 'Output : ', $cc;
+ say ' ';
+}
diff --git a/challenge-277/nelo-tovar/perl/ch-2.pl b/challenge-277/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..720e3512f3
--- /dev/null
+++ b/challenge-277/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 277 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+#
+# Task 2 - Strong Pair
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw /min uniq/;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 1, 2, 3, 4, 5 ],
+ [ 5, 7, 1, 7],
+);
+
+sub strong_pair {
+ my $elements = shift;
+ my @ints = uniq @$elements;
+ my $length = scalar @ints;
+ my $count = 0;
+
+ for (my $i = 0; $i < $length - 1; $i++) {
+ for (my $j = $i + 1; $j < $length; $j++) {
+ my $diff = abs($ints[$i] - $ints[$j]);
+ $count++ if ( 0 < $diff < min $ints[$i], $ints[$j] );
+ }
+
+ }
+ return $count;
+}
+
+for my $elements (@examples) {
+ my $sp = strong_pair $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $sp;
+ say ' ';
+}