aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-06-03 17:31:24 +0200
committerpme <hauptadler@gmail.com>2024-06-03 17:31:24 +0200
commit7d9c761b8d7c2b7744ef64f8b9f2aa1bd12bda3d (patch)
treed56944d033786cb1f2e2d2b1cc30c894156c6859
parent6b1c67381886ee30e0dd9f9356340350101139b8 (diff)
downloadperlweeklychallenge-club-7d9c761b8d7c2b7744ef64f8b9f2aa1bd12bda3d.tar.gz
perlweeklychallenge-club-7d9c761b8d7c2b7744ef64f8b9f2aa1bd12bda3d.tar.bz2
perlweeklychallenge-club-7d9c761b8d7c2b7744ef64f8b9f2aa1bd12bda3d.zip
challenge-272
-rwxr-xr-xchallenge-272/peter-meszaros/perl/ch-1.pl45
-rwxr-xr-xchallenge-272/peter-meszaros/perl/ch-2.pl93
2 files changed, 138 insertions, 0 deletions
diff --git a/challenge-272/peter-meszaros/perl/ch-1.pl b/challenge-272/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..e28f4904ad
--- /dev/null
+++ b/challenge-272/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+#
+=head1 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 "[.]".
+
+=head2 Example 1
+
+ Input: $ip = "1.1.1.1"
+ Output: "1[.]1[.]1[.]1"
+
+=head2 Example 2
+
+ Input: $ip = "255.101.1.0"
+ Output: "255[.]101[.]1[.]0"
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ ['1.1.1.1', '1[.]1[.]1[.]1', 'Example 1'],
+ ['255.101.1.0', '255[.]101[.]1[.]0', 'Example 2'],
+];
+
+sub defang_ip_address
+{
+ my $ip = shift;
+ $ip =~ s/\./\[\.\]/g;
+ return $ip;
+}
+
+for (@$cases) {
+ is(defang_ip_address($_->[0]), $_->[1], $_->[2]);
+}
+
+done_testing();
+
+exit 0;
diff --git a/challenge-272/peter-meszaros/perl/ch-2.pl b/challenge-272/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..5596168b31
--- /dev/null
+++ b/challenge-272/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/env perl
+#
+=head1 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.
+
+=head2 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
+
+=head2 Example 2
+
+ Input: "perl"
+ Output: 30
+
+ ASCII values of characters:
+ p = 112
+ e = 101
+ r = 114
+ l = 108
+
+ Score => |112 - 101| + |101 - 114| + |114 - 108|
+ => 11 + 13 + 6
+ => 30
+
+=head2 Example 3
+
+ Input: "raku"
+ Output: 37
+
+ ASCII values of characters:
+ r = 114
+ a = 97
+ k = 107
+ u = 117
+
+ Score => |114 - 97| + |97 - 107| + |107 - 117|
+ => 17 + 10 + 10
+ => 37
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ ['hello', 13, 'Example 1'],
+ ['perl', 30, 'Example 2'],
+ ['raku', 37, 'Example 3'],
+ ['r', 0, 'Example 4'],
+ ['', 0, 'Example 5'],
+];
+
+sub string_score
+{
+ my $s = shift;
+
+ my @s = split(//, $s);
+
+ my $sum = 0;
+ for my $i (1..$#s) {
+ $sum += abs(ord($s[$i-1]) - ord($s[$i]));
+ }
+
+ return $sum;
+}
+
+for (@$cases) {
+ is(string_score($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
+