aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-10 01:05:38 +0100
committerGitHub <noreply@github.com>2024-06-10 01:05:38 +0100
commit8d10b8c978be0a30822a009728d43812cbc1204e (patch)
treed46494094b0b6602929d5f8e6d67455c4667fddf
parent53bc8983a04c64dbe81e75832b443354bd2e7dd8 (diff)
parent0cc8bffa7fecfbffcfaa85c0fe7b0580369ea70f (diff)
downloadperlweeklychallenge-club-8d10b8c978be0a30822a009728d43812cbc1204e.tar.gz
perlweeklychallenge-club-8d10b8c978be0a30822a009728d43812cbc1204e.tar.bz2
perlweeklychallenge-club-8d10b8c978be0a30822a009728d43812cbc1204e.zip
Merge pull request #10232 from boblied/master
Week 272 from Bob Lied
-rw-r--r--challenge-272/bob-lied/README6
-rw-r--r--challenge-272/bob-lied/perl/ch-1.pl41
-rw-r--r--challenge-272/bob-lied/perl/ch-2.pl63
3 files changed, 107 insertions, 3 deletions
diff --git a/challenge-272/bob-lied/README b/challenge-272/bob-lied/README
index 968a88eea0..ebf6fe8695 100644
--- a/challenge-272/bob-lied/README
+++ b/challenge-272/bob-lied/README
@@ -1,4 +1,4 @@
-Solutions to weekly challenge 271 by Bob Lied
+Solutions to weekly challenge 272 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-271/
-https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-271/bob-lied
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-272/
+https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-272/bob-lied
diff --git a/challenge-272/bob-lied/perl/ch-1.pl b/challenge-272/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..fb2dcd76bb
--- /dev/null
+++ b/challenge-272/bob-lied/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge 272 Task 1 Defang IP Address
+#=============================================================================
+# You are given a valid IPv4 address.
+# Write a script to return the defanged version of the given IP address.
+# A defanged IP address replaces every period “.” with “[.]".
+# Example 1 Input: $ip = "1.1.1.1" Output: "1[.]1[.]1[.]1"
+# Example 2 Input: $ip = "255.101.1.0" Output: "255[.]101[.]1[.]0"
+#=============================================================================
+
+use v5.38;
+
+use builtin qw/true false/; no warnings "experimental::builtin";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+say defangIP($_) for @ARGV;
+
+sub defangIP($ip)
+{
+ $ip =~ s/\./[.]/gr;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( defangIP("1.1.1.1"), "1[.]1[.]1[.]1", "Example 1");
+ is( defangIP("255.101.1.0"), "255[.]101[.]1[.]0", "Example 2");
+
+ done_testing;
+}
diff --git a/challenge-272/bob-lied/perl/ch-2.pl b/challenge-272/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..ae5cef4d0e
--- /dev/null
+++ b/challenge-272/bob-lied/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# Copyright (c) 2024, Bob Lied
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge 272 Task 2 String Score
+#=============================================================================
+# 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.
+# Example 1 Input: $str = "hello"
+# Output: 13
+# ASCII values of characters: h=104 e=101 l=108 l=108 o=111
+# Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111|
+# => 3 + 7 + 0 + 3 => 13
+# Example 2 Input: "perl"
+# Output: 30
+# #xample 3 Input: "raku"
+# Output: 37
+#=============================================================================
+
+use v5.38;
+
+use builtin qw/true false/; no warnings "experimental::builtin";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+say stringScore($_) for @ARGV;
+
+sub stringScore($str)
+{
+ my @v = map { ord($_) } split(//, $str);
+
+ my $score = 0;
+
+ for ( my $first = shift @v ; defined(my $second = shift @v) ; $first = $second )
+ {
+ $score += abs($first - $second);
+ }
+
+ return $score;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( stringScore("hello"), 13, "Example 1");
+ is( stringScore("perl"), 30, "Example 2");
+ is( stringScore("raku"), 37, "Example 3");
+
+ is( stringScore(""), 0, "Empty string");
+ is( stringScore("R"), 0, "length 1 string");
+ is( stringScore("LLL"), 0, "repeated letters");
+
+ done_testing;
+}