나는 이렇게 학습한다/Algorithm & SQL

Check the exam

daco2020 2022. 7. 24. 01:42
반응형

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):
    point = 0
    for i, j in zip(arr1, arr2):
        if j == "":
            pass
        elif i == j:
            point += 4
        else:
            point -= 1
    return 0 if point < 0 else point

 

Best Practice:

def check_exam(arr1, arr2):
    return max(0, sum(4 if a == b else -1 for a, b in zip(arr1, arr2) if b))

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Money, Money, Money  (0) 2022.07.26
Calculate BMI  (0) 2022.07.25
What is between?  (0) 2022.07.24
Replace With Alphabet Position  (0) 2022.07.22
Anagram Detection  (0) 2022.07.21