diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2022-10-30 21:05:58 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2022-10-30 21:05:58 +0800 |
| commit | 0e28db3a465d9f72359946f64bd8e03b915276aa (patch) | |
| tree | 347a22cd67571aa74478c54b17284fe037decfe2 | |
| parent | 3dbc19b0e4629d0517e169dcbe4265227d1a1c37 (diff) | |
| download | perlweeklychallenge-club-0e28db3a465d9f72359946f64bd8e03b915276aa.tar.gz perlweeklychallenge-club-0e28db3a465d9f72359946f64bd8e03b915276aa.tar.bz2 perlweeklychallenge-club-0e28db3a465d9f72359946f64bd8e03b915276aa.zip | |
Week 188 Perl Solutions
| -rw-r--r-- | challenge-188/cheok-yin-fung/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-188/cheok-yin-fung/perl/ch-2.pl | 30 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-188/cheok-yin-fung/perl/ch-1.pl b/challenge-188/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..c700ee73c7 --- /dev/null +++ b/challenge-188/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,24 @@ +# The Weekly Challenge 188 +# Task 1 Divisible Pairs + +use v5.30.0; + +sub divisible_pairs { + my @list = $_[0]->@*; + my $k = $_[1]; + + my $p = 0; + for my $i (0..scalar @list - 2) { + for my $j ($i+1 .. scalar @list - 1) { + $p++ if !(($list[$i] + $list[$j]) % $k); + } + } + return $p; +} + +use Test::More tests=>5; +ok divisible_pairs([4,5,1,6], 2) == 2; +ok divisible_pairs([1,2,3,4], 2) == 2; +ok divisible_pairs([1,3,4,5], 3) == 2; +ok divisible_pairs([5,1,2,3], 4) == 2; +ok divisible_pairs([7,2,4,5], 4) == 1; diff --git a/challenge-188/cheok-yin-fung/perl/ch-2.pl b/challenge-188/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..546c3a7487 --- /dev/null +++ b/challenge-188/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,30 @@ +# The Weekly Challenge 188 +# Task 2 Total Zero +# +# Comment: Actually I don't very get how the operations run. + +use v5.30.0; + +sub total_zero { + my $t = 0; + my $x = $_[0]; + my $y = $_[1]; + while ($x != 0) { + if ($x >= $y) { + $x = $x - $y; + } + else { + $y = $y - $x; + } + $t++; + } + return $t; +} + +use Test::More tests=>6; +ok total_zero(5,4) == 5; +ok total_zero(4,6) == 3; +ok total_zero(2,5) == 4; +ok total_zero(3,1) == 3; +ok total_zero(7,4) == 5; +ok total_zero(2,2) == 1; |
