From 8968a5ff21e13aecd34c6a0697358cfe1fd30a4d Mon Sep 17 00:00:00 2001 From: boblied Date: Tue, 6 Oct 2020 07:26:56 -0500 Subject: Set up challenge 081 --- .../bob-lied/perl/lib/CommonBaseString.pm | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-081/bob-lied/perl/lib/CommonBaseString.pm (limited to 'challenge-081/bob-lied/perl/lib/CommonBaseString.pm') diff --git a/challenge-081/bob-lied/perl/lib/CommonBaseString.pm b/challenge-081/bob-lied/perl/lib/CommonBaseString.pm new file mode 100644 index 0000000000..b67ecfa948 --- /dev/null +++ b/challenge-081/bob-lied/perl/lib/CommonBaseString.pm @@ -0,0 +1,39 @@ +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# CommonBaseString.pm +#============================================================================= +# Copyright (c) 2020, Bob Lied +#============================================================================= +# Description: +#============================================================================= + +package CommonBaseString; + +use strict; +use warnings; +use v5.30; + +use feature qw/ signatures /; +no warnings qw/ experimental::signatures /; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(); +our @EXPORT_OK = qw(); + +sub new($class, $name1) +{ + $class = ref($class) || $class; + my $self = { + _name1 => $name1, + }; + bless $self, $class; + return $self; +} + +sub run($self) +{ + return undef; +} + +1; -- cgit From d0579dfc12bbc9d3878bd2aab8525e9b3140824b Mon Sep 17 00:00:00 2001 From: boblied Date: Fri, 9 Oct 2020 15:34:05 -0500 Subject: Solutions for PWC 081 --- .../bob-lied/perl/lib/CommonBaseString.pm | 48 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'challenge-081/bob-lied/perl/lib/CommonBaseString.pm') diff --git a/challenge-081/bob-lied/perl/lib/CommonBaseString.pm b/challenge-081/bob-lied/perl/lib/CommonBaseString.pm index b67ecfa948..4c6f98901c 100644 --- a/challenge-081/bob-lied/perl/lib/CommonBaseString.pm +++ b/challenge-081/bob-lied/perl/lib/CommonBaseString.pm @@ -19,21 +19,61 @@ no warnings qw/ experimental::signatures /; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(); -our @EXPORT_OK = qw(); +our @EXPORT_OK = qw(commonPrefix); -sub new($class, $name1) +sub new($class, $a, $b) { $class = ref($class) || $class; my $self = { - _name1 => $name1, + _a => $a, + _b => $b, }; bless $self, $class; return $self; } +# +sub commonPrefix($s, $t) +{ + my @s1 = split("", $s); + my @s2 = split("", $t); + my $prefix = ""; + my @possiblePrefixes = (); + + while ( @s1 && @s2 && ($s = shift @s1) eq ($t = shift @s2) ) + { + $prefix .= $s; + push @possiblePrefixes, $prefix; + } + return \@possiblePrefixes; +} + sub run($self) { - return undef; + my ($A, $B) = @{$self}{qw(_a _b)}; + my ($lenA, $lenB) = ( length($A), length($B) ); + my @thisWorks = (); + + my $prefixes = commonPrefix($A, $B); + + for my $prefix ( @$prefixes ) + { + my $lenP = length($prefix); + # Only prefixes that can be repeated to the length of both strings + # are candidates. + next unless $lenA % $lenP == 0; + next unless $lenB % $lenP == 0; + + # Number of repetitions required for each string. + my $repA = $lenA / $lenP; + my $repB = $lenB / $lenP; + + my $buildsA = ( ($prefix x $repA) eq $A ); + my $buildsB = ( ($prefix x $repB) eq $B ); + push @thisWorks, $prefix if ( $buildsA && $buildsB ); + } + + return \@thisWorks; } 1; -- cgit