what is the best, efficient approach to rank the candidates
There are N candidates who sign up answer a series of multiple-choice questions. Design a efficient method to evaluate the answer and rank the candidates.
vector <string> Evaluate (const string& answer, const vector<string>& param)
{
// answer - will have the answer for multiple choice question.
// like X X X X X where each X is a capital letter (A-D)
// (e.g) A B A C D
// param will have candidate name followed by his/her answer
// (e.g) param[0] - "Foo A C A C D"
// param[1] - "Bar D D A C B"
// return candidates name with highest % of mark in result[0],
// next highest in result[1] and so on.
}
One approach that I could think of is, tokenize the answer & param and compare it. But I am looking for any better approach. Any suggestions ?
PS: This is not a homework. Came across th开发者_运维知识库is question in topcoder
You can compute the score for a single candidate without tokenizing, if you assume a single space between answers. First, search for the first space to skip then name, then pass the remaining string to the following function
int compute_score(const string& answer_key, const char answer[])
{
int score = 0;
for (unsigned i = 0; i < answer_key.size(); i+=2) {
score += answer[i]==answer_key[i];
}
return score;
}
One solution is going through all the candidates and grading their answers on a scale of 0 - 5 where 0 is perfect, 1 is 1 answer wrong, etc. After grading each candidate, insert it into a list. Then after grading all the candidates, sort the list by grade (or sort them as you're inserting them).
Alternatively, you could add them to a binary search tree while adding them. This would probably be faster than inserting them into a list and sorting it.
Another idea would be of using a static variable to Store the name of the candidate with the highest % so far. Update the static variable's value on further function call with the student's name if he has the higher % than the current Candidate,, Finally, the Student with the Highest mark will be in the static variable display that value.
This would be a better solution if you don't want the list of all Candidates % and also have a Permission to use a static variable.
精彩评论