Created
January 6, 2013 06:34
-
-
Save agush22/4465642 to your computer and use it in GitHub Desktop.
Flip a number without changing it to string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flipnum num | |
res = 0 | |
mag = magnitude(num) | |
neg = true if num < 0 | |
num = num.abs | |
while num > 0 | |
mag -= 1 | |
res += (num % 10)* 10 ** mag | |
num = num/10 | |
end | |
unless neg | |
res | |
else | |
res * -1 | |
end | |
end | |
def magnitude num | |
num = num.abs | |
count = 0 | |
while num > 0 | |
count += 1 | |
num = num/10 | |
end | |
count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment