golclinics / DSA-Clinics24

9 stars 309 forks source link

reverting the input #265

Open solundo7456 opened 2 weeks ago

solundo7456 commented 2 weeks ago

def reverse_integer(n):

Convert the integer to string, then reverse the string

reversed_str = str(n)[::-1]

# If the input was negative, keep the negative sign in front
if n < 0:
    # Remove the negative sign from the reversed string, then re-add it
    reversed_int = -int(reversed_str[:-1])
else:
    reversed_int = int(reversed_str)

return reversed_int