Reverse Digits of an integer – Python Program – One of Most Common Interview Question

Python program to reverse a number, reverse digits of a number, reverse integer, python programs, python programs to reverse a number, basic python programs.

Reverse Digits of an integer –  One of Most Common Interview Question

Problem 

– Write a Python program to reverse digits of an integer.

Example 1 :

Input : 123,

Output : 321

Example 2 :

Input : -234

Output : -432

Solution

class Solution(object):
    def reverse_solution1(self, x):
        y=int(str(abs(x))[::-1])
        if x<0: y=-1*y
        return 0 if y> (2**31)-1 or y< -1*(2**31) else y
    
    def reverse_solution2(self, x):   
        num = 0
        while ( x > abs(0) ) :
            d = x%10
            num = num*10 + d
            x = x/10
            
        return num

 

Tags: Python program to reverse a number, reverse digits of a number, reverse integer, python programs, python programs to reverse a number, basic python programs

Leave a Comment

%d bloggers like this: