diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-03 01:40:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-03 01:40:52 +0000 |
| commit | a3067a8253db2d0d7859fc618b63fa8f2957f273 (patch) | |
| tree | 53d1e7fa0ae2e51c957334e8b1ab37630b3ae0c6 /challenge-145 | |
| parent | 3db9338016cafc8a03aafabc58fd8831e3d51d58 (diff) | |
| parent | 4951401be2fdb7e6f87c3826e76069196277efe8 (diff) | |
| download | perlweeklychallenge-club-a3067a8253db2d0d7859fc618b63fa8f2957f273.tar.gz perlweeklychallenge-club-a3067a8253db2d0d7859fc618b63fa8f2957f273.tar.bz2 perlweeklychallenge-club-a3067a8253db2d0d7859fc618b63fa8f2957f273.zip | |
Merge pull request #5453 from mattneleigh/pwc145
new file: challenge-145/mattneleigh/perl/ch-1.pl
Diffstat (limited to 'challenge-145')
| -rwxr-xr-x | challenge-145/mattneleigh/perl/ch-1.pl | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/challenge-145/mattneleigh/perl/ch-1.pl b/challenge-145/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..c9634dfbe0 --- /dev/null +++ b/challenge-145/mattneleigh/perl/ch-1.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @pairs = ( + # Given case + [ + [ 1, 2, 3 ], + [ 4, 5, 6 ] + ], + + # Additional test cases + [ + [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], + [ 10, 11, 12, 13, 14, 15, 16, 17, 18 ] + ], + [ + [ 1, 2, 3, 4, 5 ], + [ 1, 2, 3, 4 ] + ] +); +my $pair; + +foreach $pair (@pairs){ + my $dot_product = dot_product($pair->[0], $pair->[1]); + + printf("\@a = (%s);\n", join(", ", @{$pair->[0]})); + printf("\@b = (%s);\n", join(", ", @{$pair->[1]})); + printf( + "Dot product = %s\n\n", + defined($dot_product) ? $dot_product : "Undefined due to error" + ); +} + + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the dot product of two vectors +# Takes two arguments: +# * A reference to an array of scalar numeric values, A +# * A reference to an array of scalar numeric values, B +# Returns on success: +# * The dot product- a single scalar value- of A and B. +# Returns on error: +# * undef if A and B do not have the same number of members +################################################################################ +sub dot_product{ + my $a = shift(); + my $b = shift(); + + my $i = scalar(@{$a}); + + # Make sure both arrays are the same + # length + return(undef) + unless($i == scalar(@{$b})); + + my $sum = 0; + + # Accumulate the sum of the products + # of all corresponding members of + # each array + while($i--){ + $sum += $a->[$i] * $b->[$i]; + } + + return($sum); + +} + + + |
