diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-06 05:56:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-06 05:56:30 +0100 |
| commit | 5c9459a6c12722c1b6ce2b45471f95b3c0ced092 (patch) | |
| tree | d61a21b3c6fa4bd0ab0b39fdf8cab8c6ba8429eb | |
| parent | ee89dab0c2052f1a3ae6da3f550cf7f9dfc53583 (diff) | |
| parent | 97f78e0de95ecd7aa5e914ef010da023ba647304 (diff) | |
| download | perlweeklychallenge-club-5c9459a6c12722c1b6ce2b45471f95b3c0ced092.tar.gz perlweeklychallenge-club-5c9459a6c12722c1b6ce2b45471f95b3c0ced092.tar.bz2 perlweeklychallenge-club-5c9459a6c12722c1b6ce2b45471f95b3c0ced092.zip | |
Merge pull request #2453 from simongreen-net/swg-081
sgreen's solutions to challenge 081
| -rw-r--r-- | challenge-081/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-081/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-081/sgreen/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-081/sgreen/perl/ch-2.pl | 35 |
4 files changed, 80 insertions, 2 deletions
diff --git a/challenge-081/sgreen/README.md b/challenge-081/sgreen/README.md index 520da30452..fde0cedfbd 100644 --- a/challenge-081/sgreen/README.md +++ b/challenge-081/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 080 +# The Weekly Challenge 081 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-080-2if0) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-081-1jje) diff --git a/challenge-081/sgreen/blog.txt b/challenge-081/sgreen/blog.txt new file mode 100644 index 0000000000..7dcc970f69 --- /dev/null +++ b/challenge-081/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-weekly-challenge-081-1jje diff --git a/challenge-081/sgreen/perl/ch-1.pl b/challenge-081/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e7d76b54c3 --- /dev/null +++ b/challenge-081/sgreen/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); + +use List::Util qw(min); + +sub _is_base_string { + my ( $string, $substring ) = @_; + + # It cannot be a base string if it does fit evenly + return 0 if length($string) % length($substring); + + # Return true if the string is the base string repeated multiple times + my $repeatations = length($string) / length($substring); + return $substring x $repeatations eq $string; + +} + +sub main { + my ( $string1, $string2 ) = @_; + + # Santiy check + die "Please enter two strings\n" unless $string1 and $string2; + + # We only need to count to the minimum length + my @base_strings = (); + my $length = min( length($string1), length($string2) ); + + foreach my $l ( 1 .. $length ) { + my $value = substr( $string1, 0, $l ); + push @base_strings, $value + if _is_base_string( $string1, $value ) + and _is_base_string( $string2, $value ); + + } + + say join ' ', @base_strings; +} + +main(@ARGV); diff --git a/challenge-081/sgreen/perl/ch-2.pl b/challenge-081/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..3117f4e3eb --- /dev/null +++ b/challenge-081/sgreen/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use List::Util qw(uniq); + +sub main { + my $file = shift; + + # Sanity check + die "File does not exist or is not readable\n" if !-f $file && !-r $file; + + # Read the file, and slurp it into $string + my $string = ''; + open( my $fh, '<', $file ) or die "Cannot read $!"; + while ( my $line = <$fh> ) { $string .= $line } + + # Remove ignored characters (from the task). Replace them with a + # space incase they are a word seperator + $string =~ s/(\.|"|\(|\)|,|'s|--)/ /mg; + + # Find the frequency of all the words + my %frequency = (); + foreach my $word ( split /\s+/, $string ) { $frequency{$word}++; } + + # Output the results + for my $freq ( sort { $a <=> $b } ( uniq values %frequency ) ) { + print $freq, ' ', + join( ' ', sort grep { $freq == $frequency{$_} } keys %frequency ), + "\n\n"; + } +} + +main(@ARGV); |
