Can anyone help me write a code on python for checking palindrome numbers using while loop And pls explain it | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Can anyone help me write a code on python for checking palindrome numbers using while loop And pls explain it

Python programming

25th Sep 2023, 8:14 AM
ROOPASHREE S
ROOPASHREE S - avatar
8 ответов
+ 4
ROOPASHREE S , here some hints: one possible way is to take the input number (integer value) and split it to individual integer digits. since there is no function in python which can do this, we can use a while loop. # split number to digits: > to store the individual digits, we can use a list. create an empty list. > take a number as input and store it as an integer number in a varisble like `inp_num` > use a while loop with the condition that runs as long as `inp_num` is greater than 0 > use the `inp_num` and do a modulo division (%) by 10. this gives the last digit from `inp_num` store it in the list. > now we have to remove the last digit from `inp_num`. use an integer division (//) > after the condition of the while loop, we have a list of individual integer digits. > we can check now if this list equals to a reverse of the list. if yes => input is a palindrome.
25th Sep 2023, 11:52 AM
Lothar
Lothar - avatar
+ 4
Wong Hei Ming , you may be right with your first statement, but there are several challenges out there that are requesting to check for a palindrome number. and this is also frequently asked in interviews. > in all of these cases it is not allowed to use the number as a string and string methods, but to solve the task only using numerical values. this requires to use specific algorithms.
25th Sep 2023, 10:56 AM
Lothar
Lothar - avatar
+ 3
Attempts?
25th Sep 2023, 10:41 AM
A͢J
A͢J - avatar
+ 3
Wong Hei Ming , using a list as a temporarily buffer for the digits does not real look tricky, but i agree with you that your suggestion how the algorithm could be is a good way to solve the task.
26th Sep 2023, 1:11 PM
Lothar
Lothar - avatar
+ 1
Lothar I didn’t know a challenge like this exist. It is good to learn something new. I make a little research and found a piece of code in C++ to this question. Although I don’t know C++, but they are close enough to Java (which I’m learning) so I can read the code, which turns out to be a same technique as you described. However your explanation seems a bit tricky by using list data type. I though we are only allowed to use numerical data type only. To avoid using list, we can store the extracted digit into a variable. In the next loop we multiple it by 10 and add another digit extracted.
25th Sep 2023, 12:37 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
x = input() print(x==x[::-1])
25th Sep 2023, 11:02 PM
Mikita Wagner
Mikita Wagner - avatar
0
Palindrome numbers? 123321 is not the same as '123321'. A number (I assume it is an integer) is a number, there is no "forward" or "backward" of a number, unless you treat it as a string. If you want to check if a string is palindrome no or, just compare the original state with the reversed state. There are many ways to reverse a string and doesn't need to be a while loop. You can find it out with a little search with google.
25th Sep 2023, 9:31 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
With a little thinking, this kind of challenge exists probably memory was very limited in the back, maybe 640k or less. So a creative way to solve problems was needed. With this in mind, I searched how to check python code memory usage and come up with cProfile, a standard module. While the output doesn't exactly shows the usage, it shows number of function call. Apparently using a list as buffer make more calls, I assume it means more memory usage. https://sololearn.com/compiler-playground/cH63tZuFUwt8/?ref=app
26th Sep 2023, 2:16 PM
Wong Hei Ming
Wong Hei Ming - avatar