Python Forum
Program to check whether a number is palindrome or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to check whether a number is palindrome or not
#11
(Sep-07-2023, 02:47 PM)DPaul Wrote:
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?

Look here:
https://www.guru99.com/1-in-python.html
Paul

I understood now, so we used it in order reverse the input, which is a way easier and shorter method to use

Thanks, I'll definitely keep this is mind
Reply
#12
Python has reversed() which will reverse a string, but you need ''.join().to get the actual reversed string.

num = 'x'
mun = 'y'
while not num == mun:
    num = input('Enter a number ... ')
    print(f'Using \'\'.join(reversed(num)) to reverse the number ... ')
    mun = ''.join(reversed(num))
    if num == mun:
        print(f'{mun} reversed is the same as the starting number {num}')
    else:
        print(f'{mun} is not the same as {num}')
I found this way of doing this, which is neat, but creates strings, which is inadvisable I'm told.

num = 'x'
mun = 'y'
while not num == mun:
    mun = ''
    num = input('Enter a number ... ')
    for n in num:
        mun = n + mun
Reply
#13
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?
which aspect of this are you asking?

1. why assign an extra variable y?

2. what does x[::-1] mean?

i would have written it with one statement instead of two:
x = '1234321'
if x == x[::-1]:
    print('The number is a palindrome')
else:
    print('Number is not palindrome')
is that what you were wondering about?

but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
(Sep-10-2023, 11:13 PM)Skaperen Wrote:
(Sep-07-2023, 01:43 PM)PythonBoy Wrote: I am interested in this method also. Could you please explain why did we write y = x[::-1]?
which aspect of this are you asking?

1. why assign an extra variable y?

2. what does x[::-1] mean?

i would have written it with one statement instead of two:
x = '1234321'
if x == x[::-1]:
    print('The number is a palindrome')
else:
    print('Number is not palindrome')
is that what you were wondering about?

but this has no loops, as your assignment requires. to do it with at least one loop, i would compare the individual characters, one by one, over both ends of the string, until everything has been compared. if any comparisons are unequal then it is not a palindrome. you need to known the length of the string to do this right. i assume you have learned how to get that. you want to have a loop that has two index variables, one for the front, and the other for the back. you compare the front one character to the back one character in the string. i assume know how to do that. if they are unequal, the loop needs to end, right? but if they are equal then it goes on. where does it go and and how does it get there?

I was asking what x[::-1] is. And they helped me figure it out.

Yes that is exactly the way I tried doing it but I got an error. Then I got to know that I use an outdated version of python

When I downloaded the new one, my code worked properly

Thank you!
Reply
#15
You mentioned that this is supposed to use loops. The slicing technique is better and faster, but must consider what the instructor is wanting to teach you. In this case, how to reverse a number (or string) using loops, and I would guess that the key instructional point is the use of a negative step value in a for loop. Have you tried
1. Convert integer into a string
2. Loop over the string in reverse (using step value of -1) to create your reversed string
3. Compare to original string

If this were not homework then the solutions of using slicing, reversed(), etc are great. In this case I bet your instructor wants you to reinvent the wheel!
Reply
#16
(Sep-11-2023, 12:21 PM)jefsummers Wrote: You mentioned that this is supposed to use loops. The slicing technique is better and faster, but must consider what the instructor is wanting to teach you. In this case, how to reverse a number (or string) using loops, and I would guess that the key instructional point is the use of a negative step value in a for loop. Have you tried
1. Convert integer into a string
2. Loop over the string in reverse (using step value of -1) to create your reversed string
3. Compare to original string

If this were not homework then the solutions of using slicing, reversed(), etc are great. In this case I bet your instructor wants you to reinvent the wheel!

Actually, good that you mentioned, I think this is the right and the easiest way my instructor wants me use. Because he gave us this assignment after teaching us about range(start,stop,step).
I will make sure to try using this method also

Thank you!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Reply
#17
i think the teacher wants you to do a little bit more than stepping an index downward. he/she wants you to be able to index both upward from the start and downward from the end at the same time. that and index the same string both ways.

do you need one loop or two loops to do that? he/she wants you to figure that out for your code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#18
Sitting at the keyboard is not the right way to start programming. First you solve the problem, then you code the solution.

Is 1234521 a palindrome
Output:
1_____1: First and last digits match. _2___2_: Second and second to last digits match. __3_5__: Third and third from last digits do not match. Not a palindrome
.
If we save the number as a str, we can use indexing to do the test.
number = "1234521"
number[0] == number[6]
number[1] == number[5]
number[2] != number[4]
number is not a palindrome
We can use the index at the start of the string to calculate the index for the end of the string.
number = "1234521"
numlen = len(number)
number[0] == number[numlen-1]
number[1] == number[numlen-2]
number[2] != number[numlen-3]
number is not a palindrome
The ending index == numlen - start_index - 1.
Now it should be easy to write a loop to do the test. You only have to loop through half the digits because you are comparing the first half of the number to the mirror of the second half. If you reach the middle without finding a mismatch, the number is a palindrome.
Reply
#19
(Sep-11-2023, 07:24 PM)deanhystad Wrote: Sitting at the keyboard is not the right way to start programming. First you solve the problem, then you code the solution.

Is 1234521 a palindrome
Output:
1_____1: First and last digits match. _2___2_: Second and second to last digits match. __3_5__: Third and third from last digits do not match. Not a palindrome
.
If we save the number as a str, we can use indexing to do the test.
number = "1234521"
number[0] == number[6]
number[1] == number[5]
number[2] != number[4]
number is not a palindrome
We can use the index at the start of the string to calculate the index for the end of the string.
number = "1234521"
numlen = len(number)
number[0] == number[numlen-1]
number[1] == number[numlen-2]
number[2] != number[numlen-3]
number is not a palindrome
The ending index == numlen - start_index - 1.
Now it should be easy to write a loop to do the test. You only have to loop through half the digits because you are comparing the first half of the number to the mirror of the second half. If you reach the middle without finding a mismatch, the number is a palindrome.

This is exactly what I wanted to do. Comparing the first and last digit and so on.

So I made use of loops and it worked. Thanks!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,442 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,635 Feb-24-2022, 10:40 AM
Last Post: perfringo
  Palindrome checker case sensive edwdas 3 2,651 Nov-07-2019, 05:57 PM
Last Post: nilamo
  Program that displays the number with the greatest amount of factors ilusmd 3 2,826 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Palindrome program - I can't figure it out. Gabar112 3 3,543 Feb-20-2018, 07:03 PM
Last Post: Larz60+
  Palindrome.strip pirts.emordnilaP RodNintendeaux 4 3,868 Oct-08-2017, 02:30 AM
Last Post: RodNintendeaux
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,825 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020