aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-12 16:06:15 +0100
committerGitHub <noreply@github.com>2019-06-12 16:06:15 +0100
commit18063aa888595a2613e8ae465c19d2bd4e0556dd (patch)
tree03e5ccb5ece37ed974f28ee3ef505500e7d2d5fc
parent5dcfe623ac277f0f09e02089046f62d393ec88d2 (diff)
parentaf3a15271271e36871832253ffb3e44b1442bfb3 (diff)
downloadperlweeklychallenge-club-18063aa888595a2613e8ae465c19d2bd4e0556dd.tar.gz
perlweeklychallenge-club-18063aa888595a2613e8ae465c19d2bd4e0556dd.tar.bz2
perlweeklychallenge-club-18063aa888595a2613e8ae465c19d2bd4e0556dd.zip
Merge pull request #245 from jacoby/c12
Annotated Challenge #2
-rwxr-xr-xchallenge-012/dave-jacoby/c2.pl61
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-012/dave-jacoby/c2.pl b/challenge-012/dave-jacoby/c2.pl
new file mode 100755
index 0000000000..05d5b199a8
--- /dev/null
+++ b/challenge-012/dave-jacoby/c2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw{ postderef say state };
+no warnings qw{ experimental::postderef };
+
+my @paths;
+push @paths, '/a/b/c/d';
+push @paths, '/a/b/cd';
+push @paths, '/a/b/cc';
+push @paths, '/a/b/c/d/e';
+
+# should allow user input and perhaps pull into a subroutine
+# eh, exercise for a reader
+
+# before: ["/a/b/c/d", "/a/b/cd"]
+
+my @p2 = map { [ split m{/} ] } grep { m{^/} } @paths;
+
+# after: [["","a","b","c","d"],["","a","b","c"]]
+
+# while loops are worrying, but this should never go farther than the
+# shortest path in the list
+while (1) {
+ state $c = 0;
+ my $d = 0;
+
+ for my $p (@p2) {
+
+ # first pass, I had separate cases for "end of path"
+ # and "paths separate", but then I stepped back and
+ # applied more clever
+
+ # I'm checking the nth (or, rather, cth) position in each
+ # array with the same position of the first array, and also
+ # if that's defined. In the case where you have one or more
+ # paths like /a/b/c/d/e/f/g, the answer will be /a/b/c/d/e/f/g
+
+ if ( !defined $p2[$d][$c] || $p2[0][$c] ne $p2[$d][$c] ) {
+
+ # say join '/', $p2[0][ 0 .. $c - 1];
+ # Use of uninitialized value $. in range (or flip) at ./c2.pl line 43.
+ # which is weird. I guess you cannot get slices of postdereferenced
+ # arrayrefs. Who knew? So we use map instead.
+
+ # and, since there's a problem with $c, we go to $c - 1
+
+ say join '/', map { $p2[0][$_] } 0 .. $c - 1;
+ exit;
+ }
+ $d++;
+ }
+ $c++;
+}
+
+# /a/b
+
+exit;
+