Re: Help with code-lists and strings

Discussion in 'Python' started by Terry Reedy, Jan 5, 2011.

  1. Terry Reedy

    Terry Reedy Guest

    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
    Terry Reedy, Jan 5, 2011
    #1
    1. Advertising

  2. Terry Reedy

    Dave Angel Guest

    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
    Dave Angel, Jan 6, 2011
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Jan 6, 2011
    #3
  4. Terry Reedy

    Dave Angel Guest

    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
    Dave Angel, Jan 6, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robin
    Replies:
    10
    Views:
    533
    Dave Hansen
    Apr 12, 2006
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    390
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Daniel Nogradi
    Replies:
    3
    Views:
    344
    Dennis Lee Bieber
    Nov 10, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    740
    Malcolm
    Jun 24, 2006
  5. Chris Weisiger

    Help with sorting lists of lists

    Chris Weisiger, Oct 14, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    121
    Tad McClellan
    Oct 14, 2004
Loading...

Share This Page