0

I have an issue and I can't for the life of me get anything to return past ()

exam_solution = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\
           'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']

student_answers = ['B', 'D', 'B', 'A', 'C', 'A', 'A', 'A', 'C', 'D', 'B', 'C',\
           'D', 'B', 'D', 'C', 'C', 'B', 'D', 'A']

I need to compare the 2 lists and append the differences into questions_missed = [] I haven't found anything remotely close to working. Any help would be appreciated

edit: In python been stroking out over it all day.

  • 1
    What language are you using? – Mike Manfrin Nov 25 '13 at 04:09
  • Also, what do you want questions_missed to look like? Like [true, true, false...]? – Mike Manfrin Nov 25 '13 at 04:10
  • Python sorry, was in the process of having an episode – user3029955 Nov 25 '13 at 04:10
  • ## Your output should look like following: ## ##Congratulations!! You passed the exam ##You answered 17 questions correctly and 3 questions incorrectly ##The numbers of the questions you answered incorrectly are: 3 7 14 ##press enter to continue – user3029955 Nov 25 '13 at 04:11
  • I can not for the life of me find a good reference in my book or otherwise. – user3029955 Nov 25 '13 at 04:12
  • 1
    @user3029955: no, the question wasn't how you wanted to display the output, it was what format you wanted `questions_missed` in. A list of booleans? A list of the indices of the questions which were wrong? Etc. – DSM Nov 25 '13 at 04:13
  • There is this similar question [this might help](http://stackoverflow.com/questions/13732312/differences-between-two-arrays) – AMY Nov 25 '13 at 04:14

7 Answers7

1

use python list comprehensions to check list diff:

print [(index, i, j) for index, (i, j) in enumerate(zip(exam_solution, student_answers)) if i != j]
[(2, 'A', 'B'), (6, 'B', 'A'), (13, 'A', 'B')]
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42
0

You can modify this solution to fit your needs:

exam_solution = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
student_answers = ['B', 'D', 'B', 'A', 'C', 'A', 'A', 'A', 'C', 'D', 'B', 'C', 'D', 'B', 'D', 'C', 'C', 'B', 'D', 'A']

results = []

correct = 0
incorrect = 0

index = 0
while index < len(student_answers):
    if student_answers[index] == exam_solution[index]:
        results.append(True)
        correct += 1
    else:
        results.append(False)
        incorrect += 1

    index += 1

print("You answered " + correct + " questions correctly and " + incorrect + " questions incorrectly.")
joshreesjones
  • 1,934
  • 5
  • 24
  • 42
  • Much appreciated that gives me a great start point, thank you. – user3029955 Nov 25 '13 at 04:18
  • 2
    Iterating by explicitly incrementing an index is an antipattern in Python. The loop is simply `results = [sol == ans for sol, ans in zip(exam_solution, student_answers)]`. – DSM Nov 25 '13 at 04:21
0

Using list comprehensions:

[x for i, x in enumerate(exam_solution) if exam_solution[i] != student_answers[i] ]

['A', 'B', 'A']

Mark Simpson
  • 2,344
  • 2
  • 23
  • 31
0

Assuming you want an output in common English like this -

Question 3 A != B
Question 7 B != A
Question 14 A != B

You could try -

from array import *

exam_solution = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\
       'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
student_answers = ['B', 'D', 'B', 'A', 'C', 'A', 'A', 'A', 'C', 'D', 'B', 'C',\
       'D', 'B', 'D', 'C', 'C', 'B', 'D', 'A']
questions_missed = []
count = 0
for answer in exam_solution:
    if (answer != student_answers[count]):
            questions_missed.append(count)
    count = count + 1

for question in questions_missed:
    print str.format("Question {0} {1} != {2}", question+1,
            exam_solution[question], student_answers[question]);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Actually thanks a lot, that gave me an idea for part of the output I needed. Very much appreciated this assignment was out of left field. – user3029955 Nov 25 '13 at 04:29
0

Using the KISS design principle, that's how I'd do it:

exam_solution = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\
       'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']

student_answers = ['B', 'D', 'B', 'A', 'C', 'A', 'A', 'A', 'C', 'D', 'B', 'C',\
       'D', 'B', 'D', 'C', 'C', 'B', 'D', 'A']

questions_missed = []

for index in range(len(exam_solution)):
    # this assumes exam_solution and student_answers have the same size!
    if exam_solution[index] != student_answers[index]:
        questions_missed.append(index)

print (questions_missed)

And the output is:

[2, 6, 13]
0
L = [(a, b) for a, b in zip(exam_solution, student_answers) if a != b]
print(L)

Mybe you can use zip function.

The output is: [('A', 'B'), ('B', 'A'), ('A', 'B')]

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
0

Solution (use set):

>>> def result(solution, answers):
...     return set(str(n)+s for n, s in enumerate(solution)) - \ 
...            set(str(n)+r for n, r in enumerate(answers))
...
>>> result(exam_solution, student_answers)
... set(['6B', '13A', '2A'])
>>>

The result are wrong responses (you can convert to list list(result(student_answers)).

SmartElectron
  • 1,331
  • 1
  • 16
  • 17