Re: palindrome iteration

2010-09-14 Thread Bearophile
Baba: def i_palindrome(pal): while len(pal)1: if pal[0] == pal[-1]: pal=pal[1:-1] return True print i_palindrome('annab') In normal programming a significant percentage of the time is spent debugging. Experience shows that even short functions may be buggy. If you don't want to

Re: palindrome iteration

2010-09-13 Thread Cousin Stanley
To deal with real palindromes such as, Madam, I'm Adam, you should probably strip all spaces and punctuation: # untested pat = re.compile(r'[a-z]') def is_palindrome(s): letters = pat.findall(s.lower()) return letters == reversed(letters) Using python 2.5 the above solution

Re: palindrome iteration

2010-09-12 Thread Chris Colbert
;) In [29]: s = 'bannab' In [30]: a = np.frombuffer(s.lower(), dtype='uint8') In [31]: np.all(a == a[::-1]) Out[31]: True In [32]: s = 'bannac' In [33]: a = np.frombuffer(s.lower(), dtype='uint8') In [34]: np.all(a == a[::-1]) Out[34]: False --

Re: palindrome iteration

2010-09-11 Thread Aahz
In article mailman.103.1282914852.29448.python-l...@python.org, Dave Angel da...@ieee.org wrote: def is_palindrom(s): s = s.lower() return s == s[::-1] To deal with real palindromes such as, Madam, I'm Adam, you should probably strip all spaces and punctuation: # untested pat =

Re: palindrome iteration

2010-08-29 Thread Josh English
This whole conversation got interesting, so I thought I'd run some speed tests: The code: from timeit import Timer def is_palindrome_recursive(s): if len(s) = 1: return True if s[0] != s[-1]: return False else: return is_palindrome(s[1:-1]) def

Re: palindrome iteration

2010-08-29 Thread Matteo Landi
Well, I tried the also the solution posted above (recursive w/o slicing and iterative), and I discovered they were the slowest.. is_palindrome_recursive 2.68151649808 is_palindrome_slice 0.44510699381 is_palindrome_list 1.93861944217 is_palindrome_reversed 3.28969831976

Re: palindrome iteration

2010-08-29 Thread Gregory Ewing
Steven D'Aprano wrote: I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) -- Greg -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-29 Thread Arnaud Delobelle
Matteo Landi landima...@gmail.com writes: Well, I tried the also the solution posted above (recursive w/o slicing and iterative), and I discovered they were the slowest.. is_palindrome_recursive 2.68151649808 is_palindrome_slice 0.44510699381 is_palindrome_list 1.93861944217

Re: palindrome iteration

2010-08-29 Thread Matteo Landi
I thought they reached you. Here they are again: def palindrome(str, i=0, j=-1): try: if str[i] == str[j]: return palindrome(str, i + 1, j - 1) return False except IndexError: return True def palindrome(str, i=0, j=-1): try:

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 27 août, 18:20, Mark Lawrence breamore...@yahoo.co.uk wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 27 août, 20:05, Jussi Piitulainen jpiit...@ling.helsinki.fi def palindromep(s):     return ( s == or              ( s[0] == s[-1] and                palindromep(s[1:-1]) ) ) I-can-write-lisp-in-any-language-p !-) -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 29 août, 06:39, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: Steven D'Aprano wrote:  I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) +1 QOTW !-) --

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article 8dunm7fv5...@mid.individual.net, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: Steven D'Aprano wrote: I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK kEY oN bY mISTAKE. :-) So it would seem

Re: palindrome iteration

2010-08-29 Thread Josh English
I have no idea. That's a lower level of programming than I'm used to dealing with. Josh (I also only tried the one value. Had I tried with other strings that would fail the test, some functions may have performed better.) On Aug 29, 2:19 am, Matteo Landi landima...@gmail.com wrote: Well, I

Re: palindrome iteration

2010-08-29 Thread MRAB
On 29/08/2010 21:34, Roy Smith wrote: In article8dunm7fv5...@mid.individual.net, Gregory Ewinggreg.ew...@canterbury.ac.nz wrote: Steven D'Aprano wrote: I'm not entirely sure what the use-case for swapcase is. Obviously it's for correcting things that were typed in with tHE cAPS lOCK

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article mailman.174.1283114875.29448.python-l...@python.org, MRAB pyt...@mrabarnett.plus.com wrote: On 29/08/2010 21:34, Roy Smith wrote: In article8dunm7fv5...@mid.individual.net, Gregory Ewinggreg.ew...@canterbury.ac.nz wrote: Steven D'Aprano wrote: I'm not entirely sure

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns the swapcased string. Could be, but the main use case seems to be for palindrome

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Richard Arts writes: On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s):    def reversed(s):        return s[::-1]    return s == reversed(s) That seems like a bit of overkill... Why would you want to define a

Re: palindrome iteration

2010-08-28 Thread Ian
On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially to Python noobs. This makes it clear what is

Re: palindrome iteration

2010-08-28 Thread Paul Rubin
Ian hobso...@gmaiil.com writes: On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially to Python

Re: palindrome iteration

2010-08-28 Thread Arnaud Delobelle
Paul Rubin no.em...@nospam.invalid writes: Ian hobso...@gmaiil.com writes: On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 09:48:47 +0100, Ian wrote: On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. It's silly, needlessly complicated, and

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns the swapcased string.

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Steven D'Aprano writes: On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Arnaud Delobelle writes: Also, I an not aware that it is customary in python to name predicate functions with a p suffix - Python is not Lisp! Just to clarify my position: I did not mean to imply that names like palindromep might be customary in Python - clearly they are not - and I am quite

Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Paul Rubin writes: Ian writes: On 27/08/2010 21:51, Jussi Piitulainen wrote: Meanwhile, I have decided to prefer this: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially to

Re: palindrome iteration

2010-08-28 Thread Jon Clements
On Aug 28, 11:55 am, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: [snip] Not everything needs to be a built-in method. There

Re: palindrome iteration

2010-08-28 Thread D'Arcy J.M. Cain
On Sat, 28 Aug 2010 09:48:47 +0100 Ian hobso...@gmaiil.com wrote: def palindromep(s): def reversed(s): return s[::-1] return s == reversed(s) I like this. s[::-1] is obscure and non-obvious, especially to Python noobs. This makes it clear what is going on and why

Re: palindrome iteration

2010-08-28 Thread Dave Angel
Jussi Piitulainen wrote: Steven D'Aprano writes: On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote: Terry Reedy writes: On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It

Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 15:11:03 +0300, Jussi Piitulainen wrote: [...] When I said that there could be such a method, I was merely objecting to a statement, made in response to me, that there could not be such a method because strings are immutable. You clearly agree with me that that statement

palindrome iteration

2010-08-27 Thread Baba
level: beginner the following code looks ok to me but it doesn't work. I would like some hints as to where my reasoning / thought goes wrong def i_palindrome(pal): while len(pal)1: if pal[0] == pal[-1]: pal=pal[1:-1] return True print i_palindrome('annab') my reasoning: - i check the

Re: palindrome iteration

2010-08-27 Thread Xavier Ho
One possible reason I can think of - - exiting this loop means all compared chars were identical hence it is a palindrome and i return True is probably incorrect reasoning. Think again. Also, you may consider posting your code in a way that preserves the whitespace characters. Cheers, Xav On

Re: palindrome iteration

2010-08-27 Thread Peter Otten
Baba wrote: level: beginner the following code looks ok to me but it doesn't work. I would like some hints as to where my reasoning / thought goes wrong def i_palindrome(pal): while len(pal)1: if pal[0] == pal[-1]: pal=pal[1:-1] return True Do yourself a favour and use 4-space

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
Baba a écrit : level: beginner the following code looks ok to me but it doesn't work. doesn't work is about the most useless description of a problem. Please specify what you expected and what actually happens. I would like some hints as to where my reasoning / thought goes wrong def

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,

Re: palindrome iteration

2010-08-27 Thread Matteo Landi
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. I've always used to implement the is_palindrome function as you suggest, i.e. comparing the original string with the

Re: palindrome iteration

2010-08-27 Thread Dave Angel
Bruno Desthuilliers wrote: div class=moz-text-flowed style=font-family: -moz-fixedBaba a écrit : level: beginner the following code looks ok to me but it doesn't work. doesn't work is about the most useless description of a problem. Please specify what you expected and what actually

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
Richard Arts a écrit : 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

Re: palindrome iteration

2010-08-27 Thread Bruno Desthuilliers
Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? -- http://mail.python.org/mailman/listinfo/python-list

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 11:49:42 -0400 D'Arcy J.M. Cain da...@druid.net wrote: is_palindrome = lambda x: x == x.lower()[::-1] Oops. Simple and wrong. is_palindrome = lambda x: x.lower() == x.lower()[::-1] -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves

Re: palindrome iteration

2010-08-27 Thread Mark Lawrence
On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It could be worse, try responding to issue 9702. :) Cheers. Mark Lawrence. --

Re: palindrome iteration

2010-08-27 Thread Ian
On 27/08/2010 09:53, Baba wrote: level: beginner the following code looks ok to me but it doesn't work. I would like some hints as to where my reasoning / thought goes wrong def i_palindrome(pal): while len(pal)1: if pal[0] == pal[-1]: pal=pal[1:-1] return True print

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 16:43:16 +0200 Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid wrote: Dave Angel a écrit : def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? How about a one-liner?

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It could be worse, try responding to

Re: palindrome iteration

2010-08-27 Thread Mark Lawrence
On 27/08/2010 17:53, MRAB wrote: On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me feel a bit more stupid :-/ Who's next ? It

Re: palindrome iteration

2010-08-27 Thread Terry Reedy
On 8/27/2010 4:53 AM, Baba wrote: level: beginner the following code looks ok to me but it doesn't work. I would like some hints as to where my reasoning / thought goes wrong def i_palindrome(pal): while len(pal)1: if pal[0] == pal[-1]: pal=pal[1:-1] return True print

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 18:28, Mark Lawrence wrote: On 27/08/2010 17:53, MRAB wrote: On 27/08/2010 17:20, Mark Lawrence wrote: On 27/08/2010 15:43, Bruno Desthuilliers wrote: Dave Angel a écrit : (snip) or (untested) def is_palindrom(s): s = s.lower() return s == s[::-1] Right, go on, make me

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Ian writes: If you want to or must do it recursively. (Shown in pseudo code to make the logic clearer) def isPalindrome(pal) ''' test pal (a list) is a palindrome ''' if length of pal = 1 return True # all one letter strings are palindromes. if first equals last

Re: palindrome iteration

2010-08-27 Thread Dave Angel
Jussi Piitulainen wrote: Ian writes: If you want to or must do it recursively. (Shown in pseudo code to make the logic clearer) def isPalindrome(pal) ''' test pal (a list) is a palindrome ''' if length of pal = 1 return True # all one letter strings are palindromes.

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 12:02:39 -0400 D'Arcy J.M. Cain da...@druid.net wrote: On Fri, 27 Aug 2010 11:49:42 -0400 D'Arcy J.M. Cain da...@druid.net wrote: is_palindrome = lambda x: x == x.lower()[::-1] Oops. Simple and wrong. is_palindrome = lambda x: x.lower() == x.lower()[::-1] slightly

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
Dave Angel writes: Jussi Piitulainen wrote: Ian writes: Of course, the simpler way is to use the definition of a Palindrome as the same backwards and forwards. def isPalindrome(pal) return pal == pal.reverse Agreed. But is there any nicer way to spell .reverse than [::-1] in

Re: palindrome iteration

2010-08-27 Thread MRAB
On 27/08/2010 20:43, Jussi Piitulainen wrote: Dave Angel writes: Jussi Piitulainen wrote: Ian writes: Of course, the simpler way is to use the definition of a Palindrome as the same backwards and forwards. def isPalindrome(pal) return pal == pal.reverse Agreed. But is there any

Re: palindrome iteration

2010-08-27 Thread Jussi Piitulainen
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

Re: palindrome iteration

2010-08-27 Thread Terry Reedy
On 8/27/2010 3:43 PM, Jussi Piitulainen wrote: Dave Angel writes: There could easily be a .reverse() method on strings. It would return the reversed string, like .swapcase() returns the swapcased string. Could be, but the main use case seems to be for palindrome testing ;-) Given that

Re: palindrome iteration

2010-08-27 Thread Richard Arts
On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen jpiit...@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

Re: palindrome iteration

2010-08-27 Thread Richard Arts
On Fri, Aug 27, 2010 at 11:47 PM, Richard Arts arts.rich...@gmail.com wrote: On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen jpiit...@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