aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-182/dave-jacoby/perl/ch-1.pl25
-rw-r--r--challenge-182/dave-jacoby/perl/ch-2.pl31
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-182/dave-jacoby/perl/ch-1.pl b/challenge-182/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..c9f6bca26a
--- /dev/null
+++ b/challenge-182/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+use List::Util qw{ max };
+my @inputs = ( [ 5, 2, 9, 1, 7, 6 ], [ 4, 2, 3, 1, 5, 0 ] );
+
+for my $n (@inputs) {
+ my $input = join ' ', $n->@*;
+ my $max_index = max_index( $n->@* );
+ say <<"END";
+ INPUT: \@n = ($input)
+ OUTPUT: $max_index
+END
+}
+
+sub max_index ( @array ) {
+ my $max = max @array;
+ for my $i ( 0 .. -1 + scalar @array ) {
+ return $i if $max == $array[$i];
+ }
+ return -1;
+}
diff --git a/challenge-182/dave-jacoby/perl/ch-2.pl b/challenge-182/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..6ea0f25ef5
--- /dev/null
+++ b/challenge-182/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @paths = qw(
+ /a/b/c/1/x.pl
+ /a/b/c/d/e/2/x.pl
+ /a/b/c/d/3/x.pl
+ /a/b/c/4/x.pl
+ /a/b/c/d/5/x.pl
+);
+
+my $common_path = get_common_path(@paths);
+say $common_path;
+
+sub get_common_path( @paths) {
+ my @first = split /\//mx, $paths[0];
+ my $d = scalar @paths;
+ my $last_path = '/';
+ for my $i ( 0 .. -1 + scalar @first ) {
+ my $p = join '/', @first[ 0 .. $i ];
+ my $c = scalar grep { m{^$p}mx } @paths;
+ if ( $c !~ $d ) {
+ return $last_path;
+ }
+ $last_path = $p;
+ }
+
+}