aboutsummaryrefslogtreecommitdiff
path: root/challenge-110
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-01 20:58:12 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-01 20:58:12 +0100
commit98b600782520071bf6c8c7ff605104d68ece4a6b (patch)
treedeee7d37fba292b3f8c40cff11aa6244bf9a5d84 /challenge-110
parent7c280e1fa297f9483329642defc6b7b0202f911a (diff)
downloadperlweeklychallenge-club-98b600782520071bf6c8c7ff605104d68ece4a6b.tar.gz
perlweeklychallenge-club-98b600782520071bf6c8c7ff605104d68ece4a6b.tar.bz2
perlweeklychallenge-club-98b600782520071bf6c8c7ff605104d68ece4a6b.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-110')
-rw-r--r--challenge-110/laurent-rosenfeld/awk/ch-1.awk7
-rw-r--r--challenge-110/laurent-rosenfeld/awk/ch-2.awk13
-rw-r--r--challenge-110/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-110/laurent-rosenfeld/julia/ch-1.julia10
-rw-r--r--challenge-110/laurent-rosenfeld/perl/ch-1.pl11
-rw-r--r--challenge-110/laurent-rosenfeld/perl/ch-2.pl19
-rw-r--r--challenge-110/laurent-rosenfeld/python/ch-1.py8
-rw-r--r--challenge-110/laurent-rosenfeld/raku/ch-1.raku9
-rw-r--r--challenge-110/laurent-rosenfeld/raku/ch-2.raku16
-rw-r--r--challenge-110/laurent-rosenfeld/ruby/ch-1.rb9
-rw-r--r--challenge-110/laurent-rosenfeld/scala/ch-1.scala17
11 files changed, 120 insertions, 0 deletions
diff --git a/challenge-110/laurent-rosenfeld/awk/ch-1.awk b/challenge-110/laurent-rosenfeld/awk/ch-1.awk
new file mode 100644
index 0000000000..15bd4e4e6b
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/awk/ch-1.awk
@@ -0,0 +1,7 @@
+echo '
+0044 1148820341
++44 1148820342
+44-11-4882-0343
+(44) 1148820344
+00 1148820346
+' | awk '/([0-9]{4}|\+[0-9]{2}|\([0-9]{2}\))\s+[0-9]{10}/ { print $0 }'
diff --git a/challenge-110/laurent-rosenfeld/awk/ch-2.awk b/challenge-110/laurent-rosenfeld/awk/ch-2.awk
new file mode 100644
index 0000000000..6c848c89bf
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/awk/ch-2.awk
@@ -0,0 +1,13 @@
+BEGIN{
+ FS = ","
+}
+{ table[0,NR] = $1 }
+{ table[1,NR] = $2 }
+{ table[2,NR] = $3 }
+{ max = NR }
+END {
+ for (i = 0; i < 3; i++) {
+ for (j = 1; j < max - 1; j++) printf "%s,", table[i,j]
+ printf "%s\n", table[i,max-1]
+ }
+}
diff --git a/challenge-110/laurent-rosenfeld/blog.txt b/challenge-110/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..ba0835c8a6
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2021/05/perl-weekly-challenge-110-valid-phone-numbers-and-transposed-file.html
diff --git a/challenge-110/laurent-rosenfeld/julia/ch-1.julia b/challenge-110/laurent-rosenfeld/julia/ch-1.julia
new file mode 100644
index 0000000000..3f79251724
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/julia/ch-1.julia
@@ -0,0 +1,10 @@
+tests = ["foo 0044 1148820341 42", "xyz +44 1148820342 abc",
+ "44-11-4882-0343", " (44) 1148820344 ", "00 1148820345"]
+pattern = r"(\d{4}|\+\d\d|\(\d\d\))\s+\d{10}"
+
+for str in tests
+ m = match(pattern, str)
+ if (! (m === nothing))
+ println(m.match)
+ end
+end
diff --git a/challenge-110/laurent-rosenfeld/perl/ch-1.pl b/challenge-110/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..6d5189fc88
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,11 @@
+use strict;
+use warnings;
+use feature "say";
+
+# simulate a text file with an array of strings
+my @tests = (" 0044 1148820341 42 ", " +44 1148820342 abc",
+ " 44-11-4882-0343 ", " (44) 1148820344 foo (39) 1148820345", " 00 1148820346");
+
+for my $str (@tests) {
+ say $1 while $str =~ / ( (?: \d {4} | \+ \d\d | \( \d\d \) ) \s+ \d{10} ) /gx;
+}
diff --git a/challenge-110/laurent-rosenfeld/perl/ch-2.pl b/challenge-110/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..e99e552add
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use feature "say";
+
+# Note: input array simulated with a string
+my $in_string = "name,age,sex Mohammad,45,m
+ Joe,20,m Julie,35,f Cristina,10,f";
+my @input = split /\s+/, $in_string;
+my @transposed;
+for my $in (@input) {
+ my $i = 0;
+ for my $str (split /,/, $in) {
+ push @{$transposed[$i]}, $str;
+ $i++;
+ }
+}
+for my $line (@transposed) {
+ say join ',', @$line;
+}
diff --git a/challenge-110/laurent-rosenfeld/python/ch-1.py b/challenge-110/laurent-rosenfeld/python/ch-1.py
new file mode 100644
index 0000000000..a46cbbbcfd
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/python/ch-1.py
@@ -0,0 +1,8 @@
+import re
+
+tests = ("foo 0044 1148820341 42", "xyz +44 1148820342 abc", "44-11-4882-0343", " (44) 1148820344 ", "00 1148820345")
+
+for str in tests:
+ match = re.search("(\d{4}|\+\d\d|\(\d\d\))\s+\d{10}", str)
+ if (match):
+ print (match.group())
diff --git a/challenge-110/laurent-rosenfeld/raku/ch-1.raku b/challenge-110/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..d947e7a961
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,9 @@
+use v6;
+
+my @tests = " 0044 1148820341 42 ", " +44 1148820342 abc",
+ " 44-11-4882-0343 ", " (44) 1148820344 ", " 00 1148820345";
+
+my $ten-dgts = rx/\d ** 10/;
+for @tests -> $str {
+ say ~$0 if $str ~~ / ( [ \d ** 4 || '+' \d\d || \( \d\d \) ] \s+ <$ten-dgts> ) /;
+}
diff --git a/challenge-110/laurent-rosenfeld/raku/ch-2.raku b/challenge-110/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..87b0f55ea8
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,16 @@
+use v6;
+
+my @input = <name,age,sex Mohammad,45,m
+ Joe,20,m Julie,35,f Cristina,10,f>;
+
+my @transposed;
+for @input -> $in {
+ my $i = 0;
+ for $in.split(',') -> $str {
+ push @transposed[$i], $str;
+ $i++;
+ }
+}
+for @transposed -> @line {
+ say @line.join(',');
+}
diff --git a/challenge-110/laurent-rosenfeld/ruby/ch-1.rb b/challenge-110/laurent-rosenfeld/ruby/ch-1.rb
new file mode 100644
index 0000000000..2f139afdde
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/ruby/ch-1.rb
@@ -0,0 +1,9 @@
+tests = ["foo 0044 1148820341 42", "xyz +44 1148820342 abc",
+ "44-11-4882-0343", " (44) 1148820344 ", "00 1148820345"]
+pattern = %r{((\d{4}|\+\d{2}|\(\d{2}\))\s+\d{10})}
+for str in tests
+ match = str.match(pattern)
+ if match then
+ print(match[0], "\n")
+ end
+end
diff --git a/challenge-110/laurent-rosenfeld/scala/ch-1.scala b/challenge-110/laurent-rosenfeld/scala/ch-1.scala
new file mode 100644
index 0000000000..6a23b5ab85
--- /dev/null
+++ b/challenge-110/laurent-rosenfeld/scala/ch-1.scala
@@ -0,0 +1,17 @@
+import scala.util.matching.Regex
+
+object phoneNumber extends App {
+ val pattern = "((?:\\d{4}|\\+\\d\\d|\\(\\d\\d\\))\\s+\\d{10})".r
+ val tests = Array(
+ " 0044 1148820341 42 ",
+ " +44 1148820342 abc",
+ " 44-11-4882-0343 ",
+ " (44) 1148820344 (33) 1148820345",
+ " 00 1148820346"
+ );
+ for (str <- tests) {
+ if (pattern.unanchored.matches(str)) {
+ println((pattern findAllIn str).mkString(", "))
+ }
+ }
+}