diff options
| -rw-r--r-- | challenge-261/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-261/paulo-custodio/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-261/paulo-custodio/perl/ch-2.pl | 56 | ||||
| -rw-r--r-- | challenge-261/paulo-custodio/t/test-1.yaml | 20 | ||||
| -rw-r--r-- | challenge-261/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 142 insertions, 0 deletions
diff --git a/challenge-261/paulo-custodio/Makefile b/challenge-261/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-261/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-261/paulo-custodio/perl/ch-1.pl b/challenge-261/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..aaec18f3ed --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 1: Element Digit Sum +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 + +use Modern::Perl; +use List::Util 'sum'; + +my @ints = @ARGV; +my @digits = split //, join '', @ints; +say abs(sum(@ints) - sum(@digits)); diff --git a/challenge-261/paulo-custodio/perl/ch-2.pl b/challenge-261/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..289fc37213 --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 2: Multiply by Two +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. + +use Modern::Perl; + +my($start, @ints) = @ARGV; +say mult_two($start, @ints); + +sub mult_two { + my($n, @ints) = @_; + while (grep {$_ == $n} @ints) { + $n *= 2; + } + return $n; +} diff --git a/challenge-261/paulo-custodio/t/test-1.yaml b/challenge-261/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..26fe36e663 --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 2 3 45 + input: + output: 36 +- setup: + cleanup: + args: 1 12 3 + input: + output: 9 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: 0 +- setup: + cleanup: + args: 236 416 336 350 + input: + output: 1296 diff --git a/challenge-261/paulo-custodio/t/test-2.yaml b/challenge-261/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b1d5ba5d6c --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 3 5 3 6 1 12 + input: + output: 24 +- setup: + cleanup: + args: 1 1 2 4 3 + input: + output: 8 +- setup: + cleanup: + args: 2 5 6 7 + input: + output: 2 |
