http://qs321.pair.com?node_id=1218008


in reply to Find the shortest word in the English Language with: a b c d e f

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.