| 15 November 2026 | Challenge 185 |
The Mak And The Masc
Task 1: MAC Address
Submitted by: Mohammad S Anwar
You are given MAC address in the form i.e. hhhh.hhhh.hhhh.
Write a script to convert the address in the form hh:hh:hh:hh:hh:hh.
Example 1
Input: 1ac2.34f0.b1c2
Output: 1a:c2:34:f0:b1:c2
Example 2
Input: abc1.20f1.345a
Output: ab:c1:20:f1:34:5a
Solution
Perl
Match two groups of two hex digits, insert a colon and append one in case of a following dot.
use strict;
use warnings;
sub conv_mac {
shift =~ s{
((?&HEX2))((?&HEX2))(\.?)
(?(DEFINE)(?<HEX2>[0-9a-f]{2}))
}
{"$1:$2" . (":") x !!$3}gerx;
}
See the full solution to task 1.
J
Split the string at dots, reshape the intervals to \(n \times 2\), flatten the result and insert colons.
conv_mac =: ':' joinstring [: , [: <"1@((_ 2)&$);._1 ('.' , ]) : [:
See the full solution.
Task 2: Mask Code
Submitted by: Mohammad S Anwar
You are given a list of codes in many random format.
Write a script to mask first four characters (a-z,0-9) and keep the rest as it is.
Example 1
Input: @list = ('ab-cde-123', '123.abc.420', '3abc-0010.xy')
Output: ('xx-xxe-123', 'xxx.xbc.420', 'xxxx-0010.xy')
Example 2
Input: @list = ('1234567.a', 'a-1234-bc', 'a.b.c.d.e.f')
Output: ('xxxx567.a', 'x-xxx4-bc', 'x.x.x.x.e.f')
Solution
This is a reproduction of my solution dated 9 Oct 2022.
Replace the first four occurences of lower case letters or digits
with an 'x'. Though there will not be any backtracking here, it
does not hurt considering it. A slight modification of our task
would break the solution otherwise. What if we were to mask the
first four occurences of the given characters followed by a dot in
the string 'abc.d.e.f.g'?
use strict;
use warnings;
sub mask4 {
my $cnt = 0;
our $c;
shift =~ s{
[a-z0-9] # characters to be masked
(?(?{(local $c = $cnt + 1) > 4})(*FAIL)) # first four,
# backtracking safe
# (?=\.) # see comment above
(?{$cnt = $c}) # persist counter on success
}{x}xgr;
}
See the full solution to task 2.