aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Luis Perez <jluis@escomposlinux.org>2020-10-07 12:26:47 +0200
committerJose Luis Perez <jluis@escomposlinux.org>2020-10-07 12:26:47 +0200
commit6750dc1c85e2796da0b31970e0a64d2585386ac2 (patch)
tree3a3b7c96b39538c02d2109c9783af6fcd7a97113
parentcef248ba491398a30061ba49fbc2a824116ae996 (diff)
downloadperlweeklychallenge-club-6750dc1c85e2796da0b31970e0a64d2585386ac2.tar.gz
perlweeklychallenge-club-6750dc1c85e2796da0b31970e0a64d2585386ac2.tar.bz2
perlweeklychallenge-club-6750dc1c85e2796da0b31970e0a64d2585386ac2.zip
base strings 1st iteration
-rw-r--r--challenge-081/jluis/README1
-rw-r--r--challenge-081/jluis/perl/ch-1.pl53
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-081/jluis/README b/challenge-081/jluis/README
new file mode 100644
index 0000000000..ff80e1d3bd
--- /dev/null
+++ b/challenge-081/jluis/README
@@ -0,0 +1 @@
+Solution by jluis
diff --git a/challenge-081/jluis/perl/ch-1.pl b/challenge-081/jluis/perl/ch-1.pl
new file mode 100644
index 0000000000..8549a26895
--- /dev/null
+++ b/challenge-081/jluis/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+sub base {
+ # bassed on Abigil's prime number regex
+ # get all base strings of $_[0]
+ # a base string is one that concateneted 0 or more times can generate
+ # the original string
+ my $orig = shift;
+ my @bases;
+ my $length = 1;
+ while (1) {
+ last unless $orig =~ /^(.{$length,}?)\1+$/;
+ push @bases,$1;
+ $length = 1+length($1);
+ }
+ return (@bases,$orig)
+}
+
+sub format_list {
+ my $out = "(";
+ while (my $val = shift) {
+ $out .= '"'.$val.'"';
+ $out .= ',' if defined @_[0];
+ }
+ return "$out)";
+}
+
+sub common_base {
+ my @A = base(shift);
+ my @B = base(shift);
+ my @result;
+ my $AIndex = 0;
+ my $BIndex = 0;
+ while ($AIndex <= $#A and $BIndex <= $#B) {
+ if ($A[$AIndex] eq $B[$BIndex]) {
+ push @result,$A[$AIndex];
+ $AIndex += 1;
+ $BIndex += 1;
+ next;
+ }
+ last if length($A[$AIndex]) == length($B[$BIndex]);
+ if (length($A[$AIndex]) > length($B[$BIndex])) {
+ $BIndex += 1;
+ } else {
+ $AIndex += 1;
+ }
+ }
+ return @result;
+}
+CORE::say format_list common_base 'aa'x210,'a'x105;