aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-13 17:50:01 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-13 17:50:01 +0000
commit03a64ac276a7674a178d5f985840f8d76aea8896 (patch)
tree4c3d60769c2335481d01f6b581251ff2f6d955c8
parent399e89c940f55d6265bcabf48ad9172df7c1523b (diff)
parent3f102edbf082ea633a4f832969809eb693cf738a (diff)
downloadperlweeklychallenge-club-03a64ac276a7674a178d5f985840f8d76aea8896.tar.gz
perlweeklychallenge-club-03a64ac276a7674a178d5f985840f8d76aea8896.tar.bz2
perlweeklychallenge-club-03a64ac276a7674a178d5f985840f8d76aea8896.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-143/paulo-custodio/perl/ch-1.pl82
-rw-r--r--challenge-143/paulo-custodio/perl/ch-2.pl49
-rw-r--r--challenge-143/paulo-custodio/python/ch-1.py72
-rw-r--r--challenge-143/paulo-custodio/python/ch-2.py42
-rw-r--r--challenge-143/paulo-custodio/t/test-1.yaml25
-rw-r--r--challenge-143/paulo-custodio/t/test-2.yaml15
6 files changed, 285 insertions, 0 deletions
diff --git a/challenge-143/paulo-custodio/perl/ch-1.pl b/challenge-143/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..90b6fe6506
--- /dev/null
+++ b/challenge-143/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+
+# TASK #1 > Calculator
+# Submitted by: Mohammad S Anwar
+# You are given a string, $s, containing mathematical expression.
+#
+# Write a script to print the result of the mathematical expression. To keep
+# it simple, please only accept + - * ().
+#
+# Example 1:
+# Input: $s = "10 + 20 - 5"
+# Output: 25
+# Example 2:
+# Input: $s = "(10 + 20 - 5) * 2"
+# Output: 50
+
+# Note: alternative one-liner solution: say eval("@ARGV")
+
+use Modern::Perl;
+
+# ($input, $value) = expr($input)
+sub expr {
+ my($input) = @_;
+ ($input, my $value) = factor($input);
+ while (1) {
+ if ($input =~ s/^\s*\*//) {
+ ($input, my $b) = factor($input);
+ $value *= $b;
+ }
+ elsif ($input =~ s/^\s*\///) {
+ ($input, my $b) = factor($input);
+ $value /= $b;
+ }
+ elsif ($input =~ /^\s*(?:\)|$)/) {
+ return ($input, $value);
+ }
+ else {
+ die "expected / or * at: $input\n";
+ }
+ }
+}
+
+# ($input, $value) = factor($input)
+sub factor {
+ my($input) = @_;
+ ($input, my $value) = term($input);
+ while (1) {
+ if ($input =~ s/^\s*\+//) {
+ ($input, my $b) = term($input);
+ $value += $b;
+ }
+ elsif ($input =~ s/^\s*\-//) {
+ ($input, my $b) = term($input);
+ $value -= $b;
+ }
+ else {
+ return ($input, $value);
+ }
+ }
+}
+
+# ($input, $value) = term($input)
+sub term {
+ my($input) = @_;
+ while (1) {
+ if ($input =~ s/^\s*([-+]?\d+)//) {
+ return ($input, $1);
+ }
+ elsif ($input =~ s/^\s*\(//) {
+ ($input, my $value) = expr($input);
+ $input =~ s/^\s*\)// or die "expected ) at: $input\n";
+ return ($input, $value);
+ }
+ else {
+ die "expected ( or number at: $input\n";
+ }
+ }
+}
+
+my $input = "@ARGV";
+($input, my $value) = expr($input);
+say $value;
diff --git a/challenge-143/paulo-custodio/perl/ch-2.pl b/challenge-143/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..a805a8180b
--- /dev/null
+++ b/challenge-143/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# TASK #2 > Stealthy Number
+# Submitted by: Mohammad S Anwar
+# You are given a positive number, $n.
+#
+# Write a script to find out if the given number is Stealthy Number.
+#
+# A positive integer N is stealthy, if there exist positive integers a, b, c, d
+# such that a * b = c * d = N and a + b = c + d + 1.
+#
+# Example 1
+# Input: $n = 36
+# Output: 1
+#
+# Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1.
+# Example 2
+# Input: $n = 12
+# Output: 1
+#
+# Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1
+# Example 3
+# Input: $n = 6
+# Output: 0
+#
+# Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1
+
+use Modern::Perl;
+
+sub is_stealthy {
+ my($n) = @_;
+ for my $a (1..$n) {
+ if ($n % $a == 0) {
+ my $b = $n / $a; # a*b=n
+ for my $c (1..$n) {
+ if ($n % $c == 0) {
+ my $d = $n / $c; # c*d=n
+ if ($a+$b==$c+$d+1) {
+ return 1;
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+my $n = shift//1;
+say is_stealthy($n);
diff --git a/challenge-143/paulo-custodio/python/ch-1.py b/challenge-143/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a81c52fd0e
--- /dev/null
+++ b/challenge-143/paulo-custodio/python/ch-1.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python3
+
+# TASK #1 > Calculator
+# Submitted by: Mohammad S Anwar
+# You are given a string, $s, containing mathematical expression.
+#
+# Write a script to print the result of the mathematical expression. To keep
+# it simple, please only accept + - * ().
+#
+# Example 1:
+# Input: $s = "10 + 20 - 5"
+# Output: 25
+# Example 2:
+# Input: $s = "(10 + 20 - 5) * 2"
+# Output: 50
+
+import sys
+import re
+
+def expr(input):
+ input, value = factor(input)
+ while True:
+ input = input.strip()+" "
+ if input[0]=='*':
+ input = input[1:]
+ input, b = factor(input)
+ value *= b
+ elif input[0]=='/':
+ input = input[1:]
+ input, b = factor(input)
+ value /= b
+ else:
+ return input, value
+
+def factor(input):
+ input, value = term(input)
+ while True:
+ input = input.strip()+" "
+ if input[0]=='+':
+ input = input[1:]
+ input, b = term(input)
+ value += b
+ elif input[0]=='-':
+ input = input[1:]
+ input, b = term(input)
+ value -= b
+ else:
+ return input, value
+
+def term(input):
+ input = input.strip()+" "
+ match = re.match(r"[-+]?\d+", input)
+ if match:
+ value = int(match.group(0))
+ input = input[match.end(0):]
+ return input, value
+ elif input[0]=='(':
+ input = input[1:]
+ input, value = expr(input)
+ input = input.strip()+" "
+ if input[0]!=')':
+ print("expected )")
+ sys.exit(1)
+ input = input[1:]
+ return input, value
+ else:
+ print("expected ( or number")
+ sys.exit(1)
+
+input = " ".join(sys.argv[1:])
+input, value = expr(input)
+print(value)
diff --git a/challenge-143/paulo-custodio/python/ch-2.py b/challenge-143/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..3d1976d3f0
--- /dev/null
+++ b/challenge-143/paulo-custodio/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/python3
+
+# TASK #2 > Stealthy Number
+# Submitted by: Mohammad S Anwar
+# You are given a positive number, $n.
+#
+# Write a script to find out if the given number is Stealthy Number.
+#
+# A positive integer N is stealthy, if there exist positive integers a, b, c, d
+# such that a * b = c * d = N and a + b = c + d + 1.
+#
+# Example 1
+# Input: $n = 36
+# Output: 1
+#
+# Since 36 = 4 (a) * 9 (b) = 6 (c) * 6 (d) and 4 (a) + 9 (b) = 6 (c) + 6 (d) + 1.
+# Example 2
+# Input: $n = 12
+# Output: 1
+#
+# Since 2 * 6 = 3 * 4 and 2 + 6 = 3 + 4 + 1
+# Example 3
+# Input: $n = 6
+# Output: 0
+#
+# Since 2 * 3 = 1 * 6 but 2 + 3 != 1 + 6 + 1
+
+import sys
+
+def is_stealthy(n):
+ for a in range(1, n+1):
+ if n%a==0:
+ b = n/a # a*b=n
+ for c in range(1, n+1):
+ if n%c==0:
+ d = n/c # c*d=n
+ if a+b==c+d+1:
+ return 1
+ return 0
+
+n = int(sys.argv[1])
+print(is_stealthy(n))
diff --git a/challenge-143/paulo-custodio/t/test-1.yaml b/challenge-143/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..f0a1e9d550
--- /dev/null
+++ b/challenge-143/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 10 + 20 - 5
+ input:
+ output: 25
+- setup:
+ cleanup:
+ args: 10 + 20 - -5
+ input:
+ output: 35
+- setup:
+ cleanup:
+ args: "'(10 + 20 - 5) * 2'"
+ input:
+ output: 50
+- setup:
+ cleanup:
+ args: "'(10 + 20 - 5) / 2'"
+ input:
+ output: 12.5
+- setup:
+ cleanup:
+ args: "'(10 + 20 - 5) * -2'"
+ input:
+ output: -50
diff --git a/challenge-143/paulo-custodio/t/test-2.yaml b/challenge-143/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..e301a2d362
--- /dev/null
+++ b/challenge-143/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 36
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 12
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: 0