Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.6 KiB
Raw Permalink Blame History

id, title, challengeType, dashedName
id title challengeType dashedName
694596b0585c11170ac7c7fd Challenge 165: Class Average 29 challenge-165

--description--

Given an array of exam scores (numbers), return the average score in form of a letter grade according to the following chart:

Average Score Letter Grade
97-100 "A+"
93-96 "A"
90-92 "A-"
87-89 "B+"
83-86 "B"
80-82 "B-"
77-79 "C+"
7376 "C"
70-72 "C-"
67-69 "D+"
63-66 "D"
6062 "D-"
below 60 "F"

Calculate the average by adding all scores in the array and dividing by the total number of scores.

--hints--

get_average_grade([92, 91, 90, 94, 89, 93]) should return "A-".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([92, 91, 90, 94, 89, 93]), "A-")`)
}})

get_average_grade([84, 89, 85, 100, 91, 88, 79]) should return "B+".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([84, 89, 85, 100, 91, 88, 79]), "B+")`)
}})

get_average_grade([63, 69, 65, 66, 71, 64, 65]) should return "D".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([63, 69, 65, 66, 71, 64, 65]), "D")`)
}})

get_average_grade([97, 98, 99, 100, 96, 97, 98, 99, 100]) should return "A+".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([97, 98, 99, 100, 96, 97, 98, 99, 100]), "A+")`)
}})

get_average_grade([75, 100, 88, 79, 80, 78, 64, 60]) should return "C+".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([75, 100, 88, 79, 80, 78, 64, 60]), "C+")`)
}})

get_average_grade([45, 48, 50, 52, 100, 54, 56, 58, 59]) should return "F".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_average_grade([45, 48, 50, 52, 100, 54, 56, 58, 59]), "F")`)
}})

--seed--

--seed-contents--

def get_average_grade(scores):

    return scores

--solutions--

def get_average_grade(scores):
    avg = sum(scores) / len(scores)

    if avg >= 97: return "A+"
    if avg >= 93: return "A"
    if avg >= 90: return "A-"

    if avg >= 87: return "B+"
    if avg >= 83: return "B"
    if avg >= 80: return "B-"

    if avg >= 77: return "C+"
    if avg >= 73: return "C"
    if avg >= 70: return "C-"

    if avg >= 67: return "D+"
    if avg >= 63: return "D"
    if avg >= 60: return "D-"

    return "F"