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
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
;)
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
--
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 =
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
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
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
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
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:
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
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
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 !-)
--
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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,
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
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
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
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
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
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.
--
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
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?
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
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
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
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
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
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.
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
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
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
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
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
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
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
56 matches
Mail list logo