[Tutor] Finding the shortest word in a list of words
Marc Tompkins
marc.tompkins at gmail.com
Tue Jan 20 18:20:22 CET 2009
On Tue, Jan 20, 2009 at 5:42 AM, Lie Ryan <lie.1296 at gmail.com> wrote:
> > 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. This doesn't
> > catch ties, though... could do that like so:
>
>
> Other than overkill, it is wrong. If the shortest "word" is longer than
> maxint it'd give wrong result.
What language do you speak (or machine do you use), where the shortest word
in a potential list is longer than maxint? On my machine maxint is
2,147,483,647. That's what I meant by overkill.
You could just simply use the len of the first word.
True dat. Requires an extra step or two, though - initializing with some
impossibly huge number is quick.
> The best solution though, have been answered by Kent Johnson.
>
Extremely terse and elegant, doesn't find ties.
def MinKent(corpora=""):
> words= corpora.split()
> return min(words, key=len)
>
> print MinKent("No victim has ever been more repressed and alienated than
> the truth is")
>
Output: No
Mine returns (2, ['No', 'is'], 9, ['repressed', 'alienated']). I could be
wrong - it seemed more like what the OP actually wanted, but he'd be the
judge of that.
--
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090120/55842588/attachment.htm>
More information about the Tutor
mailing list