Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by mr_mischief (Monsignor)
on Jul 06, 2018 at 00:25 UTC ( #1218008=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1218008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-09-22 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (127 votes). Check out past polls.

    Notices?