0

The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer, represented as an empty string (in C the space character is used).

If the score < 0, return 0.

The first input array is the key to the correct answers to an exam, like ["a", "a", "b", "d"]. The second one contains a student's submitted answers.

This is my code, however is not working!! can someone assist me?

def check_exam(arr1,arr2):
    res = 0
    
    for i in arr1:
        for j in arr2:
            if [i] == [j]:
                res+=4
            elif i=='':
                res==0
            elif j=='':
                res==0
            else:
                res+=-1
    return res

Example:

checkExam(["a", "a", "b", "b"], ["a", "c", "b", "d"])

→ 6

ruddy simonpour
  • 133
  • 1
  • 1
  • 8

1 Answers1

0
def check_exam(arr1,arr2):
    res = 0
    
    for i,j in zip(arr1,arr2):
        if i==j and i!="":
            res += 4
        elif i!=j:
            res +=-1
        else:
            res +=0
    return res
check_exam(["a", "a", "b", "b"], ["a", "c", "b", "d"])

#output
6

for if checks if both numbers are equal and none of them is "" (empty).

elif checks that they are different. Else is for the ""==""

ombk
  • 2,036
  • 1
  • 4
  • 16
  • Thanks for helping, but your code does not work for this input: (["b", "c", "b", "a"], ["", "a", "a", "c"]) -> output 0 – ruddy simonpour Dec 03 '20 at 22:49
  • it should be -4 based on ur statement – ombk Dec 03 '20 at 22:54
  • The first array is correct answers and second array is submitted answers by student. So there's no correct answer and we have one blank answer. 0 for blank answers. Based on statement, the answer should be 0. – ruddy simonpour Dec 03 '20 at 22:59
  • ruddy, re read what you wrote in your statement, you are explaining ur exercise a bit too late... dont post questions thinking that people will predict what you mean... just make it clear from the beginning – ombk Dec 03 '20 at 23:00
  • My bad, i'm totally sorry! I've forgot to add that statement as well! – ruddy simonpour Dec 03 '20 at 23:19