Problem 183367 · medium · Level 01 Prerequisites & Setup

What Do I Need on the Last Tests?

mean · totals · integer division · rounding up

A student knows her marks so far and how many tests are left. Every remaining test is marked in whole marks from 0 to top. She wants the smallest whole mark she would need to score on each of the remaining tests (the same mark on every one) for her mean over all tests to be at least target.

Write mark_needed(marks, remaining, target, top) that returns that mark. If she already reaches the target even with 0 on every remaining test, return 0. If even top on every remaining test is not enough, return None.

Examples

Input:  marks = [62, 58, 71], remaining = 2, target = 65, top = 100
Output: 67
Explanation: 5 tests with a mean of 65 need 325 marks; she has 191,
so the last two tests need 134 between them, 67 each.

Input:  marks = [80, 90], remaining = 3, target = 71, top = 100
Output: 62
Explanation: 355 marks are needed and 170 are banked, so 185 must come from three tests.
61 each gives 183, two short; 62 each gives 186, enough.

Input:  marks = [40, 45], remaining = 2, target = 90, top = 100
Output: None

Constraints

  • 0 <= len(marks) <= 10**5, every mark a whole number from 0 to top
  • 1 <= remaining <= 1000 and 1 <= top <= 1000
  • target is a whole number from 0 to top

Goals

  • Turn a target mean into a target total
  • Round up correctly with whole-number arithmetic
  • Handle targets that are already safe or out of reach
Starting Python…