diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-07 22:01:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-07 22:01:50 +0100 |
| commit | e279d5b23bd044b6da94994dee3820943949f63e (patch) | |
| tree | 1243ec1e536d0ebc54cc153bd50c7d109984e414 | |
| parent | f11a864d14cf1a1609ad606bf883b0d5e3b854a6 (diff) | |
| parent | c65c694f21ce314c2a47e9a0bf5b4d3a0c993cb4 (diff) | |
| download | perlweeklychallenge-club-e279d5b23bd044b6da94994dee3820943949f63e.tar.gz perlweeklychallenge-club-e279d5b23bd044b6da94994dee3820943949f63e.tar.bz2 perlweeklychallenge-club-e279d5b23bd044b6da94994dee3820943949f63e.zip | |
Merge pull request #2473 from jluis/jluis81
My Challenge 081 solutions
| -rw-r--r-- | challenge-081/jluis/README | 1 | ||||
| -rwxr-xr-x | challenge-081/jluis/perl/ch-1.pl | 70 | ||||
| -rwxr-xr-x | challenge-081/jluis/perl/ch-2.pl | 30 |
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; |
