note
grinder
<p>Just a word on all the solutions that use [sort] as the means of getting the longest word:</p>
<blockquote><b>Don't</b></blockquote>
<p>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).</p>
<p>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.</p>
<p><code><update></code>This is the code I was thinking of (simplifying the problem of where the words come from for the sake of the argument):</p>
<code>
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;
}
}
</code>
<p>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.</p>
<p><code></update></code></p>
<p>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.</p>
<br><small><font color="#bcc332">p</font><font color="#a7d013">r</font><font color="#90dc02">i</font><font color="#78e401">n</font><font color="#5feb10">t</font><font color="#48ef29">@</font><font color="#33ef4b">_</font><font color="#21ee71">{</font><font color="#12e998">s</font><font color="#07e2bc">o</font><font color="#01d8d8">r</font><font color="#00ccea">t</font> <font color="#03beef">k</font><font color="#09aee8">e</font><font color="#129dd4">y</font><font color="#1d8bb6">s</font> <font color="#2a7991">%</font><font color="#39666a">_</font><font color="#495544">}</font><font color="#5a4324">,</font><font color="#6c340c">$</font><font color="#7f2501">/</font><font color="#911902">i</font><font color="#a30f0c">f</font><font color="#b3071f">%</font><font color="#c20238">_</font><font color="#d00056">=</font><font color="#db0076">s</font><font color="#e40597">p</font><font color="#eb0fb5">l</font><font color="#ee1dce">i</font><font color="#ef2ee2">t</font><font color="#ee43ed">/</font><font color="#e95aef">/</font><font color="#e272e9">,</font><font color="#d88ada">'</font><font color="#cca1c4">=</font> <font color="#beb7a8">&</font> <font color="#aeca89">*</font><font color="#9eda68">a</font><font color="#8ce649">?</font><font color="#79ed2d">b</font><font color="#67ef16">:</font><font color="#55ed07">e</font><font color="#44e600">\</font><font color="#34da02">f</font><font color="#26ca12">/</font><font color="#19b730">h</font><font color="#0fa158">^</font><font color="#078984">h</font><font color="#0271ae">!</font><font color="#0059d1">j</font><font color="#0042e8">+</font><font color="#022eef">n</font><font color="#051ce7">,</font><font color="#0a0ecf">o</font><font color="#1005ac">@</font><font color="#170081">o</font><font color="#1f0055">;</font><font color="#28062e">r</font><font color="#321011">$</font><font color="#3d2001">s</font><font color="#483402">-</font><font color="#544b1a">t</font><font color="#606448">%</font><font color="#6d7d80">t</font><font color="#7a97b7">#</font><font color="#87afdf">u</font><font color="#93c5ef">'</font></small>
212177
212177