On Tue, Jan 20, 2009 at 11:23 AM, Lie Ryan <lie.1...@gmail.com> wrote:
> what I meant as wrong is that it is possible that the code would be used > for a string that doesn't represent human language, but arbitrary array > of bytes. Also, it is a potential security issue. This is something I need to know, then - sys.maxint is a potential security issue? How? Should it be avoided? (Guess I'd better get Googling...) > > 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. > > len() is fast, and it also removes the need to import sys, which actually > removes an extra step or two > Unintended consequence - initializing minLen with the length of the first word results in trying to append to a list that doesn't exist yet - so must create minWord and maxWord ahead of time. (Could use try/except... no.) Also - it occurred to me that the input might be sentences, and sentences contain punctuation... I already took the liberty of adding "is" to the end of the OP's signature quotation; now I add a period: > corpus = "No victim has ever been more repressed and alienated than the > truth is." Now "is." has length 3, not 2; probably not what we had in mind. So, new version: > def MinMax(corpus=""): > import string > corpus = "".join( [x for x in corpus if x not in string.punctuation] ) > words = corpus.split() > minLen = len(words[0]) > maxLen = 0 > minWord, maxWord = [],[] > for word in words: > curLen = len(word) > if curLen == minLen: > minWord.append(word) > if curLen == maxLen: > maxWord.append(word) > if curLen > maxLen: > maxWord = [word] > maxLen = curLen > if curLen < minLen: > minWord = [word] > minLen = curLen > return minLen, minWord, maxLen, maxWord > Is there a good/efficient way to do this without importing string? Obviously best to move the import outside the function to minimize redundancy, but any way to avoid it at all? which, at the time of writing, was my impression on the OP's request. > Quote: "I need to find the shortest / longest word(s) in a sequence of words." I'm sure the OP has moved on by now... time I did likewise. -- www.fsrtechnologies.com
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor