diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-30 21:43:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-30 21:43:11 +0000 |
| commit | 3503252b180e28ddb77bd2227f181e2ccf935155 (patch) | |
| tree | 691125ac21706b58036103c0613a4ce3bad3db95 | |
| parent | 529384076e7a6bf970fb24383012e15a68ed415f (diff) | |
| parent | 88ccfa1bd0214b4425430bb746a5884dde830377 (diff) | |
| download | perlweeklychallenge-club-3503252b180e28ddb77bd2227f181e2ccf935155.tar.gz perlweeklychallenge-club-3503252b180e28ddb77bd2227f181e2ccf935155.tar.bz2 perlweeklychallenge-club-3503252b180e28ddb77bd2227f181e2ccf935155.zip | |
Merge pull request #6992 from adamcrussell/challenge-188
initial commit
| -rw-r--r-- | challenge-188/adam-russell/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-188/adam-russell/perl/ch-2.pl | 32 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-188/adam-russell/perl/ch-1.pl b/challenge-188/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..432fc0b993 --- /dev/null +++ b/challenge-188/adam-russell/perl/ch-1.pl @@ -0,0 +1,35 @@ +use v5.36; +use strict; +use warnings; +## +# You are given list of integers @list of size $n and divisor $k. +# Write a script to find out count of pairs in the given list that satisfies the following rules. +# The pair (i, j) is eligible if and only if +# a) 0 <= i < j < len(list) +# b) list[i] + list[j] is divisible by k +## + +sub divisible_pairs{ + my($numbers, $k) = @_; + my @pairs; + for my $i (0 .. @{$numbers} - 1){ + for my $j ($i + 1 .. @{$numbers} - 1){ + push @pairs, [$i, $j] if(($numbers->[$i] + $numbers->[$j]) % $k == 0); + } + } + return @pairs; +} + +MAIN:{ + my @pairs; + @pairs = divisible_pairs([4, 5, 1, 6], 2); + print @pairs . "\n"; + @pairs = divisible_pairs([1, 2, 3, 4], 2); + print @pairs . "\n"; + @pairs = divisible_pairs([1, 3, 4, 5], 3); + print @pairs . "\n"; + @pairs = divisible_pairs([5, 1, 2, 3], 4); + print @pairs . "\n"; + @pairs = divisible_pairs([7, 2, 4, 5], 4); + print @pairs . "\n"; +}
\ No newline at end of file diff --git a/challenge-188/adam-russell/perl/ch-2.pl b/challenge-188/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..672664a376 --- /dev/null +++ b/challenge-188/adam-russell/perl/ch-2.pl @@ -0,0 +1,32 @@ +use v5.36; +use strict; +use warnings; +## +# You are given two positive integers $x and $y. +# Write a script to find out the number of operations needed to make both ZERO. +# Each operation is made up either of the followings: +# $x = $x - $y if $x >= $y +# or +# $y = $y - $x if $y >= $x (using the original value of $x) +## + +sub count_zero{ + my($x, $y) = @_; + my $count = 0; + { + my $x_original = $x; + $x = $x - $y if $x >= $y; + $y = $y - $x_original if $y >= $x_original; + $count++; + redo unless $x == 0 && $y == 0; + } + return $count; +} + +MAIN:{ + say count_zero(5, 4); + say count_zero(4, 6); + say count_zero(2, 5); + say count_zero(3, 1); + say count_zero(7, 4); +}
\ No newline at end of file |
