aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-02 19:17:35 +0100
committerGitHub <noreply@github.com>2022-10-02 19:17:35 +0100
commit618ba3775cd9f78f3179b757f36dfc2a91d313fd (patch)
tree968de982e114add05ce1fbc62416be6984b334e4
parenta3f3c09e91254a4366584b485dd100430bfbe5a5 (diff)
parent8b9f6a3a518794ff39bee52757604b3630e231f0 (diff)
downloadperlweeklychallenge-club-618ba3775cd9f78f3179b757f36dfc2a91d313fd.tar.gz
perlweeklychallenge-club-618ba3775cd9f78f3179b757f36dfc2a91d313fd.tar.bz2
perlweeklychallenge-club-618ba3775cd9f78f3179b757f36dfc2a91d313fd.zip
Merge pull request #6824 from jacoby/master
Challenge 182
-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;
+ }
+
+}