diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-05-11 09:34:02 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-05-11 09:34:02 +0100 |
| commit | d202d9104bf9fb8f332005545dfe22b344da6b68 (patch) | |
| tree | 6a50940a4d7b07dc540fccc33617c372d926ba9e | |
| parent | 2e3ed7f3d442a7bc1854e005b55ce7c93ff1a053 (diff) | |
| download | perlweeklychallenge-club-d202d9104bf9fb8f332005545dfe22b344da6b68.tar.gz perlweeklychallenge-club-d202d9104bf9fb8f332005545dfe22b344da6b68.tar.bz2 perlweeklychallenge-club-d202d9104bf9fb8f332005545dfe22b344da6b68.zip | |
remove need for double nested loop - by implementing backtracking
| -rw-r--r-- | challenge-112/james-smith/perl/ch-1.pl | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/challenge-112/james-smith/perl/ch-1.pl b/challenge-112/james-smith/perl/ch-1.pl index b050bfd27c..33bad261ca 100644 --- a/challenge-112/james-smith/perl/ch-1.pl +++ b/challenge-112/james-smith/perl/ch-1.pl @@ -14,18 +14,14 @@ is( can_path('/a/b/../c/..'), '/a' ); is( can_path('/a/../b/../c/..'), '/' ); is( can_path('/a/../b/../c/..'), '/' ); is( can_path('/a/../b/../c/../../..'), '/' ); +is( can_path('/a/../b/../c/../../..'), '/' ); +is( can_path('/../../../a/'), '/a' ); done_testing(); sub can_path { - my $l = my @parts = grep { $_ ne '' && $_ ne '.' } split m{/}, $_[0]; - while(--$l>0) { - if($parts[$l] eq '..' && $parts[$l-1] ne '..' ) { - splice @parts, $l-1,2; - $l = @parts;# = @parts; - } - } - shift @parts while @parts && $parts[0] eq '..'; ## Remove any ## + my($p,@parts) = (1,grep { $_ ne '' && $_ ne '.' } split m{/}, $_[0]); + $parts[$p] ne '..' ? $p++ : $p>0 ? splice @parts,--$p,2 : shift @parts while $p < @parts; return '/'.join '/',@parts; } |
