aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-10 01:08:47 +0100
committerGitHub <noreply@github.com>2024-06-10 01:08:47 +0100
commit4aaf4ca4bb3399b3bbf7306c476ae431886b1359 (patch)
tree1511b9e174dbeb5b39f568fa3f825b189cc2a954
parente5ec1a13ef2efb876fbc31e540fad80725b03faf (diff)
parent5d660805aafdb65cc68e9f60cb9998740342f17b (diff)
downloadperlweeklychallenge-club-4aaf4ca4bb3399b3bbf7306c476ae431886b1359.tar.gz
perlweeklychallenge-club-4aaf4ca4bb3399b3bbf7306c476ae431886b1359.tar.bz2
perlweeklychallenge-club-4aaf4ca4bb3399b3bbf7306c476ae431886b1359.zip
Merge pull request #10235 from ntovar/branch-272
Challenge 272. Add Perl and Bash solutions. By Nelo Tovar
-rwxr-xr-xchallenge-272/nelo-tovar/bash/ch-1.sh23
-rwxr-xr-xchallenge-272/nelo-tovar/bash/ch-2.sh35
-rw-r--r--challenge-272/nelo-tovar/perl/ch-1.pl29
-rw-r--r--challenge-272/nelo-tovar/perl/ch-2.pl34
4 files changed, 121 insertions, 0 deletions
diff --git a/challenge-272/nelo-tovar/bash/ch-1.sh b/challenge-272/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..2b9a22762d
--- /dev/null
+++ b/challenge-272/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 272 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/
+#
+# Task 1 : Defang IP Address
+
+function defang_ip_address() {
+ local ip=$1
+
+ echo ${ip//./[.]}
+}
+
+examples=('1.1.1.1' '255.101.1.0')
+
+for e in ${examples[@]}; do
+ dia=($(defang_ip_address "$e"))
+ echo "Input : ip = $e"
+ echo "Output : $dia"
+ echo ""
+done
+
diff --git a/challenge-272/nelo-tovar/bash/ch-2.sh b/challenge-272/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..e9161ab2ed
--- /dev/null
+++ b/challenge-272/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 272 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/
+#
+# Task 2 : String Score
+
+function ord() {
+ LC_CTYPE=C printf '%d' "'$1"
+}
+
+function string_score() {
+ local str=$1
+ local len=$((${#str}-1))
+ local sum=0
+
+ for (( i = 0; i < $len; i++ )); do
+ c1=$(ord "${str:i:1}")
+ c2=$(ord "${str:i+1:1}")
+ diff=$((c1-c2))
+ ((sum+=${diff#-}))
+ done
+
+ echo $sum
+}
+
+examples=('hello' 'perl' 'raku')
+
+for e in ${examples[@]}; do
+ ss=$(string_score "$e")
+ echo "Input : str = $e"
+ echo -e "Output : $ss\n"
+done
+
diff --git a/challenge-272/nelo-tovar/perl/ch-1.pl b/challenge-272/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..360ac7ad1a
--- /dev/null
+++ b/challenge-272/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 272 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/
+#
+# Task 1 - Defang IP Address
+#
+
+use strict;
+use warnings;
+use v5.28;
+
+my @examples = ('1.1.1.1', '255.101.1.0');
+
+sub defang_ip_address {
+ my $ip = shift;
+ $ip =~ s/\./\[\.\]/g;
+
+ return $ip;
+}
+
+for my $elements (@examples) {
+ my $dia = defang_ip_address $elements;
+
+ say 'Input : $ip = ', $elements;
+ say 'Output : ', $dia;
+ say ' ';
+}
diff --git a/challenge-272/nelo-tovar/perl/ch-2.pl b/challenge-272/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..ff073a1044
--- /dev/null
+++ b/challenge-272/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 272 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/
+#
+# Task 2 - String Score
+#
+
+use strict;
+use warnings;
+use v5.28;
+
+my @examples = ('hello', 'perl', 'raku');
+
+sub string_score {
+ my $str = shift;
+ my $length = length($str) - 1;
+ my $sum = 0;
+
+ for (my $i = 0; $i < $length; $i++) {
+ $sum += abs(ord(substr($str, $i, 1)) - ord(substr($str,$i + 1, 1)));
+ }
+
+ return $sum;
+}
+
+for my $elements (@examples) {
+ my $ss = string_score $elements;
+
+ say 'Input : $str = ', $elements;
+ say 'Output : ', $ss;
+ say ' ';
+}