aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-140/perlboy1967/perl/ch-1.pl88
-rwxr-xr-xchallenge-140/perlboy1967/perl/ch-2.pl25
2 files changed, 113 insertions, 0 deletions
diff --git a/challenge-140/perlboy1967/perl/ch-1.pl b/challenge-140/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..b3e8358af1
--- /dev/null
+++ b/challenge-140/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,88 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 139
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-139/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #1 › Add Binary
+Submitted by: Mohammad S Anwar
+
+You are given two decimal-coded binary numbers, $a and $b.
+
+Write a script to simulate the addition of the given binary numbers.
+
+ || The script should simulate something like $a + $b. (operator overloading)
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+my $b1 = new myBin(0b001);
+my $b2 = new myBin(0b101);
+my $b3 = new myBin(0b011);
+
+printf "b1: %s, b2: %s, b3: %s\n", $b1, $b2, $b3;
+
+my $A = new myBin($b3);
+printf "A: %s\n", $A;
+
+my $B = new myBin($b1 + 7);
+printf "B: %s (%s + 7)\n", $B, $b1;
+$B += $b2;
+printf "B: %s (%s + 7 + %s)\n", $B, $b1, $b2;
+$B += $b3;
+printf "B: %s (%s + %s + %s)\n", $B, $b1, $b2, $b3;
+$B = $b1 + $b3;
+printf "B: %s (%s + %s)\n", $B, $b1, $b3;
+$B = 9 + $b1;
+printf "B: %s (%s + %s)\n", $B, 9, $b1;
+
+
+package myBin;
+
+use List::MoreUtils qw(pairwise);
+
+use overload
+ '""' => sub { '0b'.join '',reverse @{$_[0]} },
+ '+' => \&_plus;
+
+sub new {
+ my ($self,$arg) = @_;
+
+ # Accept a myBin object to initialize
+ # or an integer number
+ my @data = ();
+ if (defined $arg) {
+ if (ref $arg) {
+ @data = @$arg;
+ } else {
+ @data = reverse split //,sprintf '%b', $arg;
+ }
+ }
+
+ bless \@data,$self;
+}
+
+sub _plus {
+ my ($self, $other) = @_;
+
+ $other = new myBin($other) if (!ref $other);
+
+ my ($carry,$res) = (0);
+ my @result = pairwise {
+ no warnings 'once';
+ $res = ($a//0) + ($b//0) + $carry;
+ $carry = ($res >> 1);
+ $res &= 1;
+ } @$self, @$other;
+ push(@result,1) if $carry;
+
+ bless \@result;
+}
+
+1;
diff --git a/challenge-140/perlboy1967/perl/ch-2.pl b/challenge-140/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..8ce285cc84
--- /dev/null
+++ b/challenge-140/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 140
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #2 › Multiplication Table
+Submitted by: Mohammad S Anwar
+
+You are given 3 positive integers, $i, $j and $k.
+
+Write a script to print the $kth element in the sorted multiplication table of $i and $j.
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+use List::MoreUtils qw(arrayify);
+my($i,$j,$k)=@ARGV;
+printf"%s\n",(sort{$a<=>$b}arrayify map{my$n=$_;$_=[map{$n*$_}1..$j]}1..$i)[$k-1];