Edit: Not going to edit this node because of replies but Eily noticed there is an extra single quote at the end of my port :-/
This recent post in r/programming poses an interesting question for a bit of golf:
"What is the shortest word in the English Language which contains: a b c d e f?"
Some Junk™ code was posted to figure this out and my Perl version is a bit shorter. I know you perl better than me, if you do, and can see how else to do this:
The Junk Code:
sorted([w.strip() for w in open('/usr/share/dict/words', 'r').readline
+s()
if set(list('abcdef')).issubset(set(list(w.strip())))], key=lambda x:
+len(x))
My Perl Port:
open$:,"</usr/share/dict/words";while(<$:>){next unless/(?=.*a)(?=.*b)
+(?=.*c)(?=.*d)(?=.*e)(?=.*f)/i;push@_,$_}@_=sort{length$a<=>length$b}
+@_'
For your convenience:
Print the list:
open$:,"</usr/share/dict/words";while(<$:>){next unless/(?=.*a)(?=.*b)
+(?=.*c)(?=.*d)(?=.*e)(?=.*f)/i;push@_,$_}print for sort{length$a<=>le
+ngth$b}@_
Print the word:
open$:,"</usr/share/dict/words";while(<$:>){next unless/(?=.*a)(?=.*b)
+(?=.*c)(?=.*d)(?=.*e)(?=.*f)/i;push@_,$_}for(sort{length$a<=>length$b
+}@_){print;last}
Compare length of Perl to Junk:
#!/usr/bin/perl -lw
use strict;
my $PERL = <<'PERL';
open$:,"</usr/share/dict/words";while(<$:>){next unless/(?=.*a)(?=.*b)
+(?=.*c)(?=
.*d)(?=.*e)(?=.*f)/i;push@_,$_}@_=sort{length$a<=>length$b}@_'
PERL
{ no strict;
$JUNK = <<JUNK;
sorted([w.strip() for w in open('/usr/share/dict/words', 'r').readline
+s()
if set(list('abcdef')).issubset(set(list(w.strip())))], key=lambda x:
+len(x))
JUNK
print length $PERL, ' PERL: ', $PERL;
print length $JUNK, ' JUNK: ', $JUNK;
}
# I think junk could lose 4 spaces making it 148.
__END__
PERL: 144
JUNK: 152
Of course different versions of dict give different results...
STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!—CPAN 🐪
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.