Domanda di colloquio di Booking.com
Given a integer , return corresponding ASCII char representation without using language building in feature.
ex. input interger 1234, return "1234" in string or characters
Risposte di colloquio
function numberToString(n){
if(n/10 < 1) return "" + n //edge case
return numberToString(Math.round(n/10)) + "" + n%10
//I concatenate with the empty string just to convert the number to a string without using built-in toString()
}
def itoa(num):
array = []
while num > 0:
rem = num % 10
array.append(chr(rem + 48))
num = num // 10
array.reverse()
return ''.join(array)
num = 1234
result=''
while (num > 0):
rem = num % 10
result = str(rem) + result
num = int(num / 10)
print(result)
def int2str(num: Int): String = {
if (num > 0)
int2str(num / 10) + (num % 10).toString
else ""
}
def int2str(num: Int): String = {
if (num > 0)
int2str(num / 10) + (num % 10 + 48).toChar
else ""
}
num_to_convert = 10 # or any INT number
converted_str = ''
while True:
remainder = num_to_convert % 10
converted_str += str(remainder)
if (num_to_convert // 10) <= 0:
break
else:
num_to_convert //= 10
print(converted_str[::-1])
while (num > 0)
{
int rem = num % 10;
result += rem;
num = num / 10;
}
string x = Reverse(result);
return x;