aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-28 15:59:37 +0000
committerGitHub <noreply@github.com>2021-11-28 15:59:37 +0000
commit7c93288a6f5150f62ce4ea2097faaea07a347b46 (patch)
tree5fadfdbdf869a67a1262acd2562e357aea2e1786
parent7a464c2b7765b22c8b84dcde6867fc64824a6323 (diff)
parentfe5b0436f95bd8e611607ec5c3e78bc82447bcaf (diff)
downloadperlweeklychallenge-club-7c93288a6f5150f62ce4ea2097faaea07a347b46.tar.gz
perlweeklychallenge-club-7c93288a6f5150f62ce4ea2097faaea07a347b46.tar.bz2
perlweeklychallenge-club-7c93288a6f5150f62ce4ea2097faaea07a347b46.zip
Merge pull request #5292 from wanderdoc/master
Solutions to challenge-140
-rw-r--r--challenge-140/wanderdoc/perl/ch-1.pl79
-rw-r--r--challenge-140/wanderdoc/perl/ch-2.pl52
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-140/wanderdoc/perl/ch-1.pl b/challenge-140/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..1a909937cc
--- /dev/null
+++ b/challenge-140/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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)
+Example 1 Input: $a = 11; $b = 1; Output: 100
+Example 2 Input: $a = 101; $b = 1; Output: 110
+Example 3 Input: $a = 100; $b = 11; Output: 111
+=cut
+
+
+
+
+
+
+
+
+package BinNum
+{
+ use Carp;
+ use overload (
+ "+" => \&add_binary,
+ '""' => \&print_binary,
+ 'nomethod' => sub {croak 'no valid operation';});
+
+ sub new
+ {
+ my $class = shift;
+ my $value = shift;
+ return bless \$value => $class;
+ }
+
+
+ sub add_binary
+ {
+ my ($first, $second) = @_;
+ my $first_d = oct("0b$first" );
+
+
+ my $second_d = oct("0b$second");
+ my $output_d = $first_d + $second_d;
+ my $output = sprintf("%b", $output_d);
+
+ return bless \$output => ref($first);
+ }
+
+
+
+ sub print_binary
+ {
+ my $self = shift;
+ return $$self;
+ }
+
+ 1;
+}
+
+package main;
+
+my $val_1 = BinNum->new(11);
+my $val_2 = BinNum->new(1);
+print $val_1 + $val_2, $/;
+
+$val_1 = BinNum->new(101);
+$val_2 = BinNum->new(1);
+print $val_1 + $val_2, $/;
+
+$val_1 = BinNum->new(100);
+$val_2 = BinNum->new(11);
+print $val_1 + $val_2, $/;
+
+$val_1 = BinNum->new(111111);
+$val_2 = BinNum->new(111111);
+print $val_1 + $val_2, $/;
+
+$val_1 = BinNum->new(101010101);
+$val_2 = BinNum->new(101010101);
+print $val_1 + $val_2, $/; \ No newline at end of file
diff --git a/challenge-140/wanderdoc/perl/ch-2.pl b/challenge-140/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..5c3c9bc310
--- /dev/null
+++ b/challenge-140/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+Example 1 Input: $i = 2; $j = 3; $k = 4
+Output: 3
+
+Since the multiplication of 2 x 3 is as below:
+ 1 2 3
+ 2 4 6
+
+The sorted multiplication table: 1 2 2 3 4 6
+Now the 4th element in the table is "3".
+
+Example 2 Input: $i = 3; $j = 3; $k = 6
+Output: 4
+
+Since the multiplication of 3 x 3 is as below:
+ 1 2 3
+ 2 4 6
+ 3 6 9
+The sorted multiplication table: 1 2 2 3 3 4 6 6 9
+Now the 6th element in the table is "4".
+
+=cut
+
+use Getopt::Std;
+my %options;
+getopts("i:j:k:", \%options);
+
+my $i = $options{i} // 9; # 9;
+my $j = $options{j} // 9;
+my $k = $options{k} // 1;
+
+die "Only " . $i * $j . " elements in the output!$/" if $k > $i * $j;
+die "\$k is 1-based.$/" if $k == 0;
+
+
+my @output;
+for my $digit_2 ( 1 .. $j )
+{
+ for my $digit_1 ( 1 .. $i )
+ {
+ push @output, $digit_1 * $digit_2;
+ }
+}
+
+@output = sort {$a <=> $b} @output;
+unshift @output, ''; # $k is 1-based.
+print $output[$k], $/; \ No newline at end of file