Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

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

Just a word on all the solutions that use sort as the means of getting the longest word:


This is incredibly wasteful. A byproduct of calling sort is that you also solve the problem of determining the ordering of all the elements amongst themselves. All that information in unnecessary to solve the problem at hand, and what is more, the performance certainly isn't O(n).

You can find the longest word in a single pass of a list (and BrowserUk, if you can only do this in two passes, or with a hash, well, I'm glad you don't work for me :). You can't sort an arbitrary list in a single pass, (or if you can, I'd got a few people I'd like you to meet). This means that the sort-based solutions are not going to scale as well as a single-pass approach.

<update>This is the code I was thinking of (simplifying the problem of where the words come from for the sake of the argument):

my $max = 0; my @longest; for $word( @words ) { my $length = length $word; if( $max < $length ) { $max = $length; @longest = (); push @longest, $word; } elsif( $max == $length ) { push @longest, $word; } }

At the end of this single pass through the words, you will have a counter holding the length of the longest word(s), and an array that contains the word(s). No hashes needed.


And this is exactly the kind of program that people are going to start throwing dictionaries at. What is more, sort more or less insists that your set fits in memory, whereas the my approach needs only about as much space as the largest word.

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to Re: The longest word .... by grinder
in thread The longest word .... by AltBlue

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    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: (5)
    As of 2020-04-04 00:04 GMT
    Find Nodes?
      Voting Booth?
      The most amusing oxymoron is:

      Results (32 votes). Check out past polls.