diff options
| -rw-r--r-- | challenge-160/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-160/dave-jacoby/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-160/dave-jacoby/perl/ch-2.pl | 35 |
3 files changed, 63 insertions, 0 deletions
diff --git a/challenge-160/dave-jacoby/blog.txt b/challenge-160/dave-jacoby/blog.txt new file mode 100644 index 0000000000..0d02dfb42d --- /dev/null +++ b/challenge-160/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2022/04/12/does-that-make-friendship-equal-four-weekly-challenge-160.html diff --git a/challenge-160/dave-jacoby/perl/ch-1.pl b/challenge-160/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..dc08362edb --- /dev/null +++ b/challenge-160/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use Carp; +use Getopt::Long; +use Lingua::EN::Numbers qw(num2en); + +my $n = 6; +GetOptions( 'number=i' => \$n, ); +croak 'Out of range' if $n < 1; + +say magic($n); + +sub magic ( $i ) { + return 'four is magic.' if $i == 4; + my $w = num2en($i); + my $c = () = $w =~ /(\w)/gmix; + my $d = num2en($c); + my $out = qq{$w is $d, }; + $out .= magic($c); + return ucfirst lc $out; +} + diff --git a/challenge-160/dave-jacoby/perl/ch-2.pl b/challenge-160/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..4543ed01dc --- /dev/null +++ b/challenge-160/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ sum0 }; + +my @examples; +push @examples, [ 1, 3, 5, 7, 9 ]; +push @examples, [ 1, 2, 3, 4, 5 ]; +push @examples, [ 2, 4, 2 ]; + +for my $e (@examples) { + my $i = equilibrium_index( $e->@* ); + my $ee = join ', ', $e->@*; + + say <<"END"; + Input: \@n = $ee + Output: $i +END +} + +sub equilibrium_index ( @array ) { + + for my $i ( 1 .. $#array - 1 ) { + my @s1 = @array[ 0 .. $i - 1 ]; + my @s2 = @array[ $i + 1 .. $#array ]; + my $sum1 = sum0 @s1; + my $sum2 = sum0 @s2; + return $i if $sum1 == $sum2; + } + return -1; +} |
