Help with code-lists and strings

GrayShark howe.steven at gmail.com
Wed Jan 5 17:56:40 EST 2011


On Wed, 05 Jan 2011 14:58:05 -0500, Terry Reedy wrote:

> On 1/5/2011 12:57 PM, Cathy James wrote:
> 
>> I am learning python and came across an excercise where i need to use
>> lists to strip words from a sentence; starting with those containing
>> one or more uppercase letters, followed by words with lower case
>> letters.
> 
> When writing code, it is good to start with one or more input-output
> pairs that constitute a test. For example, what, exactly, do you want to
> result from
>     "Some special words are ALLCAPS, TitleCase, and MIXed."
> 
> It is also good to think about all relevant cases. Note the following:
>  >>> 'MIXed'.isupper() or 'MIXed'.istitle()
> False
> 
> Do you want punctuation stripped off words? You might skip that at
> first.

In python it's best to build up you functional needs. So two steps. First
a nand (negative 'and' operation). Then wrap that with a function to create
two strings of your list element, you''re calling 'word'. By the way,
list is reserved word, like string. Don't get in the bad habit of using it.

def nand( a, b ):
	"""nand has to vars. Both must be strings """
	return( ( not eval( a ) ) and ( not eval( b ) ) )

Eval of 'Abcd'.isupper() returns False. Ditto 'Abcd'.islower(); negate both
results, 'and' values, return.

Now wrap 'nand' in packaging an you're cooking with grease.
def mixed_case( str ):
	return nand( "'%s'.islower()" % str , "'%s'.isupper()" % str )

or if that's too advanced/compact, try ...  
def mixed_case( str ):
	# nand() needs strings
	a = "'%s'.isupper()" % str
	b = "'%s'.islower()" % str
	res = nand( a, b )
	return res

>>> mixed_case('Abcd' )
True
>>> mixed_case('ABCD' )
False
>>> mixed_case('abcd' )
False

Good luck
Steven Howe



More information about the Python-list mailing list