Pregunta

How to write a pseudocode to split a number value to array of numbers? Let's say the number value is 12345. I need to convert it into [1,2,3,4,5].

¿Fue útil?

Solución

num_to_arr(num):
   length_of_arr = log(num)+1 //log(num) base 10
   arr[length_of_arr]
   digit = 0

   while num > 0:
      digit = num % 10
      arr[length_of_arr - 1] = digit
      length_of_arr = length_of_arr - 1
      num = num / 10

return arr

This should do the job.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a cs.stackexchange
scroll top