From f5ffc3743a0f12f5998564b9b1fb621e4ec1b689 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 11 Apr 2022 04:16:02 -0400 Subject: new file: challenge-160/mattneleigh/perl/ch-1.pl new file: challenge-160/mattneleigh/perl/ch-2.pl --- challenge-160/mattneleigh/perl/ch-1.pl | 62 +++++++++++++++++++++++ challenge-160/mattneleigh/perl/ch-2.pl | 89 ++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100755 challenge-160/mattneleigh/perl/ch-1.pl create mode 100755 challenge-160/mattneleigh/perl/ch-2.pl diff --git a/challenge-160/mattneleigh/perl/ch-1.pl b/challenge-160/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..a6d2212f24 --- /dev/null +++ b/challenge-160/mattneleigh/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @table = ( + [ "zero", 4 ], + [ "one", 3 ], + [ "two", 3 ], + [ "three", 5 ], + [ "four", 4 ], + [ "five", 4 ], + [ "six", 3 ], + [ "seven", 5 ], + [ "eight", 5 ], + [ "nine", 4 ] +); +my @numbers = ( 5, 7, 6 ); +my $number; + +print("\n"); +foreach $number (@numbers){ + unless(($number > -1) && ($number < 10)){ + warn("Invalid number $number\n\n"); + next; + } + + my $string = ""; + + print("Input: \$n = ", $number, "\n"); + print("Output: "); + until($number == 4){ + # This number isn't four; add its count details + # and advance to the next number + $string .= + $table[$number][0] . + " is " . + $table[$table[$number][1]][0] . + ", "; + $number = $table[$number][1]; + } + + # We must have hit four, so add the magical + # sentiment, upper-case what's first, and + # output... + $string .= "four is magic."; + $string =~ s/^(.)/uc($1)/e; + print($string, "\n\n"); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + diff --git a/challenge-160/mattneleigh/perl/ch-2.pl b/challenge-160/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..11d5868909 --- /dev/null +++ b/challenge-160/mattneleigh/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @arrays = ( + [1, 3, 5, 7, 9], + [1, 2, 3, 4, 5], + [2, 4, 2] +); +my $array; + +print("\n"); +foreach $array (@arrays){ + my $rval = calculate_equilibrium_index(@{$array}); + + printf( + "Input: \@n = (%s)\nOutput: %s\n\n", + join(", ", @{$array}), + ( + $rval < 0 + ? + $rval . " as no Equilibrium Index could be found." + : + $rval + ) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the Equilibrium Index of an array of numbers- that is to say, the +# index at which the sum of the values behind (but not including) the index is +# equal to the sum of the values ahead (but not including) the index. There +# must be at least three elements in the array for this calculation to take +# place. +# Takes one argument: +# * The list to examine +# Returns: +# * The Equilibrium Index, or -1 if no Equilibrium Index can be found, which +# includes cases where the array contains fewer than three elements +################################################################################ +sub calculate_equilibrium_index{ + + # Can't have an equilibrium index if there + # aren't at least three elements + return(-1) + if(scalar(@ARG) < 3); + + my $fore = 0; + my $aft = $ARG[0]; + my $i = 1; + + # Compute the fore total + for(2 .. $#ARG){ + $fore += $ARG[$_]; + } + + # Loop from 1 to ($#ARG - 1) + while($i < $#ARG){ + if($aft == $fore){ + # Found an equilibrium index- return it + return($i); + } + + # Advance the index, adjusting the fore and + # aft totals + $aft += $ARG[$i]; + $fore -= $ARG[++$i]; + } + + # No equilibrium index found + return(-1); + +} + + + -- cgit