From 2e71082f00258d3580f667788369a70e2fbeeec2 Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Fri, 15 Apr 2022 14:33:29 +0200 Subject: Add solutions for challenge 160 --- challenge-160/alexander-pankoff/perl/ch-1.pl | 67 ++++++++++++++++++++++++++++ challenge-160/alexander-pankoff/perl/ch-2.pl | 57 +++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100755 challenge-160/alexander-pankoff/perl/ch-1.pl create mode 100755 challenge-160/alexander-pankoff/perl/ch-2.pl 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; +} -- cgit From 6396477b5b4b2b421e65128bd0e2b1d4d26b0eaf Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Fri, 15 Apr 2022 16:12:37 +0200 Subject: Remove debug output --- challenge-160/alexander-pankoff/perl/ch-2.pl | 3 --- 1 file changed, 3 deletions(-) diff --git a/challenge-160/alexander-pankoff/perl/ch-2.pl b/challenge-160/alexander-pankoff/perl/ch-2.pl index a444a887e4..782ab8efdb 100755 --- a/challenge-160/alexander-pankoff/perl/ch-2.pl +++ b/challenge-160/alexander-pankoff/perl/ch-2.pl @@ -47,9 +47,6 @@ 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; } -- cgit From ed24d856536a42be292b276c84065ce247563e46 Mon Sep 17 00:00:00 2001 From: Alexander Pankoff Date: Fri, 15 Apr 2022 16:38:54 +0200 Subject: Add Blog posts --- challenge-160/alexander-pankoff/blog1.txt | 1 + challenge-160/alexander-pankoff/blog2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-160/alexander-pankoff/blog1.txt create mode 100644 challenge-160/alexander-pankoff/blog2.txt diff --git a/challenge-160/alexander-pankoff/blog1.txt b/challenge-160/alexander-pankoff/blog1.txt new file mode 100644 index 0000000000..15a4df01cf --- /dev/null +++ b/challenge-160/alexander-pankoff/blog1.txt @@ -0,0 +1 @@ +https://pankoff.net/pages/perl-weekly-challenge/challenge-160-task-1.html \ No newline at end of file diff --git a/challenge-160/alexander-pankoff/blog2.txt b/challenge-160/alexander-pankoff/blog2.txt new file mode 100644 index 0000000000..0b5e283008 --- /dev/null +++ b/challenge-160/alexander-pankoff/blog2.txt @@ -0,0 +1 @@ +https://pankoff.net/pages/perl-weekly-challenge/challenge-160-task-2.html \ No newline at end of file -- cgit