My goal here is pretty simple, I want to turn a number like 12435987234
into an array of integers in reverse order. So that would look like:
[4,3,2,7,8,9,5,3,4,2,1]
I want to do this without changing the number to a string and back. Really, I don't want to convert it at all but I'm open to hearing ideas around this.
I've found one way to do in ruby via #divmod
but suspect there is a more perfomant way.
# turns 1234 into
# [4,3,2,1]
def reversed_digits(val)
quot = val
results = []
while quot > 0 do
quot, remainder = quot.divmod(10)
results << remainder
end
results
end
puts reversed_digits(1234)
=> [4,3,2,1]
desired_output_length=1
. \$\endgroup\$ – CiaPan Jan 21 '16 at 10:41