Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Palindrome case

I’m writing a function to check whether the integer is palindrome on Leetcode. Most of cases are acceptable. But there is a special case which is 10, why the output of my code return True. The idea for my code is convert integer to str and then convert it to list. Then reverse the list and compare two lists whether are the same.

Is my idea or code does not work out right or there are sth I missed, would be appreciate if someone can point out.
Here is the code screenshot


initial_list=list(str(x))

temp = initial_list

initial_list.reverse()

if temp == initial_list:
    print('True')
else:
    print('False')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To create a copy of your initial list, use:
temp = initial_list.copy()

Otherwise, temp is just a reference to the original initial_list.

Try printing temp again after applying initial_list.reverse()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading