## Sunday, May 31, 2009

### Problem:-

• #### Question.txt have question in the below format

• • Taking example of the first line this file says that for question no 28 the answer is 2,similarly for question no 89 the answer is 1
• Now we also have an answers.txt file in the below given format.
• • Now the simple puzzle to solve is that in answers.txt file each question answers are given they have 4 options as a1,a2,a3,a4.For example if the answer to question no 28 in questions.txt was given as 2 then in answers.txt file for questions[q28] [a2] we have to check its boolean value.If its true then a2 or 2 is one of the probable answers but we have to check all other answers as well because if any other is false then that also belongs to the set of answers.
• So if for q28 a2 is true then in the finalanswers.txt file the answer set should contain 2, and if other options in answers.txt ie 1,3,4 any of them was false then that also should be there as an answer.

Solution:-

Solution is very simple but playing with string inside the text files is little messy.Also there can be various other alternative implementations to this.

```public class AnswerText
{
public string A1 { get; set; }
public string A2 { get; set; }
public string A3 { get; set; }
public string A4 { get; set; }

public string CorrectAnswers { get; set; }
public int QuestionNo { get; set; }

}
public class QuestionText
{
public int QNo { get; set; }
public int Ans { get; set; }
}
{
public string FinalAns { get; set; }
}```

AnswerText contains A1,A2,A3,A4 as properties to store the values as extracted from the text file

QuestionText contains to properties Qno and Ans to store the correct ans and the question no which we will match against the list of answertext object.

```public static List<QuestionText> ReadQuestions()
{
String line;
var questionList = new List<int>();
{
string[] tempLine = line.Split(new char[] { '-' });
int q, a;
if (int.TryParse(tempLine, out q))
if (int.TryParse(tempLine, out a))
}
var questionTextList = new List<QuestionText>();
var j = 0;
foreach (int q in questionList)
{
var question = new QuestionText { QNo = q, Ans = correctAnswer[j]};
j++;
}
return questionTextList;
}
}```
ReadQustions Function returns a list of QuestionText object.

```public static List<AnswerText> ReadAnswers()
{
string line;
int i = 1;

{
if (!string.IsNullOrEmpty(line))
{
switch (i)
{
case 1:
i++;
break;
case 2:
i++;
break;
case 3:
i++;
break;
case 4:
i++;
break;
case 5:
i++;
break;
case 6:
i++;
break;
case 7:
i++;
break;
case 8:
i = 1;
break;
}
}

}
}```

ReadAnswers returns a list of AnswerText objects.Switch case statement contains 8 cases as the pattern of the text file is like this as in the text file each question has an option line and one answer line i.e for each option we have 2 lines and thus in total 8 lines thus total 8 case statements.

```public static FinalAnswer CreateAns(int ans, AnswerText ansObj)
{
finalans.FinalAns = String.Format("QNo-{0}-ans-", ansObj.QuestionNo);
switch (ans)
{
case 1:
if (ansObj.A1 == "true")
finalans.FinalAns += "1,";
if (ansObj.A2 == "false")
finalans.FinalAns += "2,";
if (ansObj.A3 == "false")
finalans.FinalAns += "3,";
if (ansObj.A4 == "false")
finalans.FinalAns += "4,";
break;
case 2:
if (ansObj.A1 == "false")
finalans.FinalAns += "1,";
if (ansObj.A2 == "true")
finalans.FinalAns += "2,";
if (ansObj.A3 == "false")
finalans.FinalAns += "3,";
if (ansObj.A4 == "false")
finalans.FinalAns += "4,";
break;
case 3:
if (ansObj.A1 == "false")
finalans.FinalAns += "1,";
if (ansObj.A2 == "false")
finalans.FinalAns += "2,";
if (ansObj.A3 == "true")
finalans.FinalAns += "3,";
if (ansObj.A4 == "false")
finalans.FinalAns += "4,";
break;
case 4:
if (ansObj.A1 == "false")
finalans.FinalAns += "1,";
if (ansObj.A2 == "false")
finalans.FinalAns += "2,";
if (ansObj.A3 == "false")
finalans.FinalAns += "3,";
if (ansObj.A4 == "true")
finalans.FinalAns += "4,";
break;
}
return finalans;
}```

CreateAns takes two parameters one is the ans which is of type int , it is the answer given in the question.txt file and the other parameter is the AnswerText object from which we will get the question no and value of other options and here only we will match whether the given answer in the questions.txt file is valid or not and also in this we will check for other valid answers.And finally return a finalans string.

```public static List<FinalAnswer> CalculateAnswers(List<QuestionText> qtl, List<AnswerText> atl, List<FinalAnswer> ftl)
{
foreach (QuestionText qObj in qtl)
{
var qno = qObj.QNo;
var ansObj = atl.Find(ans => ans.QuestionNo == qno);
if(ansObj != null)
{
var finalAns = CreateAns(qObj.Ans, ansObj);
}
}
return ftl;
}```