L******k 发帖数: 395 | 1 leetcode 几乎把easy 和middle 的做了一遍
面试了十几个公司,电面都很好。可是一旦考程序就挂。
怎么回事?
我已经找工作找了3个月了。
是不是要狠心把leetcode 再刷几遍? |
z*********8 发帖数: 2070 | 2 Why not paste your code here? |
L******k 发帖数: 395 | 3 Problem Description
-------------------
Your task is to write a command-line program to evaluate a set of
equations, each specified on separate lines. An equation is defined
by:
=
is the left-hand side of the equation and is always a variable
name. A variable name can only be composed of letters from the
alphabet (e.g. for which isalpha(c) is 1). is the right hand
side of the equation and can be composed of variables, unsigned
integers, and the + operator.
Here is one example set of equations:
offset = 4 + random + 1
location = 1 + origin + offset
origin = 3 + 5
random = 2
Your program should take a filename as input. The file contains a set
of equations, like the above. It should evaluate the set of equations
and print the unsigned integer value of each variable.
=
The output should be sorted by in ascending order by variable name.
The output for the example above would be:
location = 16
offset = 7
origin = 8
random = 2
You may assume the following: You may assume the input is well formed.
There will be one or more white spaces between each token. You may use
C++, the Standard C libraries and the Standard Template Library (STL).
You may use std::sort and qsort. All variables in the equation set
will have a definition. You may also assume a variable is only defined
once and will have a valid integer solution.
Submission
----------
We recognize that your time is valuable so please wind down your
investigations if you hit the 3 hour mark, or earlier if you hit
diminishing returns.
Submit your submission to me via email with the following:
1. Source code
2. Example output
3. (if necessary) instructions on how to build and execute your code
4. (optionally) any thoughts you'd like to share |
L******k 发帖数: 395 | 4 my solution:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void jf_eval(string& in_line, unordered_map
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int index1 = 0;
int i = i_dex;
for (; i < in_line.size(); ++i)
{
if (isdigit(in_line[i]))
{
new_number = false;
ele = ele * 10 + in_line[i] - '0';
}
else if (!isdigit(in_line[i]) && !new_number)
{
new_number = true;
cur_value += ele;
ele = 0;
}
if (isalpha(in_line[i]) && new_variable)
{
index1 = i;
new_variable = false;
}
else if (!isalpha(in_line[i]) && !new_variable)
{
new_variable = true;
varibles.push_back(in_line.substr(index1, i - index1));
index1 = 0;
}
}
if (ele) cur_value += ele;
if (index1) varibles.push_back(in_line.substr(index1, i - index1));
record[cur_var] = make_pair(cur_value, varibles);
}
int jf_assign(string key, unordered_map>>&
record)
{
if (!record[key].second.size())
return record[key].first;
else
{
int elevalue = record[key].first;
for (auto unres : record[key].second)
{
elevalue += jf_assign(unres, record);
}
return elevalue;
}
}
void main()
{
string line;
ifstream myfile("input.txt");
ofstream resfile;
resfile.open("output.txt");
unordered_map>> record;
if (myfile.is_open())
{
//string line_res;
while (getline(myfile, line))
{
cout << line << '\n';
jf_eval(line, record);
//resfile << line_res << '\n';
}
myfile.close();
vector jfresult;
for (auto ele : record)
{
ostringstream ss;
ss << jf_assign(ele.first, record);
string temp = ele.first + " = " + ss.str();
jfresult.push_back(temp);
}
sort(jfresult.begin(), jfresult.end());
for (auto ele : jfresult)
{
resfile << ele << '\n';
}
resfile.close();
}
else cout << "Unable to open file";
} |
b**********5 发帖数: 7881 | 5 我觉得你这种要写很多code的, 最好给点comments。。。 人家看你code的人,也是人
啊。。 骂了一天的code, 还要看你的commentless code, 头都疼啊
string>
【在 L******k 的大作中提到】 : my solution: : #include : #include : #include : #include : #include : #include : #include : using namespace std; : void jf_eval(string& in_line, unordered_map
|
j*****n 发帖数: 23 | 6 #include
#include
#include
#include
#include
#include
#include
using namespace std; //这个最好解释一下不会真的在production上这么写
void jf_eval(string& in_line, unordered_map
//jf 是什么意思?
>>& record)
{
int i_dex = in_line.find_first_of(' ');
string cur_var = in_line.substr(0, i_dex);
vector varibles;
int cur_value = 0, ele = 0;
bool new_variable = true, new_number = true; int index1 = 0;
int i = i_dex;
for (; i < in_line.size(); ++i)
{
if (isdigit(in_line[i]))
{
new_number = false;
ele = ele * 10 + in_line[i] - '0';
}
else if (!isdigit(in_line[i]) && !new_number)
{
new_number = true;
cur_value += ele;
ele = 0;
}
if (isalpha(in_line[i]) && new_variable)
{
index1 = i;
new_variable = false;
}
else if (!isalpha(in_line[i]) && !new_variable)
{
new_variable = true;
varibles.push_back(in_line.substr(index1, i - index1));
index1 = 0;
}
}
if (ele) cur_value += ele;
if (index1) varibles.push_back(in_line.substr(index1, i - index1));
/*上面那堆也可以就这样就可以了
stringstream ss;
ss << in_line ;
ss >> cur_var;
string nouse;
ss >> nouse;
vector variables;
while(ss){
string curVal;
ss >> curVal;
检查curVal是= 还是数字还是variable
}
*/
record[cur_var] = make_pair(cur_value, varibles);
}
int jf_assign(string key, unordered_map>>&
record)
{
if (!record[key].second.size())
return record[key].first;
else
{
int elevalue = record[key].first;
for (auto unres : record[key].second)
{
elevalue += jf_assign(unres, record);
}
//这最好把最终结果赋给record[key].first 然后清空record[key].second
//省的每次都要重新算
return elevalue;
}
}
void main()
{
string line;
ifstream myfile("input.txt");
ofstream resfile;
resfile.open("output.txt");
unordered_map>> record;
if (myfile.is_open())
{
//string line_res;
while (getline(myfile, line))
{
cout << line << '\n';
jf_eval(line, record);
//resfile << line_res << '\n';
}
myfile.close();
vector jfresult;
for (auto ele : record)
{
ostringstream ss;
ss << jf_assign(ele.first, record);
string temp = ele.first + " = " + ss.str();
jfresult.push_back(temp);
}
sort(jfresult.begin(), jfresult.end());
for (auto ele : jfresult)
{
resfile << ele << '\n';
}
resfile.close();
}
else cout << "Unable to open file";
} |
L******k 发帖数: 395 | 7
string>
多谢指点,我仔细看看您的注释
【在 j*****n 的大作中提到】 : #include : #include : #include : #include : #include : #include : #include : using namespace std; //这个最好解释一下不会真的在production上这么写 : void jf_eval(string& in_line, unordered_map : //jf 是什么意思?
|
i*****e 发帖数: 218 | 8 你是否已经有许多软件工作的经验 ?
你被拒绝, 是否不是由于你刷题不行, 而是由于你实际做项目的经验不够呢 ?
我也在考虑转行软件, 大家一起努力吧。
如果你在硅谷南湾, 你可以试试这个。
http://www.mitbbs.com/article/JobHunting/32988555_0.html
【在 L******k 的大作中提到】 : leetcode 几乎把easy 和middle 的做了一遍 : 面试了十几个公司,电面都很好。可是一旦考程序就挂。 : 怎么回事? : 我已经找工作找了3个月了。 : 是不是要狠心把leetcode 再刷几遍?
|
c*d 发帖数: 61 | 9 从函数定义就可以看出你经验不足:
void jf_eval(string& in_line, unordered_map < string, pair
string>>> &record)
-->
void jf_eval(const string& in_line, unordered_map < string, pair
>> &record)
int jf_assign(string key, unordered_map>>&
record)
-->
int jf_assign(const string& key, const unordered_map
>>& record)
【在 L******k 的大作中提到】 : leetcode 几乎把easy 和middle 的做了一遍 : 面试了十几个公司,电面都很好。可是一旦考程序就挂。 : 怎么回事? : 我已经找工作找了3个月了。 : 是不是要狠心把leetcode 再刷几遍?
|
l****g 发帖数: 761 | |
|
|
p**r 发帖数: 5853 | 11 电面本来就很容易,
我除了一边带娃/吃饭/开车,一边电面这种分心的情况,
不然貌似没不过的。 |
l*****k 发帖数: 1059 | |
f****a 发帖数: 98 | |
m*****n 发帖数: 204 | 14 胡说几句供参考。c++看着费劲就没仔细看,说错了请勿怪。
这个程序在算法方面有两个问题:
1. 没有一个报错机制。给个 v1 = v2 ; v2 = v1; 程序就无限循环了吧。“v =
3 + ”, “v= v2 - 1” 这种非法输入好像也能混过去。
2. 没用Cache记录计算出的变量。出现多次的变量会重复计算。
另外Line parsing 函数比较乱。要是用一个函数先tokenize再用另一个函数处理会好
些。
string>
【在 L******k 的大作中提到】 : my solution: : #include : #include : #include : #include : #include : #include : #include : using namespace std; : void jf_eval(string& in_line, unordered_map
|
i*****e 发帖数: 218 | 15 最近几个月是不是面试少了 ?
大公司是不是不招人了 ?
================================================================
你是否已经有许多软件工作的经验 ?
你被拒绝, 是否不是由于你刷题不行, 而是由于你实际做项目的经验不够呢 ?
我也在考虑转行软件, 大家一起努力吧。
如果你在硅谷南湾, 你可以试试这个。
http://www.mitbbs.com/article/JobHunting/32988555_0.html
【在 L******k 的大作中提到】 : leetcode 几乎把easy 和middle 的做了一遍 : 面试了十几个公司,电面都很好。可是一旦考程序就挂。 : 怎么回事? : 我已经找工作找了3个月了。 : 是不是要狠心把leetcode 再刷几遍?
|
h*******3 发帖数: 52 | 16 是说 onsite 的时候在机器上写程序么?
你说的店面都不 考 online programming ?
【在 L******k 的大作中提到】 : leetcode 几乎把easy 和middle 的做了一遍 : 面试了十几个公司,电面都很好。可是一旦考程序就挂。 : 怎么回事? : 我已经找工作找了3个月了。 : 是不是要狠心把leetcode 再刷几遍?
|