diff options
| -rw-r--r-- | challenge-170/cheok-yin-fung/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-170/cheok-yin-fung/perl/ch-2.pl | 118 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-170/cheok-yin-fung/perl/ch-1.pl b/challenge-170/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..c86bcfdd10 --- /dev/null +++ b/challenge-170/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl +# The Weekly Challenge 170 +# Task 1 Primorial Numbers + +use v5.24.0; +use warnings; + +my @prime = (2, 3, 5, 7, 11, 13, 17, 19, 23); + +say my $x = 1; + +say $x*=$prime[$_] for 0..8; + diff --git a/challenge-170/cheok-yin-fung/perl/ch-2.pl b/challenge-170/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..f73e80c2dc --- /dev/null +++ b/challenge-170/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,118 @@ +#!/usr/bin/perl +# The Weekly Challenge 170 +# Task 2 Kronecer Product +use v5.24.0; +use warnings; + +sub kp { + my $m1 = $_[0]; + my $m2 = $_[1]; + my $ans = []; + for my $i ($m1->@*) { + for my $j ($m2->@*) { + push $ans->@*, []; + for my $x ($i->@*) { + push $ans->[-1]->@*, map {$x*$_} ($j->@*); + } + } + } + return $ans; +} + + + +use Test::More tests => 8; + +is_deeply kp([[13]], [[17]]), [[221]], + "Scalar test case I"; + +is_deeply kp( + [ [3] ], + [ [1,2],[3,-1] ] + ), + [ [3,6],[9,-3] ], + "Scalar test case II"; + +my $v1 = [ [5], [6], [7], [8] ]; +my $v2 = [ [1, 2, 3] ]; +is_deeply kp($v1, $v2), + [ [5, 10, 15], + [6, 12, 18], + [7, 14, 21], + [8, 16, 24]], + "Two vector test case I"; + +is_deeply kp($v2, $v1), + [ [5, 10, 15], + [6, 12, 18], + [7, 14, 21], + [8, 16, 24]], + "Two vector test case II"; + +is_deeply kp( + [ [1,2],[3,4] ], + [ [5,6],[7,8] ] + ), + + [ [ 5, 6, 10, 12], + [ 7, 8, 14, 16], + [15, 18, 20, 24], + [21, 24, 28, 32] ], + "Example Given"; + +is_deeply kp( + [ [1, -4, 7], + [-2, 3, 3] ], + + [ [8, -9, -6, 5 ], + [1, -3, -4, 7 ], + [2, 8, -8, -3 ], + [1, 2, -5, -1 ] ] + ), + + [ [8, -9, -6, 5, -32, 36, 24, -20, 56, -63, -42, 35 ], + [1, -3, -4, 7, -4, 12, 16, -28, 7, -21, -28, 49 ], + [2, 8, -8, -3, -8, -32, 32, 12, 14, 56, -56, -21 ], + [1, 2, -5, -1, -4, -8, 20, 4, 7, 14, -35, -7 ], + [-16, 18, 12, -10, 24, -27, -18, 15, 24, -27, -18, 15 ], + [-2, 6, 8, -14, 3, -9, -12, 21, 3, -9, -12, 21 ], + [-4, -16, 16, 6, 6, 24, -24, -9, 6, 24, -24, -9 ], + [-2, -4, 10, 2, 3, 6, -15, -3, 3, 6, -15, -3] ], + "Example on English Wikipedia"; + +is_deeply kp( + [ [1, 2], + [3, 4], + [5, 6] ], + + [ [7, 8], [9, 0] ], + ), + + [ [7, 8, 14, 16 ], + [ 9, 0, 18, 0], + [21, 24, 28, 32 ], + [ 27, 0, 36, 0], + [35, 40, 42, 48 ], + [ 45, 0, 54, 0] ], + "Example on German Wikipedia"; + +is_deeply kp( + [ [1, 3, 2], + [1, 0, 0], + [1, 2, 2] ], + + [ [0, 5], + [5, 0], + [1, 1] ] + ), + + [ [0, 5, 0, 15, 0, 10], + [5, 0, 15, 0, 10, 0], + [1, 1, 3, 3, 2, 2], + [0, 5, 0, 0, 0, 0], + [5, 0, 0, 0, 0, 0], + [1, 1, 0, 0, 0, 0], + [0, 5, 0, 10, 0, 10], + [5, 0, 10, 0, 10, 0], + [1, 1, 2, 2, 2, 2] ], + "Example on French Wikipedia"; |
