aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-02 11:08:49 +0100
committerGitHub <noreply@github.com>2021-05-02 11:08:49 +0100
commita9a9bf413fa86ba37cb11e2c74e84c468be66ff9 (patch)
tree9848cb51f31a5cd4b5353c2d6a0eacc7028affc1
parent574c79d2ebe60c8adab39ab36dfebd2fb940a503 (diff)
parent85dde0b5afc34f67736f6b4e04265f6146804a9d (diff)
downloadperlweeklychallenge-club-a9a9bf413fa86ba37cb11e2c74e84c468be66ff9.tar.gz
perlweeklychallenge-club-a9a9bf413fa86ba37cb11e2c74e84c468be66ff9.tar.bz2
perlweeklychallenge-club-a9a9bf413fa86ba37cb11e2c74e84c468be66ff9.zip
Merge pull request #3989 from PerlMonk-Athanasius/branch-for-challenge-110
Perl & Raku solutions to Tasks 1 & 2 of the Perl Weekly Challenge #110
-rw-r--r--challenge-110/athanasius/perl/ch-1.pl159
-rw-r--r--challenge-110/athanasius/perl/ch-1_Example.txt5
-rw-r--r--challenge-110/athanasius/perl/ch-2.pl141
-rw-r--r--challenge-110/athanasius/perl/ch-2_Example.txt5
-rw-r--r--challenge-110/athanasius/raku/ch-1.raku131
-rw-r--r--challenge-110/athanasius/raku/ch-1_Example.txt5
-rw-r--r--challenge-110/athanasius/raku/ch-2.raku119
-rw-r--r--challenge-110/athanasius/raku/ch-2_Example.txt5
8 files changed, 570 insertions, 0 deletions
diff --git a/challenge-110/athanasius/perl/ch-1.pl b/challenge-110/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..d4f814525d
--- /dev/null
+++ b/challenge-110/athanasius/perl/ch-1.pl
@@ -0,0 +1,159 @@
+#!perl
+
+###############################################################################
+=comment
+
+Perl Weekly Challenge 110
+=========================
+
+TASK #1
+-------
+*Valid Phone Numbers*
+
+Submitted by: Mohammad S Anwar
+
+You are given a text file.
+
+Write a script to display all valid phone numbers in the given text file.
+
+Acceptable Phone Number Formats
+
+ +nn nnnnnnnnnn
+ (nn) nnnnnnnnnn
+ nnnn nnnnnnnnnn
+
+Input File
+
+ 0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+ (44) 1148820341
+ 00 1148820341
+
+Output
+
+ 0044 1148820341
+ +44 1148820341
+ (44) 1148820341
+
+=cut
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=comment
+
+Assumption
+----------
+Following the format of the example input file, each phone number is assumed to
+occur on a separate line.
+
+Algorithm
+---------
+Each of the three valid formats is given its own matching regular expression.
+If a given phone number does not match any of these, it is filtered out as
+invalid.
+
+=cut
+#==============================================================================
+
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $FILE => './ch-1_Example.txt';
+const my $USAGE =>
+"Usage:
+ perl $0 [<file>]
+
+ [<file>] Name of input text file (optional)\n";
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 110, Task #1: Valid Phone Numbers (Perl)\n\n";
+}
+
+#==============================================================================
+MAIN:
+#==============================================================================
+{
+ my $file = parse_command_line();
+ my $count = 0;
+
+ print "Input: $file\n\nOutput:\n\n";
+
+ open(my $fh, '<', $file)
+ or die qq[Cannot open file "$file" for reading, stopped];
+
+ while (my $line = <$fh>)
+ {
+ if (my $number = valid_number( $line ))
+ {
+ ++$count;
+ print " $number\n";
+ }
+ }
+
+ close $fh
+ or die qq[Cannot close file "$file", stopped];
+
+ print "\n$count valid phone numbers found\n";
+}
+
+#------------------------------------------------------------------------------
+sub valid_number
+#------------------------------------------------------------------------------
+{
+ my ($line) = @_;
+ my $number = '';
+
+ if ($line =~ / ^ \s* \+ (\d{2}) \s+ (\d{10}) \s* $ /x)
+ {
+ $number = " +$1 $2";
+ }
+ elsif ($line =~ / ^ \s* \( (\d{2}) \) \s+ (\d{10}) \s* $ /x)
+ {
+ $number = "($1) $2";
+ }
+ elsif ($line =~ / ^ \s* (\d{4}) \s+ (\d{10}) \s* $ /x)
+ {
+ $number = "$1 $2";
+ }
+
+ return $number;
+}
+
+#------------------------------------------------------------------------------
+sub parse_command_line
+#------------------------------------------------------------------------------
+{
+ my $args = scalar @ARGV;
+
+ 0 <= $args <= 1
+ or error( "Expected 0 or 1 command line arguments, found $args" );
+
+ my $file = ($args == 1) ? $ARGV[0] : $FILE;
+
+ -e $file or error( qq[File "$file" not found] );
+ -z $file and error( qq[File "$file" is empty] );
+ -f $file or error( qq[File "$file" is not a plain file] );
+
+ return $file;
+}
+
+#------------------------------------------------------------------------------
+sub error
+#------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+###############################################################################
diff --git a/challenge-110/athanasius/perl/ch-1_Example.txt b/challenge-110/athanasius/perl/ch-1_Example.txt
new file mode 100644
index 0000000000..b99c9a12db
--- /dev/null
+++ b/challenge-110/athanasius/perl/ch-1_Example.txt
@@ -0,0 +1,5 @@
+ 0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+ (44) 1148820341
+ 00 1148820341
diff --git a/challenge-110/athanasius/perl/ch-2.pl b/challenge-110/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..2dbc7204d8
--- /dev/null
+++ b/challenge-110/athanasius/perl/ch-2.pl
@@ -0,0 +1,141 @@
+#!perl
+
+###############################################################################
+=comment
+
+Perl Weekly Challenge 110
+=========================
+
+TASK #2
+-------
+*Transpose File*
+
+Submitted by: Mohammad S Anwar
+
+You are given a text file.
+
+Write a script to transpose the contents of the given file.
+
+Input File
+
+ name,age,sex
+ Mohammad,45,m
+ Joe,20,m
+ Julie,35,f
+ Cristina,10,f
+
+Output:
+
+ name,Mohammad,Joe,Julie,Cristina
+ age,45,20,35,10
+ sex,m,m,f,f
+
+=cut
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=comment
+
+Assumptions
+-----------
+It is assumed that the first line of the input file contains column headers for
+the remaining data; it is further assumed that data always appears in this same
+order within each line.
+
+It is not specified in the Task description whether the output should be to
+screen or to file. To simplify the exercise, output is to the screen only.
+
+Algorithm
+---------
+Read the column headers into an array, then read the remaining data into a
+hash. Read the data back from the hash in the required order (with hash keys
+ordered according to their order in the header array).
+
+=cut
+#==============================================================================
+
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $FILE => './ch-2_Example.txt';
+const my $USAGE =>
+"Usage:
+ perl $0 [<file>]
+
+ [<file>] Name of input text file (optional)\n";
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 110, Task #2: Transpose File (Perl)\n\n";
+}
+
+#==============================================================================
+MAIN:
+#==============================================================================
+{
+ my $file = parse_command_line();
+
+ print "Input: $file\n\n";
+
+ open(my $fh, '<', $file)
+ or die qq[Cannot open file "$file" for reading, stopped];
+
+ my $line = <$fh>; # Header: column names
+ chomp $line;
+ print " $line\n";
+ my @headers = split /,/, $line;
+ my %data = map { $_ => [ $_ ] } @headers;
+
+ while (my $line = <$fh>)
+ {
+ chomp $line;
+ print " $line\n";
+
+ my @entries = split /,/, $line;
+
+ push $data{ $_ }->@*, shift @entries for @headers;
+ }
+
+ close $fh
+ or die qq[Cannot close file "$file", stopped];
+
+ print "\nOutput:\n\n";
+ printf " %s\n", join ',', $data{ $_ }->@* for @headers;
+}
+
+#------------------------------------------------------------------------------
+sub parse_command_line
+#------------------------------------------------------------------------------
+{
+ my $args = scalar @ARGV;
+
+ 0 <= $args <= 1
+ or error( "Expected 0 or 1 command line arguments, found $args" );
+
+ my $file = ($args == 1) ? $ARGV[0] : $FILE;
+
+ -e $file or error( qq[File "$file" not found] );
+ -z $file and error( qq[File "$file" is empty] );
+ -f $file or error( qq[File "$file" is not a plain file] );
+
+ return $file;
+}
+
+#------------------------------------------------------------------------------
+sub error
+#------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+###############################################################################
diff --git a/challenge-110/athanasius/perl/ch-2_Example.txt b/challenge-110/athanasius/perl/ch-2_Example.txt
new file mode 100644
index 0000000000..716ebdce75
--- /dev/null
+++ b/challenge-110/athanasius/perl/ch-2_Example.txt
@@ -0,0 +1,5 @@
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f
diff --git a/challenge-110/athanasius/raku/ch-1.raku b/challenge-110/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..ef9ed263a9
--- /dev/null
+++ b/challenge-110/athanasius/raku/ch-1.raku
@@ -0,0 +1,131 @@
+use v6d;
+
+###############################################################################
+=begin comment
+
+Perl Weekly Challenge 110
+=========================
+
+TASK #1
+-------
+*Valid Phone Numbers*
+
+Submitted by: Mohammad S Anwar
+
+You are given a text file.
+
+Write a script to display all valid phone numbers in the given text file.
+
+Acceptable Phone Number Formats
+
+ +nn nnnnnnnnnn
+ (nn) nnnnnnnnnn
+ nnnn nnnnnnnnnn
+
+Input File
+
+ 0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+ (44) 1148820341
+ 00 1148820341
+
+Output
+
+ 0044 1148820341
+ +44 1148820341
+ (44) 1148820341
+
+=end comment
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=begin comment
+
+Assumption
+----------
+Following the format of the example input file, each phone number is assumed to
+occur on a separate line.
+
+Algorithm
+---------
+Each of the three valid formats is given its own matching regular expression.
+If a given phone number does not match any of these, it is filtered out as
+invalid.
+
+=end comment
+#==============================================================================
+
+my Str constant $FILE = './ch-1_Example.txt';
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ "\nChallenge 110, Task #1: Valid Phone Numbers (Raku)\n".put;
+}
+
+#==============================================================================
+sub MAIN
+(
+ #| Name of input text file (optional)
+ # Check that the file:
+ Str:D $file where { $file.IO.e && # - exists
+ $file.IO.f && # - is a plain file
+ $file.IO.s > 0 } = $FILE; # - is not empty
+)
+#==============================================================================
+{
+ my UInt $count = 0;
+
+ "Input: $file\n\nOutput:\n".put;
+
+ for $file.IO.lines -> Str $line
+ {
+ if my Str $number = valid-number( $line )
+ {
+ ++$count;
+ " $number".put;
+ }
+ }
+
+ "\n$count valid phone numbers found".put;
+}
+
+#------------------------------------------------------------------------------
+sub valid-number( Str:D $line --> Str:D )
+#------------------------------------------------------------------------------
+{
+ my Str $number = '';
+
+ if $line ~~ / ^ \s* \+ (\d ** 2) \s+ (\d ** 10) \s* $ /
+ {
+ $number = " +$0 $1";
+ }
+ elsif $line ~~ / ^ \s* \( (\d ** 2) \) \s+ (\d ** 10) \s* $ /
+ {
+ $number = "($0) $1";
+ }
+ elsif $line ~~ / ^ \s* (\d ** 4) \s+ (\d ** 10) \s* $ /
+ {
+ $number = "$0 $1";
+ }
+
+ return $number;
+}
+
+#------------------------------------------------------------------------------
+sub USAGE()
+#------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
+ $usage.put;
+}
+
+##############################################################################
diff --git a/challenge-110/athanasius/raku/ch-1_Example.txt b/challenge-110/athanasius/raku/ch-1_Example.txt
new file mode 100644
index 0000000000..b99c9a12db
--- /dev/null
+++ b/challenge-110/athanasius/raku/ch-1_Example.txt
@@ -0,0 +1,5 @@
+ 0044 1148820341
+ +44 1148820341
+ 44-11-4882-0341
+ (44) 1148820341
+ 00 1148820341
diff --git a/challenge-110/athanasius/raku/ch-2.raku b/challenge-110/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..b263e4f7a2
--- /dev/null
+++ b/challenge-110/athanasius/raku/ch-2.raku
@@ -0,0 +1,119 @@
+use v6d;
+
+###############################################################################
+=begin comment
+
+Perl Weekly Challenge 110
+=========================
+
+TASK #2
+-------
+*Transpose File*
+
+Submitted by: Mohammad S Anwar
+
+You are given a text file.
+
+Write a script to transpose the contents of the given file.
+
+Input File
+
+ name,age,sex
+ Mohammad,45,m
+ Joe,20,m
+ Julie,35,f
+ Cristina,10,f
+
+Output:
+
+ name,Mohammad,Joe,Julie,Cristina
+ age,45,20,35,10
+ sex,m,m,f,f
+
+=end comment
+###############################################################################
+
+#--------------------------------------#
+# Copyright © 2021 PerlMonk Athanasius #
+#--------------------------------------#
+
+#==============================================================================
+=begin comment
+
+Assumptions
+-----------
+It is assumed that the first line of the input file contains column headers for
+the remaining data; it is further assumed that data always appears in this same
+order within each line.
+
+It is not specified in the Task description whether the output should be to
+screen or to file. To simplify the exercise, output is to the screen only.
+
+Algorithm
+---------
+Read the column headers into an array, then read the remaining data into a
+hash. Read the data back from the hash in the required order (with hash keys
+ordered according to their order in the header array).
+
+=end comment
+#==============================================================================
+
+my Str constant $FILE = './ch-2_Example.txt';
+
+#------------------------------------------------------------------------------
+BEGIN
+#------------------------------------------------------------------------------
+{
+ "\nChallenge 110, Task #2: Transpose File (Raku)\n".put;
+}
+
+#==============================================================================
+sub MAIN
+(
+ #| Name of input text file (optional)
+ # Check that the file:
+ Str:D $file where { $file.IO.e && # - exists
+ $file.IO.f && # - is a plain file
+ $file.IO.s > 0 } = $FILE; # - is not empty
+)
+#==============================================================================
+{
+ "Input: $file\n".put;
+
+ my Str @headers;
+ my Array[Str] %data;
+ my Bool $first = True;
+
+ for $file.IO.lines -> Str $line # Note: chomps by default
+ {
+ if $first # Header: column names
+ {
+ @headers = $line.split: ',', :skip-empty;
+ %data = @headers.map: { $_ => Array[Str].new( $_ ) };
+ $first = False;
+ }
+ else # Body: column values
+ {
+ my Str @entries = $line.split: ',', :skip-empty;
+
+ %data{ $_ }.push: @entries.shift for @headers;
+ }
+
+ " $line".put;
+ }
+
+ "\nOutput:\n".put;
+ " %s\n".printf: %data{ $_ }.join( ',' ) for @headers;
+}
+
+#------------------------------------------------------------------------------
+sub USAGE()
+#------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
+ $usage.put;
+}
+
+##############################################################################
diff --git a/challenge-110/athanasius/raku/ch-2_Example.txt b/challenge-110/athanasius/raku/ch-2_Example.txt
new file mode 100644
index 0000000000..716ebdce75
--- /dev/null
+++ b/challenge-110/athanasius/raku/ch-2_Example.txt
@@ -0,0 +1,5 @@
+name,age,sex
+Mohammad,45,m
+Joe,20,m
+Julie,35,f
+Cristina,10,f