aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-28 20:19:16 +0200
committerAbigail <abigail@abigail.be>2021-04-28 20:19:16 +0200
commit888e31c6215c3c5174ad5e2f88351f2cd51e4c6f (patch)
treef5517d3cf683dea2faec47461684de230326ba52
parentaec04f00bb9b106567e84b0366024af0c47ca3de (diff)
downloadperlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.tar.gz
perlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.tar.bz2
perlweeklychallenge-club-888e31c6215c3c5174ad5e2f88351f2cd51e4c6f.zip
Perl and AWK solution should use same algorithm as we use in other languages.
-rw-r--r--challenge-110/abigail/awk/ch-1.awk14
-rw-r--r--challenge-110/abigail/perl/ch-1.pl14
2 files changed, 22 insertions, 6 deletions
diff --git a/challenge-110/abigail/awk/ch-1.awk b/challenge-110/abigail/awk/ch-1.awk
index 7b5cfbd981..f3b7780f04 100644
--- a/challenge-110/abigail/awk/ch-1.awk
+++ b/challenge-110/abigail/awk/ch-1.awk
@@ -8,9 +8,13 @@
# Run as: awk -f ch-1.awk < input-file
#
- {line = $0;
- gsub (/ */, "", line)} # Remove spaces
+{
+ line = $0;
+ gsub (/ */, "", line) # Remove spaces
+ sub (/^\+/, "00", line) # Replace leading + with 00
+ sub (/^\([0-9]{2}\)/, "0000", line) # Replace leading (NN) with 0000
+}
-match (line, /^[0-9]{14}$/) || # nnnn nnnnnnnnnn
-match (line, /^\+[0-9]{12}$/) || # +nn nnnnnnnnnn
-match (line, /^\([0-9]{2}\)[0-9]{10}$/) {print} # (nn) nnnnnnnnnn
+match (line, /^[0-9]{14}$/) { # Match exactly 14 digits
+ print
+}
diff --git a/challenge-110/abigail/perl/ch-1.pl b/challenge-110/abigail/perl/ch-1.pl
index 4ddafea3f1..c27f7d3ecc 100644
--- a/challenge-110/abigail/perl/ch-1.pl
+++ b/challenge-110/abigail/perl/ch-1.pl
@@ -28,8 +28,20 @@ use experimental 'lexical_subs';
# can completly ignore any white space in the input.
#
+#
+# To check for valid phone numbers, we will do the following:
+# - Remove all whitespace
+# - Replace a leading '+' by '00'
+# - Replace a leading '(NN)' by '0000'
+#
+# The number is valid, if and only if we are left with exactly 14 digits.
+#
+
while (<>) {
- print if s/\s+//gr =~ /^ (?: \+\d{12} | \(\d{2}\)\d{10} | \d{14} )$/ax
+ print if s{\s+} {}gr # Remove white space
+ =~ s{^\+} {00}r # Replace leading + with 00
+ =~ s{^\([0-9]{2}\)} {0000}r # Replace leading (NN) with 0000
+ =~ /^[0-9]{14}$/ # Check if 14 digits are left
}