PracticeSimulation

Combo Level Score

Easy/MediumSimulationaccumulationstate trackinginvariants Time not estimated Not started

Context

In a training challenge, each level of a combo grants a smaller bonus than the previous one. Level 0 is worth 1 point, level 1 is worth 1/2 point, level 2 is worth 1/4 point, and so on. The game wants the total bonus from level 0 through a supplied highest level.

Problem

Implement a function that returns the total combo bonus for every level from 0 through highest_level. The bonus for level n is (1/2)^n. Define the recursive calculation so that a state below level 0 returns the neutral total 0. Each nonterminal state must include its own bonus and combine it with the result for the immediately smaller level. A highest_level of -1 therefore represents no levels and has total 0.

Examples

Example 1
Input: highest_level = -1
Output: 0
Explanation: For highest_level = -1, the function returns the neutral total 0.0.
Example 2
Input: highest_level = 0
Output: 1
Explanation: For highest_level = 0, the result is 1.0 because level 0 has contribution 1.
Example 3
Input: highest_level = 3
Output: 1.875
Explanation: For highest_level = 3, the executed result is 1.875.

Constraints

  • -1 <= highest_level <= 60
  • Types: highest_level is int; result is float

Function signature

def combo_level_score(highest_level: int) -> float
highest_level int
The highest combo level whose bonus should be included.
returns float
The sum of the bonuses for levels 0 through highest_level, or 0.0 when the level is below 0.

Adapted from MBPP problem task_738 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • The level contribution uses a fixed ratio of one half.
  • The terminating state is below the lowest valid level.
Approved · c043r1-abs_138_v213 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1