Python How Can I Combine Slicing and Reversing to Determine Palindromes in Python?

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
The discussion focuses on creating a Python program to determine if a string is a palindrome by reversing it and comparing the original and reversed strings. The user has written a function to reverse a string but encounters syntax errors and issues with string comparisons. Suggestions include correcting the equality operator from "=" to "==" and ensuring that the functions return strings instead of integers. Additionally, there is a debate about the definition of palindromes and whether to include negative signs in numerical strings. The conversation also touches on the functionality of Jupyter notebooks regarding code execution across different cells.
  • #51
Rightly or wrongly, I'll take it as a personal compliment to my explanatory skills that you were able to significantly revise my code to get your version.
 
Technology news on Phys.org
  • #52
OK. What if the string starts with some whitespace?
 
  • #53
Svein said:
OK. What if the string starts with some whitespace?
Then you would need to define how whitespace (and other non-alphanumeric) characters should be treated in the context of a palindrome (I would do this by adding test cases), work out whether the way the current algorithm deals with them fits that definition (by running the test cases), and if not, change it so that it does (and all the tests pass).

You would also need to ensure that the way the code deals with characters outside the ASCII range fit your definition of a palindrome (noting that in many languages including Erlang strings are sequences of Unicode code points). For instance is the French phrase Léon, émir cornu d’un roc, rime Noël a palindrome?
 
Last edited:
  • #54
pbuk said:
And if you are going to handle numbers, be careful with e.g. 12343210. This is why we have unit testing (another reason why playing with the REPL or Jupyter notebook is not "real" programming).
I'm still figuring out how to compose slices [:a:b} , with a,b Integers, to eliminate a last place 0 and then reverse it. I've been putting of the (pretentious term) , algebra of slices (to avoid going ito loc. and .iloc). So far, I get that [:0:-1] reverses. But I need to set up an hour or so to detail it.
 
  • #55
Svein said:
OK. What if the string starts with some whitespace?
This is more at an intro level, so not really an issue and would require too much of a detour.
 
  • #56
WWGD said:
A general question about Jupyter for Python : Do definitions apply throughout in different cells in the same notebook, i.e., if I define a function in cell j of notebook k, can I apply it in any other cell of the same notebook k? Strangely, I've been able to do it only some times. I m ean, I used Rev(x), the function that reverses a string, in the definition of the Pal function, as in : "if String==Rev(String) return("Yes, Pal"), but this hasn't worked other times.

Heading out till tomorrow on PC. Thanks all.
I know this is old, and maybe it's covered elsewhere in this thread. In Jupyter, if you run a cell that creates a function or variable, then those functions and variables will remain in memory, available to use for the remainder of that session (until you close or reset the Kernel). You could open a notebook, then scroll halfway down, execute a cell which creates some variables or functions, then scroll back to the top and use those functions.

But it would be better to set up the notebook so that it could be run in order by another person, so they get the same results as you.
 
Back
Top