diff options
| author | PerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com> | 2022-01-02 17:32:26 +1000 |
|---|---|---|
| committer | PerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com> | 2022-01-02 17:32:26 +1000 |
| commit | 67844a2e2045c2908710782651e9e47ee05dfa99 (patch) | |
| tree | db1f855e664b1b7ae1ec8db5a7b7ef9c345f7ddd /challenge-145 | |
| parent | 233b172f74dd306cbc79129f40399c259ec1fa20 (diff) | |
| download | perlweeklychallenge-club-67844a2e2045c2908710782651e9e47ee05dfa99.tar.gz perlweeklychallenge-club-67844a2e2045c2908710782651e9e47ee05dfa99.tar.bz2 perlweeklychallenge-club-67844a2e2045c2908710782651e9e47ee05dfa99.zip | |
Perl & Raku solutions to Task 1 of the Perl Weekly Challenge #145
Diffstat (limited to 'challenge-145')
| -rw-r--r-- | challenge-145/athanasius/perl/ch-1.pl | 122 | ||||
| -rw-r--r-- | challenge-145/athanasius/raku/ch-1.raku | 122 |
2 files changed, 244 insertions, 0 deletions
diff --git a/challenge-145/athanasius/perl/ch-1.pl b/challenge-145/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..27ca854495 --- /dev/null +++ b/challenge-145/athanasius/perl/ch-1.pl @@ -0,0 +1,122 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly Challenge 145 +========================= + +TASK #1 +------- +*Dot Product* + +Submitted by: Mohammad S Anwar + +You are given 2 arrays of same size, @a and @b. + +Write a script to implement Dot Product. + +Example: + + @a = (1, 2, 3); + @b = (4, 5, 6); + + $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2021 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=comment + +Interface +--------- +Each of the two arrays must be entered on the command line as a string contain- +ing comma-separated real numbers. Each comma separator may, optionally, be +followed by whitespace. + +Solution +-------- +The dot product calculation is performed in this expression: + + sum0 pairmap { $a * $b } mesh $A, $B + +which uses three subroutines from the core module List::Util. + +=cut +#============================================================================== + +use strict; +use warnings; +use Const::Fast; +use List::Util 1.56 qw( mesh pairmap sum0 ); +use Regexp::Common qw( number ); + +const my $USAGE => +"Usage: + perl $0 <a> <b> + + <a> Comma-separated list of n real numbers + <b> Comma-separated list of n real numbers\n"; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 145, Task #1: Dot Product (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my $args = scalar @ARGV; + $args == 2 + or error( "Expected 2 command line arguments, found $args" ); + + my $A = parse_str( $ARGV[ 0 ] ); + my $B = parse_str( $ARGV[ 1 ] ); + + scalar @$A == scalar @$B + or error( 'The input lists have different sizes' ); + + printf "Input: \@a = (%s)\n", join ', ', @$A; + printf " \@b = (%s)\n", join ', ', @$B; + + printf "Output: %d\n", sum0 pairmap { $a * $b } mesh $A, $B; +} + +#------------------------------------------------------------------------------ +sub parse_str +#------------------------------------------------------------------------------ +{ + my ($str) = @_; + my @list = split / \, \s* /x, $str; + my @nums; + + for my $s (@list) + { + $s =~ / ^ $RE{num}{real} $ /x + or error( qq["$s" is not a valid number] ); + + push @nums, $s; + } + + return \@nums; +} + +#------------------------------------------------------------------------------ +sub error +#------------------------------------------------------------------------------ +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +############################################################################### diff --git a/challenge-145/athanasius/raku/ch-1.raku b/challenge-145/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..6c4771bdc1 --- /dev/null +++ b/challenge-145/athanasius/raku/ch-1.raku @@ -0,0 +1,122 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly Challenge 145 +========================= + +TASK #1 +------- +*Dot Product* + +Submitted by: Mohammad S Anwar + +You are given 2 arrays of same size, @a and @b. + +Write a script to implement Dot Product. + +Example: + + @a = (1, 2, 3); + @b = (4, 5, 6); + + $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2021 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=begin comment + +Interface +--------- +Each of the two arrays must be entered on the command line as a string contain- +ing comma-separated real numbers. Each comma separator may, optionally, be +followed by whitespace. + +Solution +-------- +The dot product calculation is performed in this expression: + + [+] @a Z* @b + +which uses two of Raku's built-in metaoperators: + - the zip metaoperator Z*; and + - the reduction metaoperator [+] + +=end comment +#============================================================================== + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 145, Task #1: Dot Product (Raku)\n".put; +} + +#============================================================================== +sub MAIN +( + Str:D $a, #= Comma-separated list of n real numbers + Str:D $b #= Comma-separated list of n real numbers +) +#============================================================================== +{ + my Real @a = parse-str( $a ); + my Real @b = parse-str( $b ); + + @a.elems == @b.elems + or error( 'The input lists have different sizes' ); + + "Input: @a = (%s)\n".printf: @a.join: ', '; + " @b = (%s)\n".printf: @b.join: ', '; + + "Output: %d\n".printf: [+] @a Z* @b; +} + +#------------------------------------------------------------------------------ +sub parse-str( Str:D $str --> Array:D[Real:D] ) +#------------------------------------------------------------------------------ +{ + my Str @list = $str.split: / \, \s* /, :skip-empty; + my Real @nums; + + for @list -> Str $s + { + val( $s ) ~~ (IntStr, NumStr, RatStr).any + or error( qq["$s" is not a valid number] ); + + @nums.push: val( $s ); + } + + return @nums; +} + +#------------------------------------------------------------------------------ +sub error( Str:D $message ) +#------------------------------------------------------------------------------ +{ + "ERROR: $message".put; + + USAGE(); + + exit; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +############################################################################## |
