Search code examples
pythonlistintegerreverse

Reverse digits of a list of numbers in python


for i in range(len(primo)):
    reversed_num = 0
    while i > 0:
       digit = i % 10
       reversed_num = reversed_num * 10 + digit
       i //= 10
       reversed.append (reversed_num)
    
for r in range(len(reversed)):
    print (r)

I'm writing a program to reverse a series of numbers from a list. For example:

Input: 1234,124,654

Output: 4321,421,456

The script works correctly with one number, but with a list it doesn't work properly.

EDIT: ......................................

This is the working whole code.

#program to find the multiple of the first number that summed to its reverse creates a number divisible for the second number chosen. Results are shown up to five digits.

import sys

def main():
    x = 0
    y = 0
    k = 0
    z = 0
    print ("Hello! Let's compare some numbers!\nPress input to continue")
    input()
    print ("Insert the first number")
    x = input()
    print ("Insert the second number")
    y = input()
    print()
    
    primo = []
    secondo = []
    
    #list multiples
    
    while k < 99999:
        k = int(k) + int(x)
        print (k)
        primo.append (k)
        
    while z < 99999:
        z = int(z) + int(y)
        print(z)
        secondo.append (z)
        
    print("The numbers have been calculated! Press to proceed")

    input()
    
    print("LOADING...")

    #reverse all numbers of the first list
    
    def myReverser(n: float) -> float:
       return float(''.join(list(reversed(str(n)))))
    
    reversedList = [myReverser(i) for i in primo]
    reversedList = [int(i) for i in reversedList]
    
    #sum the multiple of the first number and its reversed and search common values in res and secondo
    
    res = [
    x
    for x, y in zip(primo, reversedList)
    if (x + y) in secondo
]

    print()
    print()

    if len(res) == 0:
        print ("No results found")
    else:
        print ("The numbers are: ", res)

    #ending

    print ()
    print ("Thank you for using my program!")
    print ()
    choice = input ("Would you try with another number? [Y/N] ").capitalize()

    if choice == "Y":
        main()
    if choice == "N":
        sys.exit()

main()

#no copyright intended. This code is open-source. Feel free to use it in other projects.

The script can compare the two given numbers to find the multiple of the first number that summed to its reverse creates a number divisible for the second number chosen. Results are shown up to five digits.


Solution

  • Nice try, but what about a function that reverses the numbers? You should be happy to know that reversed already exists in Python, and you can use it this way:

    >>> list(reversed(str(0.1223142131)))
    ['1', '3', '1', '2', '4', '1', '3', '2', '2', '1', '.', '0']
    >>> float(''.join(reversed(str(0.1223142131))))
    1312413221.0
    

    so you can implement your own function:

    def myReverser(n: float) -> float:
        return float(''.join(reversed(str(n))))
    

    and then use it over the whole list:

    reversedList = [myReverser(i) for i in [1234, 124, 654]]
    

    If you don't want the .0 at the end of all the numbers, this means that you don't want floating point numbers, but integers, so you can just do this:

    reversedList = [int(i) for i in reversedList]