<div class="gmail_quote">2009/1/19 John Fouhy <span dir="ltr"><<a href="mailto:john@fouhy.net">john@fouhy.net</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2009/1/20 Emad Nawfal (عماد نوفل) <<a href="mailto:emadnawfal@gmail.com">emadnawfal@gmail.com</a>>:<br>Of course, this is not necessarily the best answer for your particular<br>
problem. The problem with sorting is that you have to look at some<br>
elements more than once. For short lists, it's not a problem, but it<br>
can slow you down on bigger lists. You could also find the shortest<br>
element by going through the list, remembering the shortest element<br>
you've seen so far. This will be quicker if you only want to find the<br>
single shortest.<br>
<font color="#888888"></font></blockquote><div><br>Here's the first thing that came to mind:<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
from sys import maxint<br><br>def MinMax(Sentence=""):<br> minLen = maxint <br> maxLen = 0<br><br> for word in Sentence.split():<br> if len(word) > maxLen:<br> maxWord = word<br> maxLen = len(word)<br>
if len(word) < minLen:<br> minWord = word<br> minLen = len(word)<br> return minLen, minWord, maxLen, maxWord<br><br><br>print MinMax("No victim has ever been more repressed and alienated than the truth") <br>
</blockquote></div></div><br clear="all">Using sys.maxint to prime minLen is overkill, of course - "antidisestablishmentarianism" is only 28 letters long, after all - but it should be larger than any word you can expect to see.<br>
This doesn't catch ties, though... could do that like so:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"> for word in Sentence.split():<br>
if len(word) == minLen:<br> minWord.append(word)<br> if len(word) == maxLen:<br> maxWord.append(word)<br> if len(word) > maxLen:<br> maxWord = [word]<br> maxLen = len(word)<br>
if len(word) < minLen:<br> minWord = [word]<br> minLen = len(word)<br><br></blockquote><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>