diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-04-22 10:55:17 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-04-22 10:55:17 -0400 |
| commit | 2f1584c7fd61a21b0ea011763a8561ea64b716c9 (patch) | |
| tree | 75ae6c8b9c333a10610298b9ba0e4eb5dec805ff | |
| parent | 042b3ce8105163b5067fe998724497efa1544100 (diff) | |
| download | perlweeklychallenge-club-2f1584c7fd61a21b0ea011763a8561ea64b716c9.tar.gz perlweeklychallenge-club-2f1584c7fd61a21b0ea011763a8561ea64b716c9.tar.bz2 perlweeklychallenge-club-2f1584c7fd61a21b0ea011763a8561ea64b716c9.zip | |
Ancient Challenges
| -rw-r--r-- | challenge-018/zapwai/perl/ch-1.pl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-018/zapwai/perl/ch-1.pl b/challenge-018/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..d9aa2d1a54 --- /dev/null +++ b/challenge-018/zapwai/perl/ch-1.pl @@ -0,0 +1,46 @@ +use v5.38; +if (@ARGV < 2) { + say "Please provide two (or more) arguments."; + exit; +} + +sub gen_substr($word) { + my @subs; + my $current_length = 1; + my $a = -1; + do { + $a++; + if ($a + $current_length > length $word) { + $a = 0; + $current_length++; + } + push @subs, substr($word, $a, $current_length); + } while ($current_length < length $word); + return @subs; +} + +sub common(@list) { + my $ans; + my @subs; + for my $i (0 .. $#list) { + push @subs, gen_substr($list[$i]); + } + my @common; + for my $i (0 .. $#subs) { + for my $j (0 .. $#subs) { + next if ($i == $j); + push @common, $subs[$i] if ($subs[$i] eq $subs[$j]); + } + } + my $max = 0; + my $ans_word; + for my $word (@common) { + if (length $word > $max) { + $max = length $word; + $ans_word = $word; + } + } + return $ans_word; +} + +say common(@ARGV); |
