Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Help with code-lists and strings

Reply
Thread Tools

Re: Help with code-lists and strings

 
 
Terry Reedy
Guest
Posts: n/a
 
      01-05-2011
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

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      01-06-2011
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-06-2011
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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      01-06-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments