From 0122b5c28a3196dac7ec94a50d9cbc31d5b93f23 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 3 May 2021 23:46:14 +0100 Subject: include uncommented version and speed up --- challenge-111/james-smith/perl/ch-2.pl | 76 +++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/challenge-111/james-smith/perl/ch-2.pl b/challenge-111/james-smith/perl/ch-2.pl index b069866080..ce10a7e856 100644 --- a/challenge-111/james-smith/perl/ch-2.pl +++ b/challenge-111/james-smith/perl/ch-2.pl @@ -6,41 +6,77 @@ use warnings; use feature qw(say); ## These are the dictionary's supplied by Ubuntu. +## +## Name: # words +## ------- ------- +## small: 50,790 +## large: 166,828 +## huge: 344,861 +## insane: 654,299 -say longest('/usr/share/dict/british-english-small'); -say longest('/usr/share/dict/british-english-large'); -say longest('/usr/share/dict/british-english-huge'); -say longest('/usr/share/dict/british-english-insane'); - -## Aegilops is a genus of Eurasian and North American -## plants in the grass family, Poaceae. They are known -## generally as goatgrasses. Some species are known -## as invasive weeds in parts of North America. +say longest( '/usr/share/dict/british-english-small' ); +say longest( '/usr/share/dict/british-english-large' ); +say longest( '/usr/share/dict/british-english-huge' ); +say longest( '/usr/share/dict/british-english-insane' ); sub longest { open my $fh, q(<), $_[0]; my @max = (0); - (chomp) ## Remove newline character - && (!/\W/) ## Remove words with non-alpha chars - && (!/^[A-Z]/) ## Remove words starting with a capital - && (join( q(), sort { $a cmp $b } split //,lc $_) eq lc $_) + (chomp) ## Remove newline character + && !/\W/ ## Remove words with non-alpha chars + && !/^[A-Z]/ ## Remove words starting with a capital + && ( lc $_ eq join q(), sort split //, lc $_ ) ## Check the word is unchanged when the ## letters are sorted - && ( ($max[0] < length $_) + && ( $max[0] < length $_ ? ( @max = ( length $_, $_ ) ) ## If the word is longer than the max length (1st entry ## in @max - reset max to include the new max length and - ## the word. - : ( ($max[0] == length $_) - && (push @max,$_) - ) + ## the word.) + : ( ( $max[0] == length $_ ) && (push @max, $_ ) ) ) ## If the word is the same length as the maximal word ## push it onto @max - so we store all the longest words ## with maximum length. while <$fh>; return "$_[0] > @max"; - ## Return the name of the file used, the size of the words and - ## a complete list of the words of that length. + ## Return the name of the file used, the size of the words + ## and a complete list of the words of that length. +} + +sub longest_no_comments { + open my $fh, q(<), $_[0]; + my @max = (0); + (chomp) && !/\W/ && !/^[A-Z]/ + && ( lc $_ eq join q(), sort split //, lc $_ ) + && ( $max[0] < length $_ ? ( @max = ( length $_, $_ ) ) : + ( ( $max[0] == length $_ ) && (push @max, $_ ) ) ) + while <$fh>; + return "$_[0] > @max"; } +## Long words that you may not recognise.... +## +## All 21 of the 6 letter words in the "small" dictionary +## are common words. +## +## Two (billowy & beefily) are common in the "huge" list +## The other two 7 letter words are: +## +## chikors - An alternative spelling of chukars - A +## species of partridge native to central +## Asia (Alectoris chukar). +## +## dikkops - (From afrikaans) A bird of the family +## Burhinidae. The stone curlew, thick-knee +## Comes from dik-kop or thick head +## +## Finally the 8 letter word in the insane diction is: +## +## aegilops - a genus of Eurasian and North American +## plants in the grass family, Poaceae. +## They are known generally as goatgrasses. +## Some species are known as invasive weeds +## in parts of North America. + + -- cgit