diff options
| -rw-r--r-- | challenge-160/duncan-c-white/README | 55 | ||||
| -rwxr-xr-x | challenge-160/duncan-c-white/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-160/duncan-c-white/perl/ch-2.pl | 66 |
3 files changed, 154 insertions, 29 deletions
diff --git a/challenge-160/duncan-c-white/README b/challenge-160/duncan-c-white/README index c512a02d69..c84ca6bc89 100644 --- a/challenge-160/duncan-c-white/README +++ b/challenge-160/duncan-c-white/README @@ -1,58 +1,55 @@ -TASK #1 - Farey Sequence +TASK #1 - Four Is Magic -You are given a positive number, $n. +You are given a positive number, $n < 10. -Write a script to compute the Farey Sequence of the order $n, defined as: -is the sequence of completely reduced fractions, between 0 and 1, -which have numerators and denominators less than or equal to n, -arranged in order of increasing size). +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: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1. + Output: Five is four, four is magic. Example 2: Input: $n = 7 - Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, - 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1. + Output: Seven is five, five is four, four is magic. Example 3: - Input: $n = 4 - Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1. + Input: $n = 6 + Output: Six is three, three is five, five is four, four is magic. -MY NOTES: ok. Pretty easy. Will need a way to reduce fractions, and a way of -storing that reduced fraction in a set (storing "Num/Denom" should do it). +MY NOTES: ok. Pretty easy. -TASK #2 - Moebius Number +TASK #2 - Equilibrium Index -You are given a positive number $n. +You are give an array of integers, @n. -Write a script to generate the Moebius Number for the given number, -definition: For any positive integer n, define moeb(n) as: +Write a script to find out the Equilibrium Index of the given array, if found. - moeb(n) = +1 if n is a square-free positive integer with an even - number of prime factors. - moeb(n) = 1 if n is a square-free positive integer with an odd - number of prime factors. - moeb(n) = 0 if n has a squared prime factor. +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 = 5 - Output: -1 + Input: @n = (1, 3, 5, 7, 9) + Output: 3 Example 2: - Input: $n = 10 - Output: 1 + Input: @n = (1, 2, 3, 4, 5) + Output: -1 as no Equilibrium Index found. Example 3: - Input: $n = 20 - Output: 0 + Input: @n = (2, 4, 2) + Output: 1 -MY NOTES: ok. Slightly tricky. +MY NOTES: ok. Pretty easy. Rather than recomputing sums each time, +let's keep track of "the sum before i" and "the sum after i" and +adjust them each pass.. diff --git a/challenge-160/duncan-c-white/perl/ch-1.pl b/challenge-160/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..026fc1dd0a --- /dev/null +++ b/challenge-160/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# TASK #1 - Four Is Magic +# +# 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. +# +# MY NOTES: ok. Pretty easy. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use List::Util qw(sum0); +#use Data::Dumper; + +my $debug=0; +die "Usage: four-is-magic [--debug] [N] (default 3, less then 10)\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV<2; + +my $n = shift // 3; +die "four-is-magic: n ($n) must be < 10\n" if $n>9; +die "four-is-magic: n ($n) must be >= 0\n" if $n<0; + +my @n_to_word = qw(zero one two three four five six seven eight nine); + +my $word = $n_to_word[$n]; + +my @answer = (); + +while( $word ne "four" ) +{ + my $len = length($word); + my $lenword = $n_to_word[$len]; + push @answer, "$word is $lenword"; + say "debug: word=$word, len=$len, lenword=$lenword, added $word is $lenword" if $debug; + $word = $lenword; +} + +push @answer, "four is magic"; + +say join( ', ', @answer ); diff --git a/challenge-160/duncan-c-white/perl/ch-2.pl b/challenge-160/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..c7d56a2ecb --- /dev/null +++ b/challenge-160/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl +# +# TASK #2 - Equilibrium Index +# +# 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 +# +# MY NOTES: ok. Pretty easy. Rather than recomputing sums each time, +# let's keep track of "the sum before i" and "the sum after i" and +# adjust them each pass.. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use List::Util qw(sum0); +use Function::Parameters; +#use Data::Dumper; + +my $debug=0; +die "Usage: equilibrium-index [--debug] numeric values\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>2; + +my @n = @ARGV; +my $equiindex = equi_index( @n ); +say $equiindex; + +# +# my $equiindex = equi_index( @n ); +# Return the equi index (as defined above) of @n, +# if there is one, or -1 if there isn't. +# +fun equi_index( @n ) +{ + my $sumafter = sum0( @n ) - $n[0] - $n[1]; + my $sumbefore = $n[0]; + foreach my $i (1..$#n-1) + { + say "i=$i, sumbefore=$sumbefore, sumafter=$sumafter, add=$n[$i], sub=$n[$i+1]" if $debug; + return $i if $sumbefore == $sumafter; + $sumbefore += $n[$i]; + $sumafter -= $n[$i+1]; + } + return -1; +} |
