[Python] Find the palindrome numbers of a given range

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I want to find the palindrome numbers of a given range of integers without reversing the number. I want to rather check a condition going from digit to digit. If I use an if-else condition I get the numbers for which the condition holds only for one digit and not all of them. In order to make the condition hold for all digits I was tempted to use a while loop. But still I can not get the list of palindromes, the while loop running too long. Can somebody tell me what is wrong and what should I correct ? Thanks. Here is the code:

    def pal():
    s = []
    for i in range(100, 10000):
    for j in range(len(str(i))):
    while str(i)[j] == str(i)[len(str(i)) - (j+1)]:
    s.append(i)
    return s
    print(pal())

    Continue reading...

Compartilhe esta Página