diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-06-03 07:27:40 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-06-03 07:27:40 +0000 |
| commit | 165c442c42e9cefa806b2ec6ba2c550592e0dd16 (patch) | |
| tree | 52b16c052d310510d33c0ce06204ff381d587640 | |
| parent | 6b1c67381886ee30e0dd9f9356340350101139b8 (diff) | |
| download | perlweeklychallenge-club-165c442c42e9cefa806b2ec6ba2c550592e0dd16.tar.gz perlweeklychallenge-club-165c442c42e9cefa806b2ec6ba2c550592e0dd16.tar.bz2 perlweeklychallenge-club-165c442c42e9cefa806b2ec6ba2c550592e0dd16.zip | |
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; + |
