diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 14:22:27 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 14:22:27 +0100 |
| commit | 4da3eb91ec0f729447e5d0819168eb411bff2cd1 (patch) | |
| tree | eb1408df06fccf1fe230b62203c29a4ca3103d20 | |
| parent | 7af11923bf07ca9f2d20243ee3649651c06378e6 (diff) | |
| download | perlweeklychallenge-club-4da3eb91ec0f729447e5d0819168eb411bff2cd1.tar.gz perlweeklychallenge-club-4da3eb91ec0f729447e5d0819168eb411bff2cd1.tar.bz2 perlweeklychallenge-club-4da3eb91ec0f729447e5d0819168eb411bff2cd1.zip | |
Add Perl solution to challenge 160
| -rw-r--r-- | challenge-160/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-160/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-160/paulo-custodio/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-160/paulo-custodio/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-160/paulo-custodio/t/test-1.yaml | 20 | ||||
| -rw-r--r-- | challenge-160/paulo-custodio/t/test-2.yaml | 15 |
6 files changed, 127 insertions, 0 deletions
diff --git a/challenge-160/paulo-custodio/Makefile b/challenge-160/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-160/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-160/paulo-custodio/README b/challenge-160/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-160/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-160/paulo-custodio/perl/ch-1.pl b/challenge-160/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..958713a46a --- /dev/null +++ b/challenge-160/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Challenge 160 +# +# TASK #1 Four Is Magic +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n < 10. +# +# Write a script to generate english text sequence starting with the English +# cardinal representation of the given number, the word is and then the +# English cardinal representation of the count of characters that made up the +# first word, followed by a comma. Continue until you reach four. +# +# +# Example 1: +# Input: $n = 5 +# Output: Five is four, four is magic. +# +# Example 2: +# Input: $n = 7 +# Output: Seven is five, five is four, four is magic. +# +# Example 3: +# Input: $n = 6 +# Output: Six is three, three is five, five is four, four is magic. + +use Modern::Perl; +use Lingua::EN::Numbers qw(num2en); + +say sequence(shift||1); + +sub sequence { + my($n) = @_; + my @out; + while ($n != 4) { + my $num_en = num2en($n); + my $len = length($num_en); + my $len_en = num2en($len); + + push @out, "$num_en is $len_en"; + $n = $len; + } + + push @out, "four is magic."; + $out[0] =~ s/(\w)/\U$1/; + return join(", ", @out); +} diff --git a/challenge-160/paulo-custodio/perl/ch-2.pl b/challenge-160/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..9036422a55 --- /dev/null +++ b/challenge-160/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# Challenge 160 +# +# TASK #2 Equilibrium Index +# Submitted by: Mohammad S Anwar +# You are give an array of integers, @n. +# +# Write a script to find out the Equilibrium Index of the given array, if found. +# +# For an array A consisting n elements, index i is an equilibrium index if the +# sum of elements of subarray A[0
i-1] is equal to the sum of elements of +# subarray A[i+1
n-1]. +# +# +# Example 1: +# Input: @n = (1, 3, 5, 7, 9) +# Output: 3 +# +# Example 2: +# Input: @n = (1, 2, 3, 4, 5) +# Output: -1 as no Equilibrium Index found. +# +# Example 3: +# Input: @n = (2, 4, 2) +# Output: 1 + +use Modern::Perl; +use List::Util qw( sum ); + +say equilibrium_index(@ARGV); + +sub equilibrium_index { + my(@n) = @_; + for my $i (1 .. $#n-1) { + my $left = sum(@n[0..$i-1]); + my $right = sum(@n[$i+1..$#n]); + return $i if $left==$right; + return -1 if $left>$right; + } + return -1; +} diff --git a/challenge-160/paulo-custodio/t/test-1.yaml b/challenge-160/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..81e73fbe64 --- /dev/null +++ b/challenge-160/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 5 + input: + output: Five is four, four is magic. +- setup: + cleanup: + args: 6 + input: + output: Six is three, three is five, five is four, four is magic. +- setup: + cleanup: + args: 7 + input: + output: Seven is five, five is four, four is magic. +- setup: + cleanup: + args: 10 + input: + output: Ten is three, three is five, five is four, four is magic. diff --git a/challenge-160/paulo-custodio/t/test-2.yaml b/challenge-160/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..41372351ac --- /dev/null +++ b/challenge-160/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 3 5 7 9 + input: + output: 3 +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: -1 +- setup: + cleanup: + args: 2 4 2 + input: + output: 1 |
