aboutsummaryrefslogtreecommitdiff
path: root/challenge-113
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-15 23:10:58 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-15 23:10:58 +0100
commit87d45d0ee52acc6701981beea4e15b6432d54c22 (patch)
tree1d454fb091e4428c8f6243baf33bb605f39d00e0 /challenge-113
parent9f9a45e099e9d4675962476178ed03926f7204c7 (diff)
downloadperlweeklychallenge-club-87d45d0ee52acc6701981beea4e15b6432d54c22.tar.gz
perlweeklychallenge-club-87d45d0ee52acc6701981beea4e15b6432d54c22.tar.bz2
perlweeklychallenge-club-87d45d0ee52acc6701981beea4e15b6432d54c22.zip
Add Perl solution to challenge 113
Diffstat (limited to 'challenge-113')
-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
5 files changed, 102 insertions, 0 deletions
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';