Pergunta de entrevista da empresa Amazon

Convert a number to an array of characters without using to_s

Respostas da entrevista

Sigiloso

16 de jan. de 2012

I think the interviewer was looking for some kind of use of modular math to separate the digits out, and then assuming that the string could be represented as an array of ASCII digits.

Sigiloso

22 de jan. de 2012

Here's an easy to understand version in python. Instead of chr(48 + nNum) you could just do str(nNum) but it can't hurt to demonstrate familiarity with ASCII. def to_string(number): chars = [] while number > 0: nNum = number % 10 number /= 10 chars.append(chr(48 + nNum)) chars.reverse() return chars Which would give something like >>> to_string(34839) ['3', '4', '8', '3', '9']