Saturday, February 26, 2022

Check the exam Python Codewars

Task:

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.

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.

For example:

checkExam(["a", "a", "b", "b"], ["a", "c", "b", "d"]) → 6
checkExam(["a", "a", "c", "b"], ["a", "a", "b",  ""]) → 7
checkExam(["a", "a", "b", "c"], ["a", "a", "b", "c"]) → 16
checkExam(["b", "c", "b", "a"], ["",  "a", "a", "c"]) → 0

Solution:


def check_exam(arr1,arr2):
    total = 0
    if len(arr1) != len(arr2):
        return total
    for i in range(len(arr1)):
        if arr1[i] == arr2[i]:
            total += 4
        elif arr2[i] == '':
            total += 0
        else:
            total -= 1
    return total if total >= 0 else 0

print(check_exam(["a", "a", "c", "b"], ["", "c", "b", "d"]))

Training on Check the exam | Codewars



Related Posts


No comments :

Post a Comment