diff options
| author | Julien Fiegehenn <simbabque@cpan.org> | 2022-10-03 19:34:55 +0100 |
|---|---|---|
| committer | Julien Fiegehenn <simbabque@cpan.org> | 2022-10-03 19:34:55 +0100 |
| commit | ff4f71ce1b1539832c8791d76f2ad5bd90a6a43c (patch) | |
| tree | 0b99e542a4eef1879e510665b8595f27638f13f1 | |
| parent | 73886c60216f15232fb3c7a196a08ca1ce3d2307 (diff) | |
| download | perlweeklychallenge-club-ff4f71ce1b1539832c8791d76f2ad5bd90a6a43c.tar.gz perlweeklychallenge-club-ff4f71ce1b1539832c8791d76f2ad5bd90a6a43c.tar.bz2 perlweeklychallenge-club-ff4f71ce1b1539832c8791d76f2ad5bd90a6a43c.zip | |
challenge 185 in perl
| -rw-r--r-- | challenge-185/julien-fiegehenn/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-185/julien-fiegehenn/perl/ch-2.pl | 41 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-185/julien-fiegehenn/perl/ch-1.pl b/challenge-185/julien-fiegehenn/perl/ch-1.pl new file mode 100644 index 0000000000..0f4fd2c724 --- /dev/null +++ b/challenge-185/julien-fiegehenn/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# 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 + +sub convert_mac { + my $mac = shift; + + return $mac =~ s/(..)(..)\.(..)(..)\.(..)(..)/$1:$2:$3:$4:$5:$6/r; +} + +use Test::More; + +is convert_mac('1ac2.34f0.b1c2'), '1a:c2:34:f0:b1:c2', 'example 1'; +is convert_mac('abc1.20f1.345a'), 'ab:c1:20:f1:34:5a', 'example 2'; + +done_testing;
\ No newline at end of file diff --git a/challenge-185/julien-fiegehenn/perl/ch-2.pl b/challenge-185/julien-fiegehenn/perl/ch-2.pl new file mode 100644 index 0000000000..7789f80c3a --- /dev/null +++ b/challenge-185/julien-fiegehenn/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use experimental 'regex_sets'; + +# 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 = () +# 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') + +# breaks if the string has an x in the first four +sub mask_first_four { + my $code = shift; + + $code =~ s/(?[ [:alnum:] - [x] ])/x/ for 1 .. 4; + return $code; +} + +sub mask_all { + my @codes = @_; + + return map mask_first_four($_), @codes; +} + +use Test::More; + +is_deeply [ mask_all('ab-cde-123', '123.abc.420', '3abc-0010.xy') ], + [ 'xx-xxe-123', 'xxx.xbc.420', 'xxxx-0010.xy' ], + 'example 1'; + +is_deeply [ mask_all('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;
\ No newline at end of file |
