Facebook Interview Question
Software Engineer InternsCountry: United States
Interview Type: Phone Interview
Reads from arguments:
public class Convert {
public static void main(String[] argv) {
String num = argv[0];
// split string is not a parsing function.
// you could write a for loop instead if you like
String[] num_pow = num.split("E");
if (num_pow.length == 1) {
num_pow = num.split("e");
}
double f = 0.0;
int p = 0;
char[] dec = num_pow[0].toCharArray();
while(dec[p++] != '.');
p--;
double c = Math.pow(10.0, p - 1);
for (int k = 0; k < dec.length; k++) {
if (dec[k] != '.') {
f = f + c * floatOf(dec[k]);
c = c / 10.0;
}
}
// Decode the exp. term
char[] exp_char = null;
boolean neg = false;
if (num_pow[1].charAt(0) == '-') {
exp_char = num_pow[1].substring(1).toCharArray();
neg = !neg;
}
else if (num_pow[1].charAt(0) == '+') {
exp_char = num_pow[1].substring(1).toCharArray();
}
else {
exp_char = num_pow[1].toCharArray();
}
double e = 0.0;
c = Math.pow(10, exp_char.length - 1);
p = 0;
while(p < exp_char.length) {
e += c * floatOf(exp_char[p]);
c /= 10;
p++;
}
if (neg) {
e = -e;
}
f *= Math.pow(10.0, e);
System.out.println(f);
}
public static double floatOf(char dig) {
switch(dig) {
case '0':
return 0.0;
case '1':
return 1.0;
case '2':
return 2.0;
case '3':
return 3.0;
case '4':
return 4.0;
case '5':
return 5.0;
case '6':
return 6.0;
case '7':
return 7.0;
case '8':
return 8.0;
case '9':
return 9.0;
}
return -1;
}
}
hi, here a c++ solution...im not sure what you mean by built-in in function...
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
float string_to_float(const string &number) {
int exp_pos = number.find("E");
int exp_num = 0;
float float_num = 0.0;
if(exp_pos != string::npos) {
stringstream ss;
ss << number.substr(exp_pos + 1, number.size() - exp_pos - 1);
ss >> exp_num;
}
else {
return float_num;
}
stringstream ss;
ss << number.substr(0, exp_pos);
ss >> float_num;
float_num = float_num * pow(10, exp_num);
return float_num;
}
int main() {
// your code goes here
cout << string_to_float("342.18E-2") << endl;
return 0;
}
int inv(char c){
return (int)c - int('0');
}
int range(int c){
return c >= 0 && c <= 9 ? 1 : 0;
}
void convert(){
char *A = "123.145E-3";
int a = strlen(A);
int power = 1;
int rest = 0;
int expvalue = 0;
double restvalue = 0;
int j = 0;
int decimal1 = 0;
int decimal = 0;
for (int i = 0; i < a; i++){
if (rest){
if (range(inv(A[a - i - 1]))){
restvalue = restvalue + inv(A[a - i - 1])*pow(10, j);
j++;
}
else if (A[a - i - 1] == '.'){
//expvalue *= -1;
//power = 0;
//j--;
decimal = i - decimal1-1;
//cout << decimal;
}
}
if (power){
if (range(inv(A[a - i - 1]))){
expvalue = expvalue + inv(A[a - i - 1])*pow(10, i);
}
else if (A[a - i - 1] == '-'){
expvalue *= -1;
power = 0;
}
else if (A[a - i - 1] == 'E'){
rest = 1;
j = 0;
decimal1 = i;
power = 0;
}
//(A[a-1-i])
}
else if (A[a - i - 1]=='E'){
rest = 1;
j = 0;
decimal1 = i;
power = 0;
}
}
restvalue = restvalue*pow(10, -decimal)*pow(10, expvalue);
cout << restvalue;
};
- mahdi.oraei February 25, 2014