diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2024-04-29 06:53:39 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2024-04-29 06:53:39 +0800 |
| commit | c3fd9f1ebf61a2c1b617e93a4cd089d6103880cb (patch) | |
| tree | cee4c1e075a5d118fe5c1ba9554bae9541dadd2d | |
| parent | 7592a868df05ecb400eccebe7b9705bfe3fad48d (diff) | |
| download | perlweeklychallenge-club-c3fd9f1ebf61a2c1b617e93a4cd089d6103880cb.tar.gz perlweeklychallenge-club-c3fd9f1ebf61a2c1b617e93a4cd089d6103880cb.tar.bz2 perlweeklychallenge-club-c3fd9f1ebf61a2c1b617e93a4cd089d6103880cb.zip | |
Week 266
| -rw-r--r-- | challenge-266/cheok-yin-fung/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-266/cheok-yin-fung/perl/ch-2.pl | 46 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-266/cheok-yin-fung/perl/ch-1.pl b/challenge-266/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..29210cd224 --- /dev/null +++ b/challenge-266/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,31 @@ +# The Weekly Challenge 266 +# Task 1 Uncommon Words +use v5.30.0; +use warnings; + +sub uw0 { + my $line1 = $_[0]; + my $line2 = $_[1]; + my @words1 = split " ", $line1; + my %hash1; + my %hash2; + $hash1{$_}++ for @words1; + my @words2 = split " ", $line2; + for (@words2) { + $hash2{$_}++ if !defined($hash1{$_}); + } + return [grep {$hash2{$_} == 1} keys %hash2]; +} + +sub uw { + my $line1 = $_[0]; + my $line2 = $_[1]; + return [@{uw0($line1, $line2)}, @{uw0($line2, $line1)}]; +} + +use Test2::V0; +use Test2::Tools::Compare qw/bag/; +is uw('Mango is sweet', 'Mango is sour'), bag {item 'sweet'; item 'sour'; end}; +is uw('Mango Mango', 'Orange'), bag {item 'Orange'; end}; +is uw('Mango is Mango', 'Orange is Orange'), bag {end}; +done_testing(); diff --git a/challenge-266/cheok-yin-fung/perl/ch-2.pl b/challenge-266/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..19bbf8b3bd --- /dev/null +++ b/challenge-266/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,46 @@ +# The Weekly Challenge 266 +# Task 2 X Matrix +use v5.30.0; +use warnings; + +sub xm { + my $matrix = $_[0]; + my $dim0 = $matrix->$#*; + for my $i (0..$dim0) { + for my $j (0..$dim0) { + if ($i == $j || $i+$j == $dim0) { + return 0 if $matrix->[$i]->[$j] == 0; + } + else { + return 0 if $matrix->[$i]->[$j] != 0; + } + } + } + return 1; +} + +use Test::More tests=>3; +ok xm( + [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ] + ) + == 1; + +ok xm( + [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ] + ) + == 0; + +ok xm( + [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ] + ) + == 1; |
