palindrome iteration

M

MRAB

Dave said:
Jussi said:
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
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().
 
J

Jussi Piitulainen

MRAB said:
Dave said:
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)
 
T

Terry Reedy

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 slicing and reversed() can do the same thing, the need is thin.
 
R

Richard Arts

MRAB said:
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)

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
 
R

Richard Arts

MRAB said:
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)

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
 
J

Jussi Piitulainen

Terry said:
Could be, but the main use case seems to be for palindrome testing ;-)
Given that slicing and reversed() can do the same thing, the need is thin.

The need is quite thin, but immutability of strings is not an issue,
just like there can be .swapcase() though strings are immutable. That
is all I am saying above.
 
J

Jussi Piitulainen

Richard said:
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
function in a function for something trivial like this? Just

def palindrome(s):
return s[::-1]

will do fine.

I'm sure your version will do something just fine, but what that
something is, I can not tell. The body of your version is quite
obscure and does not seem to agree with the name of the function.

I find (s == reversed(s)) a clearer expression than (s == s[::-1]),
and I found a simple way to use my preferred expression.
Of course, you can stick the inner function in a library somewhere
if you like.

From my point of view, it would be an understatement to say that
setting up a library for this would be an overkill. A simple local
auxiliary function is nothing.
 
I

Ian

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 going on and why at a cost of very little code.

Very helpful to the maintenance programming in 18 months time!

Regards

Ian
 
P

Paul Rubin

Ian said:
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.

Overriding the 'reversed' builtin even in an inner scope is a little bit
ugly.

If you don't mind some overhead, list(s)==list(reversed(s)) (using the
built-in reversed, not the special obscure one) is pretty clear.
 
A

Arnaud Delobelle

Paul Rubin said:
Ian said:
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.

It may be non-obvious to newcomers, but it is quite a well known idiom.
Also, I an not aware that it is customary in python to name predicate
functions with a "p" suffix - Python is not Lisp!
Overriding the 'reversed' builtin even in an inner scope is a little bit
ugly.

I agree.
If you don't mind some overhead, list(s)==list(reversed(s)) (using the
built-in reversed, not the special obscure one) is pretty clear.

May I suggest a comment instead:

def ispalindrome(s):
# s[::-1] evaluates to the string s reversed
return s == s[::-1]
 
S

Steven D'Aprano

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 inefficient. Why create a *one
line* nested function that only gets called once? Every single time you
call the function, it has to create the inner function again, then call
it once, then throw it away. Admittedly Python does recreate the inner
function from pre-compiled parts, which is quick, but still, it doesn't
gain you anything that a simple comment wouldn't give:

def palindromep(s):
return s == s[::-1] # Compare s to its reverse.

s[::-1] is obscure and non-obvious, especially to Python noobs.

*Only* to Python noobs. Slicing is fundamental to Python, and using a
slice of [::-1] to reverse something is a basic Python idiom.

This makes it clear what is going on and why at a cost of very little
code.

Very helpful to the maintenance programming in 18 months time!

Only if writing three lines when one would do is your definition of
"helpful".
 
S

Steven D'Aprano

The need is quite thin, but immutability of strings is not an issue,
just like there can be .swapcase() though strings are immutable. That is
all I am saying above.


You're right, there could be a reversed() method for strings. There could
also be a disemvowel method that removes vowels, a randomise method that
shuffles the letters around, a studlycaps method that changes the case of
each letter randomly, and a method to check that brackets () are well-
formed. They would all be useful to somebody. There are lots of different
methods that strings could have. Where do you draw the line?

Not everything needs to be a built-in method. There is already a standard
way to spell "reverse a string":

astring[::-1]

If you don't like that, you can do this:

''.join(reversed(astring))


I don't object to a hypothetical reverse() method on strings, but the
gain is minimal.
 
J

Jussi Piitulainen

Steven said:
You're right, there could be a reversed() method for strings. There
could also be a disemvowel method that removes vowels, a randomise
method that shuffles the letters around, a studlycaps method that
changes the case of each letter randomly, and a method to check that
brackets () are well- formed. They would all be useful to
somebody. There are lots of different methods that strings could
have. Where do you draw the line?

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 was not correct. Would you have let it stand if it was
made to you?

To answer your question, I don't see a real need for .reversed() in
strings, but I do think .reversed() would be much more useful than
..swapcase() which is in Python now and for which I see no use at all.

I have not proposed adding anything to Python. I have only asked if
there is any nicer expression for string reversal than [::-1] in
Python now, and corrected an incorrect statement that was made in
response to me that there could not be a string reversal method
because Python strings are immutable.

I am still not proposing that anything be added to Python.

I have not even criticized Python for not having a nicer expression
for string reversal than [::-1]. I have merely asked if there is one,
because I didn't know if there is one, and I have shown some snippets
of code to illustrate what I might mean by nicer. Someone even
understood me. (Thanks.)

I think I have received the answer to my question by now - that there
is no obviously nicer way, and all other string reversal expressions
require some extra cruft and overhead.
Not everything needs to be a built-in method. There is already a
standard way to spell "reverse a string":

astring[::-1]

If you don't like that, you can do this:

''.join(reversed(astring))

I know. I agree. I was also shown a different way to test for
palindromicity,

list(s) == list(reversed(s))

which is quite nice apart from the overhead.
I don't object to a hypothetical reverse() method on strings, but
the gain is minimal.

I have not suggested that such a method should be added to the
language. I merely corrected a statement that there could not be such
a method because strings are immutable. I would not have bothered to
do even that if that incorrect statement had not been made in response
to my own post.

I agree that the gain would be minimal. There is no harm in the method
either, so I would not object to it if somebody were to propose its
addition, but just to clarify my position: I have not proposed it.

Hope this helps.
 
J

Jussi Piitulainen

Arnaud said:
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 aware that Python is not Lisp.

My background is elsewhere, I was not paying particular attention to
the name at all, and I just could not be bothered to look up what
implications any of palindrome, palindromic, ispalindrome,
is_palindrome, isPalindrome, has_palindrome_nature, check_palindrome
and so on might have in Python.

Perhaps I should have used a neutral name like f or test or so, but it
did not occur to me at the time.
 
J

Jussi Piitulainen

Paul said:
Ian said:
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.

Overriding the 'reversed' builtin even in an inner scope is a little
bit ugly.

If you don't mind some overhead, list(s)==list(reversed(s)) (using
the built-in reversed, not the special obscure one) is pretty clear.

Thanks for that. I'm beginning to like it - not its overhead but
certainly its natural clarity. It wins over ''.join(reversed(s))
easily, in my eyes.
 
J

Jon Clements

[snip]
Not everything needs to be a built-in method. There is already a standard
way to spell "reverse a string":

astring[::-1]

If you don't like that, you can do this:

''.join(reversed(astring))

I've had to debug code that assumed str(reversed('abc')) == 'cba''<reversed object at 0xa66f78c>'

So, a str doesn't "construct" like tuple/list...it's a call to
__str__().
It's designated as a "friendly print out" (that's my phrasing).
['a', 'b', 'c']

I s'pose str is special (2.6) in some way, but it doesn't parallel the
other builtins.

[Not at Terry / Steve intended -- just most relevant post to respond
to]

Jon.
 
D

D'Arcy J.M. Cain

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 cost of very little code.

It seems unnecessary to me. Even if you can't figure it out through
simple inspection, it takes seconds to fire up Python and type "print
'abc'[::-1]" into it to see what that does. Then you have another tool
in your toolbox.
 
D

Dave Angel

Jussi said:
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 was not correct. Would you have let it stand if it was
made to you?
Since you repeat that assertion three times, I figure you must think
it's important. And it was I who asserted that a reverse() method
wouldn't be possible on an immutable object. reverse() would reverse
the characters in place, and return None. At least it would if it tried
to be at all consistent with the list, array, and audioop methods of the
same name.

reversed() is certainly possible, and it'd make a new string with the
reverse order of the original.


DaveA
 
S

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 was not correct. Would you have let it stand if it was
made to you?

Ha ha, you're new here aren't you?
To answer your question, I don't see a real need for .reversed() in
strings, but I do think .reversed() would be much more useful than
.swapcase() which is in Python now and for which I see no use at all.

It's hard to disagree with that. I'm not entirely sure what the use-case
for swapcase is. It's not quite as specialised as sTUdlEycApS but not far
off.


[...]
I agree that the gain would be minimal. There is no harm in the method
either, so I would not object to it if somebody were to propose its
addition, but just to clarify my position: I have not proposed it.

Then we are in agreement :)

I think the only thing we disagree on is that I think [::-1] is a
perfectly nice expression for reversal, while you don't. True, it's not
entirely intuitive to newbies, or self-documenting, you need to learn
slicing to understand it. But then, if you were Dutch and had not learned
English, you would probably be looking for a method called omgekeerde and
would find reverse equally unintuitive.
 
G

Gregory Ewing

Steven said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top