Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2023-03-22 06:43 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.