From 035b63413959f8a7218f0595751b14dd7d26a3b4 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 00:07:11 -0600 Subject: Update ch-1.pl Iterate indices instead of substrings. --- challenge-018/jaime/perl5/ch-1.pl | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl index ce9415e8c2..eeb61845eb 100644 --- a/challenge-018/jaime/perl5/ch-1.pl +++ b/challenge-018/jaime/perl5/ch-1.pl @@ -4,18 +4,10 @@ # Write a script that takes 2 or more strings as command line parameters # and print the longest common substring. -sub of_size { - my ($n,$s) = @_; - my @subs = (); - for (my $i = 0; ($i+$n) <= length $s; $i++) { - push @subs, substr($s,$i,$n); - } - return @subs; #substrings of $s of length $n. -} - my $head = shift; for my $n (reverse 1..(length $head)) { - for my $s (of_size($n,$head)) { + for my $i (0..((length $head)-$n)) { + my $s = substr($head,$i,$n); if (@ARGV == grep(/$s/,@ARGV)) { print "$s\n"; exit; -- cgit