Today's Problem of the Day is to reimplement a classic function, atoi. Atoi takes in one parameter, a string, and returns an int version of that string. So when passed the string "10" atoi will return 10. If the string can't be converted to an int just return 0 or throw an error.
Comments:
Anonymous - 10 years, 4 months ago
In Ruby this is trivial:
reply permalink
David - 10 years, 3 months ago
An Atoi by any other name is still Atoi.
reply permalink
Dustin S. - 10 years, 4 months ago
I wrote this in Java, but, this could be a general language implementation. Sorry the indentation is weird.
reply permalink
Dustin S. - 10 years, 4 months ago
Sorry, I should have put
double power = Math.pow(10, s_len -1 - i);
in the else-if block.reply permalink
bumbleguppy - 10 years, 3 months ago
javascript also has the native parseInt() function, but here's my effort:
reply permalink
Aaron Frase - 10 years, 3 months ago
Here's my implementation in python without using int().
reply permalink