diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-04 10:47:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-04 10:47:03 +0100 |
| commit | b580883fec1db396960fe1d5046a58d83659386c (patch) | |
| tree | 5f3df4ff6906d54c71733ea1664245959a2c1b89 | |
| parent | 180da59bd6b9e3716617455c37b6f4e2c7379b6c (diff) | |
| parent | 165c442c42e9cefa806b2ec6ba2c550592e0dd16 (diff) | |
| download | perlweeklychallenge-club-b580883fec1db396960fe1d5046a58d83659386c.tar.gz perlweeklychallenge-club-b580883fec1db396960fe1d5046a58d83659386c.tar.bz2 perlweeklychallenge-club-b580883fec1db396960fe1d5046a58d83659386c.zip | |
Merge pull request #10197 from PerlBoy1967/branch-for-challenge-272
w272 - Task 1 & 2
| -rwxr-xr-x | challenge-272/perlboy1967/perl/ch1.pl | 38 | ||||
| -rwxr-xr-x | challenge-272/perlboy1967/perl/ch2.pl | 40 |
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; + |
