aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-081/jluis/README1
-rwxr-xr-xchallenge-081/jluis/perl/ch-1.pl70
-rwxr-xr-xchallenge-081/jluis/perl/ch-2.pl30
3 files changed, 101 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 100755
index 0000000000..0563d11964
--- /dev/null
+++ b/challenge-081/jluis/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.010;
+
+if ($#ARGV != 1) {
+ say <<USAGE;
+
+ Returns the comon base strigs
+ Base strings are those that can generte the original by repetition
+
+ ch-1.pl "aaaa" "aaaaaaaaaaaa"
+
+ returns ("a","aa","aa") the commom bases for the two strings
+USAGE
+ exit;
+}
+my ($A,$B) = @ARGV;
+
+say format_list( common_base($A,$B) );
+
+sub base {
+ # bassed on Abigail'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;
+ #Both arrays are ordered by the length of its strings
+ 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;
+}
diff --git a/challenge-081/jluis/perl/ch-2.pl b/challenge-081/jluis/perl/ch-2.pl
new file mode 100755
index 0000000000..a06a666220
--- /dev/null
+++ b/challenge-081/jluis/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use 5.010;
+say(<<USAGE) and exit if @ARGV;
+finds the frequency of all the words of input
+
+ch-2.pl must be called without any parameters it operates on a file caled input on the current dir
+
+USAGE
+
+open(my $input,'<','input') or die "Can't open input: $!";
+
+my %freq;
+while(<$input>){
+ chomp;
+ s/\.|"|\(|\)|,|'s|--/ /g;
+ while (/(\w+)/g) {
+ $freq{$1} = 0 unless defined $freq{$1};
+ $freq{$1} += 1;
+ }
+}
+
+my @results;
+for my $key (sort keys %freq) {
+ $results[$freq{$key}] = "$freq{$key}" unless defined $results[$freq{$key}];
+ $results[$freq{$key}] .= " $key";
+}
+
+defined($_) and say for @results;