diff options
| author | Shawn <shawnw.mobile@gmail.com> | 2020-10-06 02:16:28 -0700 |
|---|---|---|
| committer | Shawn <shawnw.mobile@gmail.com> | 2020-10-06 02:26:35 -0700 |
| commit | 27cd79d114db989658262ab8f98a494b83219aad (patch) | |
| tree | 772c6dc2d6b28d4a91ddaf4f118745b319fb8f58 | |
| parent | 89a288199348fb289086328e6b49d2151216cc6a (diff) | |
| download | perlweeklychallenge-club-27cd79d114db989658262ab8f98a494b83219aad.tar.gz perlweeklychallenge-club-27cd79d114db989658262ab8f98a494b83219aad.tar.bz2 perlweeklychallenge-club-27cd79d114db989658262ab8f98a494b83219aad.zip | |
Challenge 081, both parts in perl
| -rwxr-xr-x | challenge-081/shawn-wagner/perl/ch1.pl | 32 | ||||
| -rwxr-xr-x | challenge-081/shawn-wagner/perl/ch2.pl | 27 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-081/shawn-wagner/perl/ch1.pl b/challenge-081/shawn-wagner/perl/ch1.pl new file mode 100755 index 0000000000..cc1b41e5eb --- /dev/null +++ b/challenge-081/shawn-wagner/perl/ch1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use warnings; +use strict; +use feature qw/say/; + +sub substrings :prototype($) { + my $s = shift; + my $len = length $s; + my %subs; + for my $start (0 .. $len - 1) { + for my $slen (1 .. $len - $start) { + $subs{substr $s, $start, $slen} = 1; + } + } + return keys %subs; +} + +sub task1 :prototype($$) { + my ($A, $B) = @_; + my (@common, %subs); + for (substrings($A), substrings($B)) { + $subs{$_} += 1; + } + while (my ($substr, $count) = each %subs) { + next if $count != 2; + push @common, $substr if ($A =~ /^(?:$substr)+$/ && $B =~ /^(?:$substr)+$/); + } + say join(" ", sort @common); +} + +task1 "abcdabcd", "abcdabcdabcdabcd"; +task1 "aaa", "aa"; diff --git a/challenge-081/shawn-wagner/perl/ch2.pl b/challenge-081/shawn-wagner/perl/ch2.pl new file mode 100755 index 0000000000..ca65fa440a --- /dev/null +++ b/challenge-081/shawn-wagner/perl/ch2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use warnings; +use strict; +use feature qw/say/; +use experimental qw/postderef/; + +my %words; + +open my $file, "<", "input" or die "Unable to open input: $!\n"; +while (<$file>) { + chomp; + s/[."(),]|--|'s//g; + for my $word (split /\s+/, $_) { + $words{$word}++; + } +} +close $file; + +my %frequencies; +while (my ($word, $count) = each %words) { + push @{$frequencies{$count}}, $word; +} + +for my $count (sort { $a <=> $b } keys %frequencies) { + say $count, " ", join(" ", sort $frequencies{$count}->@*); +} + |
