palindrome iteration

Richard Arts arts.richard at gmail.com
Fri Aug 27 17:52:39 EDT 2010


On Fri, Aug 27, 2010 at 11:47 PM, Richard Arts <arts.richard at gmail.com> wrote:
> On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen
> <jpiitula at ling.helsinki.fi> wrote:
>> MRAB writes:
>>> On 27/08/2010 20:43, Jussi Piitulainen wrote:
>>>> Dave Angel writes:
>>>>> Jussi Piitulainen wrote:
>>>>>> Agreed. But is there any nicer way to spell .reverse than [::-1]
>>>>>> in Python? There is .swapcase() but no .reverse(), right?
>>>>>>
>>>>> There can't be a .reverse() method on string, because it's
>>>>> immutable. You could use
>>>>>
>>>>>      "".join(reversed(pal))
>>>>>
>>>>> but I'd prefer  pal[::-1]  as I said earlier.
>>>>
>>>> There could easily be a .reverse() method on strings. It would
>>>> return the reversed string, like .swapcase() returns the swapcased
>>>> string.
>>>
>>> Lists have a .reverse method, but it's an in-place reversal. In
>>> order to reduce confusion, a string method which returned the string
>>> reversed would be better called .reversed().
>>
>> Yes, agreed.
>>
>> Meanwhile, I have decided to prefer this:
>>
>> def palindromep(s):
>>    def reversed(s):
>>        return s[::-1]
>>    return s == reversed(s)
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
> That seems like a bit of overkill... Why would you want to define a
> function in a function for something trivial like this? Just
>
> def palindrome(s):
>    return s[::-1]
>
> will do fine.
>
> Of course, you can stick the inner function in a library somewhere if you like.
>
> Regards,
> Richard
>

Duh, of course I mean

def palindrome(s):
   return s == s[::-1]

I'm sorry.

Richard



More information about the Python-list mailing list