[Tutor] using the reduce function

Daniel Ehrenberg littledanehren at yahoo.com
Thu Jan 22 19:33:50 EST 2004


Christopher Spears wrote:
> Yet another homework problem is giving me fits. 
> Here
> it goes:
> 
> Use the reduce function to find the length of the
> longest string in a list of strings.
> 
> I have been banging my head against the wall with
> this
> one.  Ouch!  I figured out how to do it another way
> using control structures:
> 
> >>> strings = ['long', 'longer', 'longest']
> >>> for i in range(len(strings)):
> 	count = 0
> 	if len(strings[i]) > count:
> 		count = len(strings[i])
> 
> 		
> >>> count
> 7
> 
> So how do I use the logic of this script and apply
> it
> to the problem?
> 
> -Chris

Unfortunately, you can't really use the logic of this
program with the reduce() function, and we're really
not supposed to give you the answer on your homework,
but I can still tell you the basic application logic.
Write a function that, given two strings, finds the
longer one. Then apply that function, using reduce, to
the sequence (in the form reduce(function, sequence)).
You will have the longest string. Then it's just a
matter of finding that string's length.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/



More information about the Tutor mailing list