diff options
Diffstat (limited to 'challenge-160')
| -rwxr-xr-x | challenge-160/alexander-pankoff/perl/ch-1.pl | 67 | ||||
| -rwxr-xr-x | challenge-160/alexander-pankoff/perl/ch-2.pl | 57 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-160/alexander-pankoff/perl/ch-1.pl b/challenge-160/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..0c8670bf09 --- /dev/null +++ b/challenge-160/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +# 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. + +run() unless caller(); + +sub run() { + my ($N) = @ARGV; + die "Expect a single digit positive number!\n" unless $N and $N =~ m/^\d$/; + say four_is_magic($N); +} + +sub four_is_magic($n) { + ucfirst _four_is_magic($n); + +} + +sub _four_is_magic($n) { + my %cardinal_representations = ( + 1 => 'one', + 2 => 'two', + 3 => 'three', + 4 => 'four', + 5 => 'five', + 6 => 'six', + 7 => 'seven', + 8 => 'eight', + 9 => 'nine', + ); + + if ( $n == 4 ) { + return 'four is magic.'; + } + + my $cardinal = $cardinal_representations{$n}; + my $length = length $cardinal; + + return join( ', ', + join( ' ', $cardinal, 'is', $cardinal_representations{$length} ), + _four_is_magic($length) ); +} diff --git a/challenge-160/alexander-pankoff/perl/ch-2.pl b/challenge-160/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..a444a887e4 --- /dev/null +++ b/challenge-160/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +# 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 List::Util qw(all sum0); +use Scalar::Util qw(looks_like_number); + +run() unless caller(); + +sub run() { + my @xs = @ARGV; + die "Expect a list of integers!\n" unless all { m/^-?\d+$/ } @xs; + + say equilibrium_index(@xs); +} + +sub equilibrium_index(@xs) { + for my $i ( 0 .. $#xs ) { + my $lower = sum0( @xs[ 0 .. $i - 1 ] ); + my $upper = sum0( @xs[ $i + 1 .. $#xs ] ); + say $i; + say $lower; + say $upper; + return $i if $lower == $upper; + } + + return -1; +} |
