diff options
| -rw-r--r-- | challenge-161/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-161/wlmb/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-161/wlmb/perl/ch-2.pl | 29 |
3 files changed, 53 insertions, 0 deletions
diff --git a/challenge-161/wlmb/blog.txt b/challenge-161/wlmb/blog.txt new file mode 100644 index 0000000000..ab0d81b49b --- /dev/null +++ b/challenge-161/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2022/04/18/PWC161/ diff --git a/challenge-161/wlmb/perl/ch-1.pl b/challenge-161/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..1fb5e9a443 --- /dev/null +++ b/challenge-161/wlmb/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +# Perl weekly challenge 161 +# Task 1: Abecedarian words +# +# See https://wlmb.github.io/2022/04/11/PWC160/#task-1-abecedarian-words +use v5.12; +use warnings; +use Text::Wrap qw(wrap $columns $break); +die "Usage: ./ch-1.pl dictionary\n". + "to find all abecedarian words in the given dictionary..." + unless @ARGV==1; +die "Can't read dictionary" unless -r $ARGV[0]; +$columns=62; $break=qr/\s/; +# Read the comments from the bottom up +say wrap "", "", # pretty print + join " ", # join into line + sort {length $b <=> length $a} # sort by decreasing length + grep {$_ eq # compare to original word and filter + join "", # join into new word + sort {$a cmp $b} # sort them + split "", $_} # separate letters + map {chomp; $_} # Remove line end + <>; # Read all the dictionary, one word per line diff --git a/challenge-161/wlmb/perl/ch-2.pl b/challenge-161/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..857856c79f --- /dev/null +++ b/challenge-161/wlmb/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +# Perl weekly challenge 161 +# Task 2: Pangrams +# +# See https://wlmb.github.io/2022/04/11/PWC160/#task-2-pangrams +use v5.12; +use warnings; +use List::Util qw(notall uniq shuffle first); +die "Usage: ./ch-2.pl dictionary\n". + "to find an abedecedarian pangram from the given dictionary..." + unless @ARGV==1; +die "Can't read dictionary" unless -r $ARGV[0]; +my @dictionary=map {chomp; $_} <>; # read the dictionary +my @abecedarian=grep {$_ eq join "", sort {$a cmp $b} split "", $_} @dictionary; +my %words_with; # maps letters to words containing them +for my $word(@abecedarian){ + push @{$words_with{$_}},$word for uniq split "", $word; +} +my %letters; +my $pangram; +my @all_letters='a'..'z'; +while(notall {$letters{$_}} @all_letters){ + my $letter=first {!$letters{$_}} shuffle @all_letters; # random missing letter + my @possible_words=@{$words_with{$letter}}; # words containing letter + my $word=$possible_words[rand @possible_words]; # choose a random one + $pangram.=" ".$word; # add to output phrase + ++$letters{$_} for split "", $word; # update used letters +} +say $pangram; |
