From 6750dc1c85e2796da0b31970e0a64d2585386ac2 Mon Sep 17 00:00:00 2001 From: Jose Luis Perez Date: Wed, 7 Oct 2020 12:26:47 +0200 Subject: base strings 1st iteration --- challenge-081/jluis/README | 1 + challenge-081/jluis/perl/ch-1.pl | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-081/jluis/README create mode 100644 challenge-081/jluis/perl/ch-1.pl 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; -- cgit