diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-10-03 14:31:22 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-10-03 14:31:22 +0100 |
| commit | 2ad14f79a51d96d4bde8c358982bf6fb320c2dfb (patch) | |
| tree | da1cc67a3ec3bf3839bc4a75d1df950913ab4aa1 /challenge-185 | |
| parent | 28dc2b86e008c47b91abdad519be34b4e18338f6 (diff) | |
| download | perlweeklychallenge-club-2ad14f79a51d96d4bde8c358982bf6fb320c2dfb.tar.gz perlweeklychallenge-club-2ad14f79a51d96d4bde8c358982bf6fb320c2dfb.tar.bz2 perlweeklychallenge-club-2ad14f79a51d96d4bde8c358982bf6fb320c2dfb.zip | |
- Added Perl solutions to the week 185.
Diffstat (limited to 'challenge-185')
| -rw-r--r-- | challenge-185/mohammad-anwar/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-185/mohammad-anwar/perl/ch-2.pl | 60 |
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-185/mohammad-anwar/perl/ch-1.pl b/challenge-185/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..5b4f3ce3ed --- /dev/null +++ b/challenge-185/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +=head1 + +Week 185: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-185 + +Task #1: MAC Address + + 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. + +=cut + +use v5.36; +use Test2::V0; + +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'; + +done_testing; + +# +# +# METHOD + +sub mac_address($str) { + $str =~ s/\.//g; + my @chars = split //, $str; + + my $i = 0; + my @str = (); + while ($i < @chars) { + push @str, $chars[$i].$chars[$i+1]; + $i += 2; + } + + return join(":", @str); +} diff --git a/challenge-185/mohammad-anwar/perl/ch-2.pl b/challenge-185/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..62a00d8a09 --- /dev/null +++ b/challenge-185/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +=head1 + +Week 185: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-185 + +Task #2: Mask Code + + 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. + +=cut + +use v5.36; +use Test2::V0; + +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'; + +done_testing; + +# +# +# METHOD + +sub mask_code(@list) { + my @mc = (); + foreach my $entry (@list) { + my $mask = ''; + my $i = 0; + + foreach my $char (split //, $entry) { + if ($i < 4) { + if ($char =~ /^[a-z0-9]$/) { + $mask .= 'x'; + $i++ + } + else { + $mask .= $char; + } + } + else { + $mask .= $char; + } + } + + push @mc, $mask; + } + + return \@mc; +} |
