diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-10-05 11:05:51 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-10-07 21:17:25 +0200 |
| commit | dd2635a5d37c5a853c5400613eb63c5fc301539c (patch) | |
| tree | 97b93c3ad65ff62c817d88c17d75a72debd03445 | |
| parent | 45bc85340b1d0295a7ea66b20623f3f1fd20f1ad (diff) | |
| download | perlweeklychallenge-club-dd2635a5d37c5a853c5400613eb63c5fc301539c.tar.gz perlweeklychallenge-club-dd2635a5d37c5a853c5400613eb63c5fc301539c.tar.bz2 perlweeklychallenge-club-dd2635a5d37c5a853c5400613eb63c5fc301539c.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-081/jo-37/perl/ch-1.pl | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-081/jo-37/perl/ch-1.pl b/challenge-081/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..61941ab415 --- /dev/null +++ b/challenge-081/jo-37/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use Test2::V0; + +# Find common base strings in two given strings. +sub cbs { + + # Combine both strings by joining them with a newline. + # The strings must not contain newlines. + local $_ = shift . "\n" . shift; + + # Collect all common base strings. + # Note: "dot" does not match a newline here. + my @base; + m{ + ^ (.+?) \1*+ \n \1++ \z # capture base string for both + (?{push @base, $1}) # collect captured base string + (*FAIL) # force backtracking + }x; + + @base; +} + +is [cbs("abcdabcd", "abcdabcdabcdabcd")], ["abcd", "abcdabcd"], + "first example"; + +is [cbs("aaa", "aa")], ["a"], "second example"; + +is [cbs("abcabc", "abcdabcdabcd")], [], "no common base strings"; + +done_testing; |
