locked
Is there an easier way RRS feed

  • Question

  • User-1274246664 posted

    Thought I would share this code and ask for suggestions to make this better.

    I am grading a quiz and finding what answer(s) were missed.  I need to display the question number and the missed answer in lower case as the result.

    I have the following:

    two sting on is the answers the other is a student selected values there are 54 elements in each string I want to compare the Answer key to the quiz element by element  starting at the 4th element to the  end. 

    The results should be the quiz (answered value) and the position  or quiz question number so the result  should look  like this  "4x,5a,6b,13b,23b

    [position][Quiz Missed Question]

    my code is listed below however I was wondering if LINQ .except or .union  or .intersect would be a lot easier, any suggestions?

    void Main()
    {
        var Answer = "17519,  ,  , ,A,B,D,A,D,B,A,C,B,A,C,D,B,D,B,D,D,B,A,D,D,B,B,A,B,A,D,C,B,C,C,B,A,A,C,A,D,B,C,C,A,D,A,D,A,B,B,B,A,B".Split(',').ToArray();
        var Quiz   = "17519,Test,Person,middle,x,A,B,A,D,B,A,C,B,B,C,D,B,D,B,D,D,B,A,B,D,B,B,A,B,A,D,C,B,C,C,B,A,A,C,A,D,B,C,C,A,D,A,D,A,B,B,B,A,B".Split(',').ToArray();
    	
    	var MissedQuestionList = gradeExam(Answer,Quiz);
    	MissedQuestionList.Dump();
    }
    
    List<string> gradeExam( string[] answer, string[] quiz)
    {
    	List<string> missedQuestions = new List<string>();
    	
    	try
    	{	        
    		for( int x = 4; x<=answer.Count()-1;x++)
    		{
    			if(answer[x].ToLower() != quiz[x].ToLower())
    			{
    				missedQuestions.Add( String.Format("{0}{1},",x, quiz[x].ToLower()));
    			}
    		}
    		
    	return(missedQuestions);
    	}
    	catch (Exception ex)
    	{
    		throw;
    	}	
    	
    }

    Wednesday, December 20, 2017 11:55 PM

Answers

  • User2103319870 posted

    I was wondering if LINQ .except or .union  or .intersect would be a lot easier, any suggestions

    You can try with below code. 

     var Answer = "17519,  ,  , ,A,B,D,A,D,B,A,C,B,A,C,D,B,D,B,D,D,B,A,D,D,B,B,A,B,A,D,C,B,C,C,B,A,A,C,A,D,B,C,C,A,D,A,D,A,B,B,B,A,B".Split(',').ToArray();
                var Quiz = "17519,Test,Person,middle,x,A,B,A,D,B,A,C,B,B,C,D,B,D,B,D,D,B,A,B,D,B,B,A,B,A,D,C,B,C,C,B,A,A,C,A,D,B,C,C,A,D,A,D,A,B,B,B,A,B".Split(',').ToArray();
    
                List<string> differentvalues = Answer.Select((item, index) => new { item, index })
                               .Where(x => x.item != Quiz[x.index])
                               .Select(x => String.Format("{0}{1},", x.index, Quiz[x.index].ToLower()))
                               .ToList();
                //Remove first four values and start with 4th index
                differentvalues.RemoveRange(0, 3);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 22, 2017 1:30 AM