aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-110/perlboy1967/perl/ch-1.pl62
-rwxr-xr-xchallenge-110/perlboy1967/perl/ch-2.pl48
-rw-r--r--challenge-110/perlboy1967/perl/input1.txt5
-rw-r--r--challenge-110/perlboy1967/perl/input2.txt5
4 files changed, 120 insertions, 0 deletions
diff --git a/challenge-110/perlboy1967/perl/ch-1.pl b/challenge-110/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..b0a192c0b5
--- /dev/null
+++ b/challenge-110/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 110
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK1
+#
+# Task 1 - Valid Phone Numbers
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use File::Basename qw(dirname);
+use File::Slurp;
+
+use Test::More;
+use Test::Deep;
+
+# Prototype(s)
+sub validatePhoneNumber($);
+
+# Work relative from script directory
+chdir(dirname($0));
+
+cmp_deeply ([map { validatePhoneNumber($_) } read_file('input1.txt')],
+ ['0044 1148820341',
+ '+44 1148820341',
+ '(44) 1148820341']);
+
+done_testing;
+
+
+# Interpreted the phone number rules to:
+# 1) +nn nnnnnnnnnn:
+# '+' followed 2-4 digits (see: https://countrycode.org/)
+# followed by one or more spaces followed 6 or more digits
+# 2) nnnn nnnnnnnnnn:
+# '00' followed by 2-4 digits followed by one or more spaces
+# followed by 6 or more digits
+# 3) (nn) nnnnnnnnnn:
+# '(' followed by 2-4 digits followed by ')' followed by
+# one or more spaces followed by 6 or more digits
+
+sub validatePhoneNumber($) {
+ my ($n) = @_;
+
+ # trim input
+ $n =~ s/^\s*(.+?)\s*$/$1/;
+
+ my ($ccMinLen,$ccMaxLen) = (2,4);
+ my $pMinLen = 6;
+
+ return $n if ($n =~ m/(
+ ^ \+ \d{$ccMinLen,$ccMaxLen} \s+ \d{$pMinLen,} $ |
+ ^ 00 \d{$ccMinLen,$ccMaxLen} \s+ \d{$pMinLen,} $ |
+ ^ \( \d{$ccMinLen,$ccMaxLen} \) \s+ \d{$pMinLen,} $
+ )/x);
+
+ return;
+}
+
diff --git a/challenge-110/perlboy1967/perl/ch-2.pl b/challenge-110/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..656d263fc4
--- /dev/null
+++ b/challenge-110/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 110
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-110/#TASK2
+#
+# Task 2 - Transpose File
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use File::Basename qw(dirname);
+use File::Slurp;
+
+use Test::More;
+use Test::Deep;
+
+# Prototype(s)
+sub transposeFile($);
+
+# Work relative from script directory
+chdir(dirname($0));
+
+cmp_deeply ([transposeFile('input2.txt')],
+ ['name,Mohammad,Joe,Julie,Cristina',
+ 'age,45,20,35,10',
+ 'sex,m,m,f,f']);
+
+done_testing;
+
+
+sub transposeFile($) {
+ my ($f) = @_;
+
+ # Read and trim
+ my @lines = map { s/^\s*(.*?)\s*$/$1/; $_ } read_file($f);
+
+ my @out;
+ map {
+ my $i = 0;
+ map { push(@{$out[$i++]},$_) } split(/,/, $_);
+ } @lines;
+
+ return map {join(',',@$_)} @out;
+}
+
diff --git a/challenge-110/perlboy1967/perl/input1.txt b/challenge-110/perlboy1967/perl/input1.txt
new file mode 100644
index 0000000000..48d6254741
--- /dev/null
+++ b/challenge-110/perlboy1967/perl/input1.txt
@@ -0,0 +1,5 @@
+0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+(44) 1148820341
+ 00 1148820341
diff --git a/challenge-110/perlboy1967/perl/input2.txt b/challenge-110/perlboy1967/perl/input2.txt
new file mode 100644
index 0000000000..716ebdce75
--- /dev/null
+++ b/challenge-110/perlboy1967/perl/input2.txt
@@ -0,0 +1,5 @@
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f