Re: palindrome iteration

2010-08-27 Thread Richard Arts
> Now there is another solution. A palindrom is made of two symetric halves,
> with (odd len) or without (even len) a single char between the symetric
> halves, ie :
>
> * odd : ABCBA ('AB' + 'C' + 'BA')
> * even : ABCCBA ('ABC' + 'CBA')
>
> So you just have to extract the symetric halves, reverse one, and compare
> both (case insensitive compare while we're at it).

Yes, this is a correct observation, but it is not necessary to compare
the halves; Simply compare the complete string with its reverse. If
they match, it is a palindrome.

> Here's a possible (and a
> bit tricky) Python 2.x implementation:
>
> def is_palindrom(s):
>    s = s.lower()
>    slen = len(s)
>    until = slen / 2 # Python 2x integer division
>    offset = int(not(slen % 2))
>    runtil = until - offset
>    return s[0:until] == s[-1:runtil:-1]
>
>

At first glance this seems to be correct, but it is tricky indeed.
Particularly the assignment of the offset variable, casting a bool to
an integer of a negated expression. Given that Baba notes that this is
a beginners level query, it wouldn't have hurt to be a little bit more
verbose there.

Richard
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-27 Thread Richard Arts
On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-27 Thread Richard Arts
On Fri, Aug 27, 2010 at 11:47 PM, Richard Arts  wrote:
> On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen
>  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQL Problem

2010-09-03 Thread Richard Arts
These are also mere suggestions.

The statements you use in your print statement and the one you use to
feed the cursor differ slightly. The latter is missing quotes around
your search criterium.

Isn't it possible to fetch results row by row and see if the missing
row is in the set? That way you can get a better feeling about the
nature of the error.

> cursor.execute('insert into spreadsheets values (Null, %s, 0, %s, 0, Null)', 
> (client, prod))

Out of curiosity, why would you want to insert null values in id
fields? That's a disaster waiting to happen.

Regards,
Richard
-- 
http://mail.python.org/mailman/listinfo/python-list