Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Help with code-lists and strings (http://www.velocityreviews.com/forums/t741615-re-help-with-code-lists-and-strings.html)

Terry Reedy 01-05-2011 07:58 PM

Re: Help with code-lists and strings
 
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.

--
Terry Jan Reedy


Dave Angel 01-06-2011 03:17 AM

Re: Help with code-lists and strings
 
On 01/-10/-28163 02:59 PM, GrayShark wrote:
>

< <snip>
> 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 ) ) )
>


Two problems with that. One is that you've defined a NOR function, but
called it nand(). The other is using eval. There's no need for it, and
it's both slow and risky.

DaveA

Steven D'Aprano 01-06-2011 04:19 AM

Re: Help with code-lists and strings
 
Apologies if this comes through twice, I'm having problems with my news
client and/or provider.


On Wed, 05 Jan 2011 16:56:40 -0600, GrayShark wrote:

> 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.



Speaking of bad habits:

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



What is the purpose of the two calls to eval, other than potentially
introducing serious security bugs, being slow, and raising exceptions?


def nand(a, b):
return not (a and b)


is faster and safer, and less likely to cause annoyance if somebody
manages to fool you into executing something similar to:

nand("0", "__import__('os').system('# r m -rf /')")


More safely, and works on both Linux and Windows:

nand("0", "__import__('os').system('dir .')")



> 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.


Eww. Greasy food.

I think the idiom you are thinking of is "now you're cooking with gas",
gas cooking being cleaner, faster and easier than cooking with wood.


> def mixed_case( str ):
> return nand( "'%s'.islower()" % str , "'%s'.isupper()" % str )


I'm afraid that's incorrect, because it returns True for strings that
aren't mixed case:

>>> mixed_case("123")

True

as well as strings that can't be mixed anything on account of being a
single character:

>>> mixed_case("!")

True


A better solution would be:


def ismixed(s):
seen_upper = seen_lower = False
for c in s:
if c.isupper(): seen_upper = True
if c.islower(): seen_lower = True
if seen_upper and seen_lower:
return True
return False


which should return True if and only if the string contains both
lowercase and uppercase characters.




--
Steven

Dave Angel 01-06-2011 11:38 AM

Re: Help with code-lists and strings
 
On 01/06/2011 12:28 AM, Steven Howe wrote:
> On 01/05/2011 07:17 PM, Dave Angel wrote:
>> On 01/-10/-28163 02:59 PM, GrayShark wrote:
>>>

>> < <snip>
>>> 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 ) ) )
>>>

>>
>> Two problems with that. One is that you've defined a NOR function, but
>> called it nand(). The other is using eval. There's no need for it, and
>> it's both slow and risky.
>>
>> DaveA
>>

> Stupid.
> All code is risky.
> As to a nand operation, it not the same as a nor operation.
> Stupid


Well, if you're going to attack me, you could try to get your facts
straight. Of course namd is different than nor. If it were not, I
wouldn't have responded.

NAND is defined as NOT (A and B)

while NOR is defined as NOT (A or B)

What you defined was the contrapositive of NOR, but you called it nand()

If you need a refresher, check out
http://en.wikipedia.org/wiki/Negated_AND_gate
In particular, see the truth table, using ones and zeroes.


DaveA


All times are GMT. The time now is 12:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.