aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-28 19:16:49 +0100
committerGitHub <noreply@github.com>2019-06-28 19:16:49 +0100
commit5c8e007af38a211475dcf076f78a6aaeeec8c140 (patch)
tree97d9faa60eb8a7db8ee0b63682056e03c1d541f1
parent31f0d0a867468c37911902cef7d4196e6b478680 (diff)
parent9cdafdf8d2291198725131889932553d409740b1 (diff)
downloadperlweeklychallenge-club-5c8e007af38a211475dcf076f78a6aaeeec8c140.tar.gz
perlweeklychallenge-club-5c8e007af38a211475dcf076f78a6aaeeec8c140.tar.bz2
perlweeklychallenge-club-5c8e007af38a211475dcf076f78a6aaeeec8c140.zip
Merge pull request #312 from jmaslak/joelle-14-1-1
Solution for challenge 14.2 in P6 & P5
-rwxr-xr-xchallenge-014/joelle-maslak/perl5/ch-2.pl70
-rwxr-xr-xchallenge-014/joelle-maslak/perl6/ch-2.p641
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-014/joelle-maslak/perl5/ch-2.pl b/challenge-014/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..66df946dc6
--- /dev/null
+++ b/challenge-014/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+
+use List::Util qw(all any max);
+
+my (%states) = map { $_, 1 } qw(
+ AK AL AR AZ CA CO CT DE FL GA
+ HI IA ID IL IN KS LA MA MD ME
+ MI MO MN MS MT NM NC ND NE NH
+ NJ NM NV NY OH OK OR PA RI SC
+ SD TN TX UT VA VT WA WI WV WY
+);
+
+die("Usage: $0 <dictionary file location>") unless @ARGV <= 1;
+
+my $file = $ARGV[1] // '/usr/share/dict/words';
+
+# Read word list
+open my $fh, '<', $file;
+my @words;
+while (my $line = <$fh>) {
+ chomp $line;
+ push @words, $line;
+}
+close $fh;
+
+# Get list of all possible candidate words
+my @candidates = grep { is_candidate($_) } @words;
+
+# Find maximum word length
+my $max = max map { length($_) } @candidates;
+
+# Find the longest one(s) and print them.
+say "Longest matches: ", join(" ", grep { length($_) == $max } @candidates);
+
+
+sub is_candidate($word) {
+ # We don't want words that are not some multiple of 2 characters
+ # long.
+ return if length($word) % 2;
+
+ # We'll use this array for the component parts of the word (the
+ # potential state abbreviations), upper casing them.
+ my (@parts) = map { uc substr($word, $_*2, 2) } 0..(length($word)/2 - 1);
+
+ # A state abbrevaition can only appear once - and "part" must be a
+ # valid state abbreviation.
+ my $last = '';
+ for my $part (@parts) {
+ # Duplicate abbreviation
+ return if $part eq $last;
+
+ # Not a state abbreviation
+ return if ! exists $states{$part};
+
+ $last = $part;
+ }
+
+ return 1;
+}
+
+
diff --git a/challenge-014/joelle-maslak/perl6/ch-2.p6 b/challenge-014/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..befc46733d
--- /dev/null
+++ b/challenge-014/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl6
+use v6;
+
+unit sub MAIN(Str:D $dict? = '/usr/share/dict/words');
+
+my $states = Set.new: «
+ AK AL AR AZ CA CO CT DE FL GA
+ HI IA ID IL IN KS LA MA MD ME
+ MI MO MN MS MT NM NC ND NE NH
+ NJ NM NV NY OH OK OR PA RI SC
+ SD TN TX UT VA VT WA WI WV WY
+»;
+
+# Get list of all possible candidate words
+my @candidates = $dict.IO.lines.race.grep: { is-candidate($_) };
+
+# Find the longest one(s) and print them
+my @longest = @candidates.grep: { .chars == @candidates».chars.max };
+say "Longest matches: ", @longest.join(" ");
+
+sub is-candidate(Str:D $word -->Bool:D) {
+ # We don't want words that are not some multiple of 2 characters
+ # long.
+ return False unless $word.chars %% 2;
+
+ # We'll use this array for the component parts of the word (the
+ # potential state abbreviations), upper casing them.
+ my @parts = $word.comb(2)».uc;
+
+ # A state abbreviation can only appear once.
+ return if Bag.new(@parts).first( { $_.value > 1 } ).defined;
+
+ # We also don't want any words that contain two letters that are not
+ # a state abbreviation.
+ return if @parts.first( { ! $states{$_} } );
+
+ # So we know that the word
+ return True;
+}
+
+