aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--challenge-112/paulo-custodio/perl/ch-1.pl50
-rw-r--r--challenge-112/paulo-custodio/perl/ch-2.pl48
-rw-r--r--challenge-112/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-112/paulo-custodio/t/test-2.yaml25
-rwxr-xr-xchallenge-112/paulo-custodio/test.pl7
-rw-r--r--challenge-113/paulo-custodio/perl/ch-1.pl31
-rw-r--r--challenge-113/paulo-custodio/perl/ch-2.pl35
-rw-r--r--challenge-113/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-113/paulo-custodio/t/test-2.yaml19
-rwxr-xr-xchallenge-113/paulo-custodio/test.pl7
11 files changed, 253 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index a70b9e5e03..db4cc30c24 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
.precomp/
.DS_Store
.vstags
+.vscode
.*.swp
*~
*.class
diff --git a/challenge-112/paulo-custodio/perl/ch-1.pl b/challenge-112/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..058a6b9e1f
--- /dev/null
+++ b/challenge-112/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+# Challenge 112
+#
+# TASK #1 - Canonical Path
+# Submitted by: Mohammad S Anwar
+# You are given a string path, starting with a slash ‘/'.
+#
+# Write a script to convert the given absolute path to the simplified canonical
+# path.
+#
+# In a Unix-style file system:
+#
+# - A period '.' refers to the current directory
+# - A double period '..' refers to the directory up a level
+# - Multiple consecutive slashes ('//') are treated as a single slash '/'
+# The canonical path format:
+#
+# - The path starts with a single slash '/'.
+# - Any two directories are separated by a single slash '/'.
+# - The path does not end with a trailing '/'.
+# - The path only contains the directories on the path from the root directory
+# to the target file or directory
+# Example
+# Input: "/a/"
+# Output: "/a"
+#
+# Input: "/a/b//c/"
+# Output: "/a/b/c"
+#
+# Input: "/a/b/c/../.."
+# Output: "/a"
+
+use Modern::Perl;
+my $path = shift || '';
+say canon($path);
+
+sub canon {
+ my($path) = @_;
+ my $dir = qr/[^\/\.]+/;
+ for ($path) {
+ 1 while s{/\./}{/};
+ 1 while s{/\.$}{/};
+ 1 while s{/$dir/\.\./}{/};
+ 1 while s{/$dir/\.\.$}{/};
+ s{/+}{/}g;
+ s{/+$}{}g;
+ }
+ return $path;
+}
diff --git a/challenge-112/paulo-custodio/perl/ch-2.pl b/challenge-112/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3e866319b9
--- /dev/null
+++ b/challenge-112/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+# Challenge 112
+#
+# TASK #2 - Climb Stairs
+# Submitted by: Mohammad S Anwar
+# You are given $n steps to climb
+#
+# Write a script to find out the distinct ways to climb to the top. You are
+# allowed to climb either 1 or 2 steps at a time.
+#
+# Example
+# Input: $n = 3
+# Output: 3
+#
+# Option 1: 1 step + 1 step + 1 step
+# Option 2: 1 step + 2 steps
+# Option 3: 2 steps + 1 step
+#
+# Input: $n = 4
+# Output: 5
+#
+# Option 1: 1 step + 1 step + 1 step + 1 step
+# Option 2: 1 step + 1 step + 2 steps
+# Option 3: 2 steps + 1 step + 1 step
+# Option 4: 1 step + 2 steps + 1 step
+# Option 5: 2 steps + 2 steps
+
+use Modern::Perl;
+
+my $n = shift || 0;
+say count($n);
+
+sub count {
+ my($n) = @_;
+ if ($n <= 0) {
+ return 0;
+ }
+ elsif ($n == 1) {
+ return 1;
+ }
+ elsif ($n == 2) {
+ return 2;
+ }
+ else {
+ return count($n-1)+count($n-2);
+ }
+}
diff --git a/challenge-112/paulo-custodio/t/test-1.yaml b/challenge-112/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..379880547d
--- /dev/null
+++ b/challenge-112/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: /a/
+ input:
+ output: /a
+- setup:
+ cleanup:
+ args: /a/b//c/
+ input:
+ output: /a/b/c
+- setup:
+ cleanup:
+ args: /a/./b/./c/
+ input:
+ output: /a/b/c
+- setup:
+ cleanup:
+ args: /a/b/c/../../
+ input:
+ output: /a
diff --git a/challenge-112/paulo-custodio/t/test-2.yaml b/challenge-112/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..c1156b2fe4
--- /dev/null
+++ b/challenge-112/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 5
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: 8
diff --git a/challenge-112/paulo-custodio/test.pl b/challenge-112/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-112/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-113/paulo-custodio/perl/ch-1.pl b/challenge-113/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..b9badbba36
--- /dev/null
+++ b/challenge-113/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+# Challenge 113
+#
+# TASK #1 - Represent Integer
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N and a digit $D.
+#
+# Write a script to check if $N can be represented as a sum of positive
+# integers having $D at least once. If check passes print 1 otherwise 0.
+#
+# Example
+# Input: $N = 25, $D = 7
+# Output: 0 as there are 2 numbers between 1 and 25 having the digit 7
+# i.e. 7 and 17. If we add up both we don't get 25.
+#
+# Input: $N = 24, $D = 7
+# Output: 1
+
+use Modern::Perl;
+my($N, $D) = @ARGV;
+say represent($N||0, $D||0) ? 1 : 0;
+
+sub represent {
+ my($n, $d) = @_;
+ my $sum = 0;
+ for (1..$n) {
+ $sum += $_ if /$d/;
+ }
+ return $sum==$n;
+}
diff --git a/challenge-113/paulo-custodio/perl/ch-2.pl b/challenge-113/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3c709f59be
--- /dev/null
+++ b/challenge-113/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# Challenge 113
+#
+# TASK #2 - Recreate Binary Tree
+# Submitted by: Mohammad S Anwar
+# You are given a Binary Tree.
+#
+# Write a script to replace each node of the tree with the sum of all the
+# remaining nodes.
+#
+# Example
+# Input Binary Tree
+# 1
+# / \
+# 2 3
+# / / \
+# 4 5 6
+# \
+# 7
+# Output Binary Tree
+# 27
+# / \
+# 26 25
+# / / \
+# 24 23 22
+# \
+# 21
+
+use Modern::Perl;
+my $tree = join('', <>);
+my $sum = 0;
+$sum += $1 while $tree =~ /(\d+)/g;
+$tree =~ s/(\d+) ?/$sum - $1/ge;
+print $tree;
diff --git a/challenge-113/paulo-custodio/t/test-1.yaml b/challenge-113/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..f3baa5852a
--- /dev/null
+++ b/challenge-113/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 25 7
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 24 7
+ input:
+ output: 1
diff --git a/challenge-113/paulo-custodio/t/test-2.yaml b/challenge-113/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..47e288d60c
--- /dev/null
+++ b/challenge-113/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,19 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ | 1
+ | / \
+ | 2 3
+ | / / \
+ | 4 5 6
+ | \
+ | 7
+ output: |
+ | 27
+ | / \
+ | 26 25
+ | / / \
+ | 24 23 22
+ | \
+ | 21
diff --git a/challenge-113/paulo-custodio/test.pl b/challenge-113/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-113/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';