aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-04-15 19:54:30 +0100
committerGitHub <noreply@github.com>2022-04-15 19:54:30 +0100
commitd1b742fa08ae54086a1bec537807732bd1223201 (patch)
tree0e187580349fab35d7ee15e0fb085084aaed12a6
parent3395c4cd3ea74f3c4505dbf500fdadcbd263049d (diff)
parented24d856536a42be292b276c84065ce247563e46 (diff)
downloadperlweeklychallenge-club-d1b742fa08ae54086a1bec537807732bd1223201.tar.gz
perlweeklychallenge-club-d1b742fa08ae54086a1bec537807732bd1223201.tar.bz2
perlweeklychallenge-club-d1b742fa08ae54086a1bec537807732bd1223201.zip
Merge pull request #5943 from ccntrq/challenge-160
Add solutions for challenge 160
-rw-r--r--challenge-160/alexander-pankoff/blog1.txt1
-rw-r--r--challenge-160/alexander-pankoff/blog2.txt1
-rwxr-xr-xchallenge-160/alexander-pankoff/perl/ch-1.pl67
-rwxr-xr-xchallenge-160/alexander-pankoff/perl/ch-2.pl54
4 files changed, 123 insertions, 0 deletions
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
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..782ab8efdb
--- /dev/null
+++ b/challenge-160/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/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 ] );
+ return $i if $lower == $upper;
+ }
+
+ return -1;
+}