aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-272/perlboy1967/perl/ch1.pl38
-rwxr-xr-xchallenge-272/perlboy1967/perl/ch2.pl40
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-272/perlboy1967/perl/ch1.pl b/challenge-272/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..39142f5b2f
--- /dev/null
+++ b/challenge-272/perlboy1967/perl/ch1.pl
@@ -0,0 +1,38 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 272
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-272
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Defrang IP Address
+Submitted by: Mohammad Sajid Anwar
+
+You are given a valid IPv4 address.
+
+Write a script to return the defranged version of the given IP address.
+
+|| A defranged IP address replaces every period “.” with “[.]".
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);;
+
+use Net::IP qw(ip_is_ipv4);
+
+sub defrangIpAddress ($ipv4) {
+ ip_is_ipv4($ipv4) ? $ipv4 =~ s#\.#[.]#gr : $ipv4;
+}
+
+is(defrangIpAddress('1.1.1.1'),'1[.]1[.]1[.]1','Example 1');
+is(defrangIpAddress('255.101.1.0'),'255[.]101[.]1[.]0','Example 2');
+is(defrangIpAddress('dead::beaf'),'dead::beaf','Own example');
+
+done_testing;
+
diff --git a/challenge-272/perlboy1967/perl/ch2.pl b/challenge-272/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..6f877bffa4
--- /dev/null
+++ b/challenge-272/perlboy1967/perl/ch2.pl
@@ -0,0 +1,40 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 272
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-272
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: String Score
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str.
+
+Write a script to return the score of the given string.
+
+|| The score of a string is defined as the sum of the absolute difference
+|| between the ASCII values of adjacent characters.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);;
+
+use List::Util qw(sum0);
+use List::MoreUtils qw(slide);
+
+sub stringScore ($str) {
+ sum0 slide { abs(ord($a) - ord($b)) } split //, $str;
+}
+
+is(stringScore('hello'),13,'Example 1');
+is(stringScore('perl'),30,'Example 2');
+is(stringScore('raku'),37,'Example 3');
+
+done_testing;
+