diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-04 10:49:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-04 10:49:27 +0100 |
| commit | 14c73ef81e24547fee6ded800c16fb52f749b05d (patch) | |
| tree | 25adc77d470e2b504660896d52543ced3d9d161c | |
| parent | b580883fec1db396960fe1d5046a58d83659386c (diff) | |
| parent | 61532a1b4c1ca4fa99d30262977f6167de2ebb90 (diff) | |
| download | perlweeklychallenge-club-14c73ef81e24547fee6ded800c16fb52f749b05d.tar.gz perlweeklychallenge-club-14c73ef81e24547fee6ded800c16fb52f749b05d.tar.bz2 perlweeklychallenge-club-14c73ef81e24547fee6ded800c16fb52f749b05d.zip | |
Merge pull request #10198 from choroba/ech272
Add solutions to 272: Defang IP Address & String Score by E. Choroba
| -rwxr-xr-x | challenge-272/e-choroba/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-272/e-choroba/perl/ch-2.pl | 22 |
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'; |
