aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-188/adam-russell/perl/ch-1.pl35
-rw-r--r--challenge-188/adam-russell/perl/ch-2.pl32
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