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 wan

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

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 -- http://mail.python.org/mailman/listinfo/pytho

Re: palindrome iteration

2010-09-11 Thread Aahz
In article , Dave Angel 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.compile(r'[a-z]') def is_palindrome(s): letters = pat.find

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article , MRAB wrote: > On 29/08/2010 21:34, Roy Smith wrote: > > In article<8dunm7fv5...@mid.individual.net>, > > Gregory Ewing 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

Re: palindrome iteration

2010-08-29 Thread MRAB
On 29/08/2010 21:34, Roy Smith wrote: In article<8dunm7fv5...@mid.individual.net>, Gregory Ewing 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

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 wrote: > Well, I tried the also the solu

Re: palindrome iteration

2010-08-29 Thread Roy Smith
In article <8dunm7fv5...@mid.individual.net>, Gregory Ewing 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 (http://bugs.python

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 29 août, 06:39, Gregory Ewing 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 !-) -- http://mail.python.org/mailman/listinfo/python-l

Re: palindrome iteration

2010-08-29 Thread bruno.desthuilli...@gmail.com
On 27 août, 20:05, Jussi Piitulainen > 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 27 août, 18: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-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 Arnaud Delobelle
Matteo Landi 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 > is_palindrome_reversed 3.289

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 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 is_palindrome_recursive_no_

Re: palindrome iteration

2010-08-28 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 is_palindrome_

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 statemen

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 would

Re: palindrome iteration

2010-08-28 Thread D'Arcy J.M. Cain
On Sat, 28 Aug 2010 09:48:47 +0100 Ian 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 at a co

Re: palindrome iteration

2010-08-28 Thread Jon Clements
On Aug 28, 11:55 am, Steven D'Aprano 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 is already a standard > way

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-

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 quit

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

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 swapcas

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 complicat

Re: palindrome iteration

2010-08-28 Thread Arnaud Delobelle
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-obviou

Re: palindrome iteration

2010-08-28 Thread Paul Rubin
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 Python noobs.

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 goin

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

Re: palindrome iteration

2010-08-27 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 palin

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

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 .rever

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 slicin

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,

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 nicer

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 t

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" wrote: > On Fri, 27 Aug 2010 11:49:42 -0400 > "D'Arcy J.M. Cain" wrote: > > is_palindrome = lambda x: x == x.lower()[::-1] > > Oops. Simple and wrong. > > is_palindrome = lambda x: x.lower() == x.lower()[::-1] slightly more efficient I thi

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 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 equal

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 feel

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 i_palindrome(

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 co

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 is

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 i_palindrome(

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. -- h

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" 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 | Democracy is three wolves http://www.druid.net/darcy/|

Re: palindrome iteration

2010-08-27 Thread D'Arcy J.M. Cain
On Fri, 27 Aug 2010 16:43:16 +0200 Bruno Desthuilliers 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? is_palindrome = lambda x: len(x)> 0 and x =

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 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 halve

Re: palindrome iteration

2010-08-27 Thread Dave Angel
Bruno Desthuilliers wrote: 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 reasonin

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 reve

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

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 i_

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 u

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

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 l