From 87d45d0ee52acc6701981beea4e15b6432d54c22 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 15 Jun 2021 23:10:58 +0100 Subject: Add Perl solution to challenge 113 --- challenge-113/paulo-custodio/perl/ch-1.pl | 31 ++++++++++++++++++++++++++ challenge-113/paulo-custodio/perl/ch-2.pl | 35 ++++++++++++++++++++++++++++++ challenge-113/paulo-custodio/t/test-1.yaml | 10 +++++++++ challenge-113/paulo-custodio/t/test-2.yaml | 19 ++++++++++++++++ challenge-113/paulo-custodio/test.pl | 7 ++++++ 5 files changed, 102 insertions(+) create mode 100644 challenge-113/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-113/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-113/paulo-custodio/t/test-1.yaml create mode 100644 challenge-113/paulo-custodio/t/test-2.yaml create mode 100755 challenge-113/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'; -- cgit