aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-11-24 01:11:12 -0500
committerAdam Russell <ac.russell@live.com>2021-11-24 01:11:12 -0500
commit68ac62b23aad0ad243a08ae791f5016c00358eb3 (patch)
treec261c0ba18f076f101987a7842d59b868c77a82b
parente57e8ba97ca974deeadbd7137390e99a38d8304d (diff)
downloadperlweeklychallenge-club-68ac62b23aad0ad243a08ae791f5016c00358eb3.tar.gz
perlweeklychallenge-club-68ac62b23aad0ad243a08ae791f5016c00358eb3.tar.bz2
perlweeklychallenge-club-68ac62b23aad0ad243a08ae791f5016c00358eb3.zip
Perl solutions
-rw-r--r--challenge-140/adam-russell/perl/ch-1.pl47
-rw-r--r--challenge-140/adam-russell/perl/ch-2.pl22
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-140/adam-russell/perl/ch-1.pl b/challenge-140/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..993fda172c
--- /dev/null
+++ b/challenge-140/adam-russell/perl/ch-1.pl
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+##
+# You are given two decimal coded binary numbers $a and $b.
+# Write a script to simulate the addition of the binary numbers.
+##
+
+sub add_binary{
+ my($x, $y) = @_;
+ my $sum = "";
+ my @a = reverse(split(//, $x));
+ my @b = reverse(split(//, $y));
+ if(@b > @a){
+ my @c = @b;
+ @b = @a;
+ @a = @c;
+ }
+ my $carry = 0;
+ for(my $d = 0; $d <= @a - 1; $d++){
+ my $d0 = $a[$d];
+ my $d1 = $b[$d];# || 0;
+ if($d1){
+ $sum = "0$sum", $carry = 0 if $d0 == 1 && $d1 == 1 && $carry == 1;
+ $sum = "1$sum", $carry = 0 if $d0 == 1 && $d1 == 0 && $carry == 0;
+ $sum = "0$sum", $carry = 1 if $d0 == 1 && $d1 == 1 && $carry == 0;
+ $sum = "0$sum", $carry = 1 if $d0 == 0 && $d1 == 1 && $carry == 1;
+ $sum = "0$sum", $carry = 0 if $d0 == 0 && $d1 == 0 && $carry == 0;
+ $sum = "1$sum", $carry = 0 if $d0 == 0 && $d1 == 0 && $carry == 1;
+ $sum = "0$sum", $carry = 1 if $d0 == 1 && $d1 == 0 && $carry == 1;
+ $sum = "1$sum", $carry = 0 if $d0 == 0 && $d1 == 1 && $carry == 0;
+ }
+ else{
+ $sum = "0$sum", $carry = 1, next if $d0 == 1 && $carry == 1;
+ $sum = "1$sum", $carry = 0, next if $d0 == 0 && $carry == 1;
+ $sum = "0$sum", $carry = 0, next if $d0 == 0 && $carry == 0;
+ $sum = "1$sum", $carry = 0, next if $d0 == 1 && $carry == 0;
+ }
+ }
+ $sum = "$carry$sum" if $carry == 1;
+ return $sum;
+}
+
+MAIN:{
+ print add_binary(11, 1) . "\n";
+ print add_binary(101, 1) . "\n";
+ print add_binary(100, 11) . "\n";
+}
diff --git a/challenge-140/adam-russell/perl/ch-2.pl b/challenge-140/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..5f05c0f54b
--- /dev/null
+++ b/challenge-140/adam-russell/perl/ch-2.pl
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+##
+# You are given 3 positive integersm $i, $j, $k.
+# Write a script to print the $kth element in the
+# sorted multiplication table of $i x $j.
+##
+sub nth_from_table{
+ my($i, $j, $k) = @_;
+ my @table;
+ for my $x (1 .. $i){
+ for my $y (1 .. $j){
+ push @table, $x * $y;
+ }
+ }
+ return (sort {$a <=> $b} @table)[$k - 1];
+}
+
+MAIN:{
+ print nth_from_table(2, 3, 4) . "\n";
+ print nth_from_table(3, 3, 6) . "\n";
+}