Skip to content

Instantly share code, notes, and snippets.

@anbento0490
Last active December 16, 2020 09:55
Show Gist options
  • Save anbento0490/f9af3de342368a0f606403d22af83306 to your computer and use it in GitHub Desktop.
Save anbento0490/f9af3de342368a0f606403d22af83306 to your computer and use it in GitHub Desktop.
# Given an integer, return the integer with reversed digits.
# Note: The integer could be either positive or negative.
def solution(x):
string = str(x)
if string[0] == '-':
return int('-'+string[:0:-1])
else:
return int(string[::-1])
print(solution(-231))
print(solution(345))
@gunesmes
Copy link

>>> str(-1999)[::-1].rstrip("-")
'9991'
>>> str(1999)[::-1].rstrip("-")
'9991'

@jkariscodes
Copy link

Very helpful on algorithm tests. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment