aboutsummaryrefslogtreecommitdiff
path: root/challenge-272
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2024-06-03 09:36:31 +0200
committerE. Choroba <choroba@matfyz.cz>2024-06-03 09:36:31 +0200
commit61532a1b4c1ca4fa99d30262977f6167de2ebb90 (patch)
tree272587f1cebd07fc4813cf95c27c3598c7a49254 /challenge-272
parent6b1c67381886ee30e0dd9f9356340350101139b8 (diff)
downloadperlweeklychallenge-club-61532a1b4c1ca4fa99d30262977f6167de2ebb90.tar.gz
perlweeklychallenge-club-61532a1b4c1ca4fa99d30262977f6167de2ebb90.tar.bz2
perlweeklychallenge-club-61532a1b4c1ca4fa99d30262977f6167de2ebb90.zip
Add solutions to 272: Defang IP Address & String Score by E. Choroba
Diffstat (limited to 'challenge-272')
-rwxr-xr-xchallenge-272/e-choroba/perl/ch-1.pl13
-rwxr-xr-xchallenge-272/e-choroba/perl/ch-2.pl22
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-272/e-choroba/perl/ch-1.pl b/challenge-272/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..e38b5da952
--- /dev/null
+++ b/challenge-272/e-choroba/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub defang_ip_address($address) {
+ $address =~ s/[.]/[.]/gr
+}
+
+use Test::More tests => 2;
+
+is defang_ip_address('1.1.1.1'), '1[.]1[.]1[.]1', 'Example 1';
+is defang_ip_address('255.101.1.0'), '255[.]101[.]1[.]0', 'Example 2';
diff --git a/challenge-272/e-choroba/perl/ch-2.pl b/challenge-272/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..f0f4116061
--- /dev/null
+++ b/challenge-272/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub string_score($str) {
+ my $score = 0;
+
+ while ($str =~ /(.)(?=(.))/g) {
+ $score += abs(ord($1) - ord($2));
+ }
+ return $score
+}
+
+use Test::More tests => 3 + 2;
+
+is string_score('hello'), 13, 'Example 1';
+is string_score('perl'), 30, 'Example 2';
+is string_score('raku'), 37, 'Example 3';
+
+is string_score(""), 0, 'Empty';
+is string_score('a'), 0, 'Single character';