aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-22 15:05:28 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-22 15:05:28 +0000
commit43041c5f15754374a8ed4f5beee5d00248a04fc2 (patch)
tree4cf4bdf6e759c382b3527af43b98bc6668188d67
parente57e8ba97ca974deeadbd7137390e99a38d8304d (diff)
downloadperlweeklychallenge-club-43041c5f15754374a8ed4f5beee5d00248a04fc2.tar.gz
perlweeklychallenge-club-43041c5f15754374a8ed4f5beee5d00248a04fc2.tar.bz2
perlweeklychallenge-club-43041c5f15754374a8ed4f5beee5d00248a04fc2.zip
Add Perl and Python solutions to challenge 140
-rw-r--r--challenge-140/paulo-custodio/perl/ch-1.pl46
-rw-r--r--challenge-140/paulo-custodio/perl/ch-2.pl53
-rw-r--r--challenge-140/paulo-custodio/python/ch-1.py38
-rw-r--r--challenge-140/paulo-custodio/python/ch-2.py49
-rw-r--r--challenge-140/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-140/paulo-custodio/t/test-2.yaml10
6 files changed, 211 insertions, 0 deletions
diff --git a/challenge-140/paulo-custodio/perl/ch-1.pl b/challenge-140/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..440c1b4996
--- /dev/null
+++ b/challenge-140/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+# 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)
+#
+# 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
+
+use Modern::Perl;
+
+{
+ package Binary;
+
+ sub new {
+ my($class, $n) = @_;
+ $n //= 0;
+ return bless \$n, $class;
+ }
+
+ sub add {
+ my($self, $other) = @_;
+ my $a = oct("0b".$$self);
+ my $b = oct("0b".$$other);
+ return ref($self)->new(sprintf("%b", $a+$b));
+ }
+
+ use overload '+' => \&add;
+ use overload '""' => sub { my($self) = @_; return $$self; }
+}
+
+my $a = Binary->new(shift);
+my $b = Binary->new(shift);
+my $c = $a+$b;
+say $c;
diff --git a/challenge-140/paulo-custodio/perl/ch-2.pl b/challenge-140/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..2e2ad24d83
--- /dev/null
+++ b/challenge-140/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+
+# 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.
+#
+# 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".
+
+use Modern::Perl;
+
+sub item {
+ my($i, $j, $k) = @_;
+ my @table;
+ for my $x (1..$j) {
+ for my $y (1..$i) {
+ push @table, $x*$y;
+ }
+ }
+ @table = sort @table;
+ return $table[$k-1];
+}
+
+say item(@ARGV);
diff --git a/challenge-140/paulo-custodio/python/ch-1.py b/challenge-140/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..364d04f5a9
--- /dev/null
+++ b/challenge-140/paulo-custodio/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/usr/bin/python3
+
+# 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)
+#
+# 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
+
+import sys
+
+class Binary():
+ def __init__(self, n):
+ self.n = n
+
+ def __str__(self):
+ return str(self.n)
+
+ def __add__(self, other):
+ a = int(str(self.n), 2)
+ b = int(str(other.n), 2)
+ return Binary(int("{:b}".format(a+b)))
+
+a = Binary(int(sys.argv[1]))
+b = Binary(int(sys.argv[2]))
+c = a+b
+print(c)
diff --git a/challenge-140/paulo-custodio/python/ch-2.py b/challenge-140/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..54ea2a76ba
--- /dev/null
+++ b/challenge-140/paulo-custodio/python/ch-2.py
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# 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.
+#
+# 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".
+
+import sys
+
+def item(i, j, k):
+ table = []
+ for x in range(1, j+1):
+ for y in range(1, i+1):
+ table.append(x*y)
+ table.sort()
+ return table[k-1]
+
+print(item(*[int(x) for x in sys.argv[1:4]]))
diff --git a/challenge-140/paulo-custodio/t/test-1.yaml b/challenge-140/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1324bfbeee
--- /dev/null
+++ b/challenge-140/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 11 1
+ input:
+ output: 100
+- setup:
+ cleanup:
+ args: 101 1
+ input:
+ output: 110
+- setup:
+ cleanup:
+ args: 100 11
+ input:
+ output: 111
diff --git a/challenge-140/paulo-custodio/t/test-2.yaml b/challenge-140/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..835786110e
--- /dev/null
+++ b/challenge-140/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 2 3 4
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 3 3 6
+ input:
+ output: 4