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
> 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
;)
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
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
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
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
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
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
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
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
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
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:
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
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
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_
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_
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
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
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
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
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-
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
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
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
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
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
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.
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
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
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
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
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
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
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,
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
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
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
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.
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
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
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(
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
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
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(
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
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/|
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 =
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
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
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
> 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
> 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
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_
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
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
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
56 matches
Mail list logo