For golf purposes, the data usually isn't embedded in the code. Here's a not-too golfed 116 characters that is flexible enough to have you specify your dictionary file as the first argument.

$s=99;$c='abcdef';for(<>){chomp;$w=$_;$l=y///c;@m=grep{1+index$w,$_}sp +lit//,$c;@m>5&&$s>$l||next;$s=$l;$n=$w}print$n

If we know already that we're looking for something shorter than ten characters, we can save one character right off the bat.

$s=9;

But wait, we're storing an array just to take a count of its elements later. We also know there's just the one newline after the word, so chomp wastes one. We can save some strokes there, too. 110:

$s=9;$c='abcdef';for(<>){chop;$w=$_;$l=y///c;(5<grep{1+index$w,$_}spli +t//,$c)&&$s>$l||next;$s=$l;$n=$w}print$n

Speaking of storing things and referring to them later... 104:

$s=9;for(<>){chop;$w=$_;$l=y///c;(5<grep{1+index$w,$_}split//,'abcdef' +)&&$s>$l||next;$s=$l;$n=$w}print$n

Sometimes you can drop characters by switching your control flow a bit. 103:

$s=9;for(<>){chop;$w=$_;$l=y///c;(5<grep{1+index$w,$_}split//,'abcdef' +)&&$s>$l&&do{$s=$l;$n=$w}}print$n

Sometimes you can remove an operation by thinking a bit differently about your data. 101:

$s=9;for(<>){$w=$_;$l=y///c;(6<grep{1+index$w,$_}split//,"abcdef\n")&& +$s>=$l&&do{$s=$l;$n=$w}}print$n

All of this, mind you, meets the exact spec of finding the shortest word rather than printing a sorted list of words. It's from scratch against that spec rather than deriving from the other solutions already presented. This is just a stab at describing the process of golfing down a solution that came to me from reading the spec. Notice I dropped 15 characters in 5 revisions from something that already had no whitespace, and this solution doesn't abuse the language too badly considering it's code golf. I'm sure someone can come along with some dirtier tricks and more magical parts of the language and get a shorter pure Perl solution.


In reply to Re: Find the shortest word in the English Language with: a b c d e f by mr_mischief
in thread Find the shortest word in the English Language with: a b c d e f by usemodperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.