aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-03 20:22:33 +0100
committerGitHub <noreply@github.com>2022-10-03 20:22:33 +0100
commita26b9d9c63ad7871e7989d96ea7f9795018db871 (patch)
treed270864ff7d70c7c27a81b32bab674007db93be1
parent904d0fc37eefa8657eeadb442df1b3bbe6dec77f (diff)
parent3525f8845321f8a62e58e0b144d419fba260e631 (diff)
downloadperlweeklychallenge-club-a26b9d9c63ad7871e7989d96ea7f9795018db871.tar.gz
perlweeklychallenge-club-a26b9d9c63ad7871e7989d96ea7f9795018db871.tar.bz2
perlweeklychallenge-club-a26b9d9c63ad7871e7989d96ea7f9795018db871.zip
Merge pull request #6842 from mattneleigh/pwc185
new file: challenge-185/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-185/mattneleigh/perl/ch-1.pl56
-rwxr-xr-xchallenge-185/mattneleigh/perl/ch-2.pl103
2 files changed, 159 insertions, 0 deletions
diff --git a/challenge-185/mattneleigh/perl/ch-1.pl b/challenge-185/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..3a5a2e6cf4
--- /dev/null
+++ b/challenge-185/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @addresses = (
+ "1ac2.34f0.b1c2",
+ "abc1.20f1.345a"
+);
+
+print("\n");
+foreach my $address (@addresses){
+ printf(
+ "Input: %s\nOutput: %s\n\n",
+ $address,
+ convert_mac_address($address)
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Convert a MAC address from the format XXXX.XXXX.XXXX to XX:XX:XX:XX:XX:XX
+# Takes one argument:
+# * A MAC address in the format XXXX.XXXX.XXXX
+# Returns:
+# * The supplied address in the form XX:XX:XX:XX:XX:XX
+################################################################################
+sub convert_mac_address{
+
+ return(
+ join(
+ ":",
+ map(
+ # This will create two list members of
+ # two characters each
+ m/(..)(..)/,
+ split(/\./, shift())
+ )
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-185/mattneleigh/perl/ch-2.pl b/challenge-185/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..0e570af3b0
--- /dev/null
+++ b/challenge-185/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @codes = (
+ # Given cases
+ [
+ [ "ab-cde-123", "123.abc.420", "3abc-0010.xy" ],
+ 0
+ ],
+ [
+ [ "1234567.a", "a-1234-bc", "a.b.c.d.e.f" ],
+ 0
+ ],
+
+ # Additional test cases
+ [
+ [ "CPE-1704-TKS", "NX-2000", "867-5309" ],
+ 0
+ ],
+ [
+ [ "CPE-1704-TKS", "NX-2000", "867-5309" ],
+ 1
+ ]
+);
+
+print("\n");
+foreach my $code_list (@codes){
+ printf(
+ "Input: \@list = (%s)\nOutput: (%s)\n\n",
+ join(
+ ", ",
+ map(
+ "'$_'",
+ @{$code_list->[0]}
+ )
+ ),
+ join(
+ ", ",
+ map(
+ "'".mask_alphanumerics($_, 4, $code_list->[1])."'",
+ @{$code_list->[0]}
+ )
+ )
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Mask the first N alphanumeric characters (i.e. a-z, A-Z, 0-9) from either end
+# of a string, replacing them with 'x'
+# Takes three arguments:
+# * The string to examine (e.g. "a.b.c.d.e.f")
+# * The number N of appropriate characters to mask (e.g. 4)
+# * The direction in which to operate: if this argument is false or omitted,
+# mask characters from the beginning of the string; if it is true, operate
+# from the end of the string
+# Returns:
+# * The modified string, with the appropriate N alphanumerics masked (e.g.
+# "x.x.x.x.e.f")
+################################################################################
+sub mask_alphanumerics{
+ my $code = shift();
+ my $num = shift();
+ my $direction = shift();
+
+ # Don't include 'x' in the character class(es)
+ # here- we'd just replace it with a new 'x'
+ # anyway, but we also don't want to match
+ # things we've already replaced
+ if($direction){
+ # Operate on the end of the string
+ while($num--){
+ # This isn't pretty, but it works
+ $code =~ s/[0-9A-Za-wy-z]{1}([^0-9A-Za-wy-z]{0,})$/x/;
+ $code .= $1
+ if(length($1));
+ }
+ } else{
+ # Operate on the beginning of the string
+ while($num--){
+ $code =~ s/[0-9A-Za-wy-z]/x/;
+ }
+ }
+
+ return($code);
+
+}
+
+
+