Number to Digits

tags: linear-scan

Write a function that takes a number and returns a list of its digits. For example, for input 2342 it should return [2,3,4,2].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def extract(n)
  result = []
  input = n.to_s

  for i in (0..input.size-1)
    result << input[i].to_i
  end
  
  result
end

p extract(2342)

The input is converted to a string and we scan through the string one by one and add it to the output array after it is converted to an integer.