aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2022-10-03 22:03:38 +0200
committerE. Choroba <choroba@matfyz.cz>2022-10-03 22:03:38 +0200
commit6a23bbdf9176d5ac060caaffe2d56db9dc4049fc (patch)
tree19a0f93ff9f01ff21b2e17205dc3753c30b504c5
parent0a2f900e76be04874b9ca7d427c7862b24596d59 (diff)
downloadperlweeklychallenge-club-6a23bbdf9176d5ac060caaffe2d56db9dc4049fc.tar.gz
perlweeklychallenge-club-6a23bbdf9176d5ac060caaffe2d56db9dc4049fc.tar.bz2
perlweeklychallenge-club-6a23bbdf9176d5ac060caaffe2d56db9dc4049fc.zip
Add solutions to 185: MAC Address & Mask Code by E. Choroba
-rwxr-xr-xchallenge-185/e-choroba/perl/ch-1.pl25
-rwxr-xr-xchallenge-185/e-choroba/perl/ch-2.pl27
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-185/e-choroba/perl/ch-1.pl b/challenge-185/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..d1f4d068e5
--- /dev/null
+++ b/challenge-185/e-choroba/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+my $XD4 = qr/[[:xdigit:]]{4}/;
+sub mac_address ($addr) {
+ die 'Invalid input' if $addr !~ /^$XD4\.$XD4\.$XD4$/;
+
+ $addr =~ s/\.//g;
+ substr $addr, 3 * $_ - 1, 0, ':' for 1 .. 5;
+ return $addr
+}
+
+use Test2::V0;
+plan 6;
+
+is mac_address('1ac2.34f0.b1c2'), '1a:c2:34:f0:b1:c2', 'Example 1';
+is mac_address('abc1.20f1.345a'), 'ab:c1:20:f1:34:5a', 'Example 2';
+
+is mac_address('ABC1.20F1.345A'), 'AB:C1:20:F1:34:5A', 'Case';
+
+like dies { mac_address('x123.1234.abcd') }, qr/Invalid input/, 'Invalid char';
+like dies { mac_address('123.1234.abcd') }, qr/Invalid input/, 'Too short';
+like dies { mac_address('1234.12345.abcd') }, qr/Invalid input/, 'Too long';
diff --git a/challenge-185/e-choroba/perl/ch-2.pl b/challenge-185/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..af76280b4e
--- /dev/null
+++ b/challenge-185/e-choroba/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental qw{ signatures };
+
+sub mask_code (@codes) {
+ for my $code (@codes) {
+ my $count = 0;
+ $code =~ s/[a-z0-9]/++$count <= 4 ? 'x' : $&/peg;
+ }
+ return \@codes
+}
+
+use Test2::V0;
+plan 3;
+
+is mask_code('ab-cde-123', '123.abc.420', '3abc-0010.xy'),
+ ['xx-xxe-123', 'xxx.xbc.420', 'xxxx-0010.xy'],
+ 'Example 1';
+
+is mask_code('1234567.a', 'a-1234-bc', 'a.b.c.d.e.f'),
+ ['xxxx567.a', 'x-xxx4-bc', 'x.x.x.x.e.f'],
+ 'Example 2';
+
+is mask_code('abc'),
+ ['xxx'],
+ 'Shorter';