aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2021-04-27 16:16:03 -0600
committerRyan Thompson <i@ry.ca>2021-04-27 16:16:03 -0600
commit4a9fd571537c5d55567d56711d21fbda09bcffec (patch)
treed4658c482bbd2769cc12047bd8238a85c78ce584
parent32b7f3041862a27fd0a89328b57879750269b560 (diff)
downloadperlweeklychallenge-club-4a9fd571537c5d55567d56711d21fbda09bcffec.tar.gz
perlweeklychallenge-club-4a9fd571537c5d55567d56711d21fbda09bcffec.tar.bz2
perlweeklychallenge-club-4a9fd571537c5d55567d56711d21fbda09bcffec.zip
Ryan's week 110 solutions
-rw-r--r--challenge-110/ryan-thompson/README.md14
-rwxr-xr-xchallenge-110/ryan-thompson/perl/ch-1.pl31
-rwxr-xr-xchallenge-110/ryan-thompson/perl/ch-2.pl17
-rwxr-xr-xchallenge-110/ryan-thompson/raku/ch-1.raku17
-rwxr-xr-xchallenge-110/ryan-thompson/raku/ch-2.raku9
5 files changed, 81 insertions, 7 deletions
diff --git a/challenge-110/ryan-thompson/README.md b/challenge-110/ryan-thompson/README.md
index 698e3ee64f..3dd1e955d3 100644
--- a/challenge-110/ryan-thompson/README.md
+++ b/challenge-110/ryan-thompson/README.md
@@ -1,19 +1,19 @@
# Ryan Thompson
-## Week 056 Solutions
+## Week 110 Solutions
-### Task 1 › Diff-K
+### Task 1 › Phone Number Validation
* [Perl](perl/ch-1.pl)
- * [Raku](raku/ch-1.p6)
+ * [Raku](raku/ch-1.raku)
-### Task 2 › Path Sum
+### Task 2 › Transpose CSV File
* [Perl](perl/ch-2.pl)
- * [Raku](raku/ch-2.p6)
+ * [Raku](raku/ch-2.raku)
## Blogs
- * [Diff-K](https://ry.ca/2020/04/diff-k/)
- * [Path Sum](https://ry.ca/2020/04/path-sum/)
+ * [Phone Number Validation](https://ry.ca/2021/04/phone-number-validation)
+ * [Transpose CSV File](https://ry.ca/2021/04/transpose-csv-file)
diff --git a/challenge-110/ryan-thompson/perl/ch-1.pl b/challenge-110/ryan-thompson/perl/ch-1.pl
new file mode 100755
index 0000000000..d994daaa16
--- /dev/null
+++ b/challenge-110/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - Parse phone numbers using templates
+#
+# 2021 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+no warnings 'uninitialized';
+
+# Valid phone number templates, where n is a digit.
+# Internal whitespace is squashed, leading/trailing is trimmed.
+my %valid =
+ map { y/ / /sr => 1 }
+ split /\s*(\n|$)\s*/, q{
+ +nn nnnnnnnnnn
+ (nn) nnnnnnnnnn
+ nnnn nnnnnnnnnn
+ };
+
+print for grep { check_number($_) } <>;
+
+# Check if a number matches any template in %valid
+sub check_number {
+ local $_ = shift;
+
+ s/^\s+//, s/\s+$//; # Trim leading and trailing whitespace
+ y/0-9/n/, y/ / /s; # Replace digits, squash internal spaces
+
+ return $_ if $valid{$_};
+}
diff --git a/challenge-110/ryan-thompson/perl/ch-2.pl b/challenge-110/ryan-thompson/perl/ch-2.pl
new file mode 100755
index 0000000000..ee4b277007
--- /dev/null
+++ b/challenge-110/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Transpose a file (CSV)
+#
+# 2021 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+use autodie;
+no warnings 'uninitialized';
+
+my @rows = map { chomp; [ split ',' ] } <>;
+
+for my $col (0..$#{$rows[0]}) {
+ say join ',', map { $_->[$col] } @rows
+}
diff --git a/challenge-110/ryan-thompson/raku/ch-1.raku b/challenge-110/ryan-thompson/raku/ch-1.raku
new file mode 100755
index 0000000000..13ef3bf49e
--- /dev/null
+++ b/challenge-110/ryan-thompson/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+
+# ch-1.raku - Phone number validation
+#
+# Ryan Thompson <rjt@cpan.org>
+
+# Phone number, with country code, optional whitespace,
+# inner whitespace, and local number
+grammar Phone-Number {
+ token TOP { <ows> <cc> <iws> <local> <ows> }
+ token cc { \+ \d\d | \( \d\d \) | \d ** 4 }
+ token ows { \s* }
+ token iws { \s+ }
+ token local { \d ** 10 }
+}
+
+.say for lines.grep: { Phone-Number.parse($_) }
diff --git a/challenge-110/ryan-thompson/raku/ch-2.raku b/challenge-110/ryan-thompson/raku/ch-2.raku
new file mode 100755
index 0000000000..34d9e87e69
--- /dev/null
+++ b/challenge-110/ryan-thompson/raku/ch-2.raku
@@ -0,0 +1,9 @@
+#!/usr/bin/env raku
+
+# ch-2.raku - Transpose a CSV file
+#
+# Ryan Thompson <rjt@cpan.org>
+
+sub MAIN( Str $file ) {
+ ([Z] $file.IO.lines.map(*.split(','))).map(*.join(','))».say;
+}