← Back to challenges

Hold Your Breath!

PythonHardconditionsarraysnumbersloopsgames

Instructions

You will be given a list of numbers which represent your character's altitude above sea level at regular intervals:

  • Positive numbers represent height above the water.
  • 0 is sea level.
  • Negative numbers represent depth below the water's surface.

Create a function which returns whether your character survives their unsupervised diving experience, given a list of integers.

  1. Your character starts with a breath meter of 10, which is the maximum. When diving underwater, your breath meter decreases by 2 per item in the array. Watch out! If your breath diminishes to 0, your character dies!

  2. To prevent this, you can replenish your breath by 4 (up to the maximum of 10) for each item in the array where you are at or above sea level.

  3. Your function should return True if your character survives, and False if not.

Worked Example

diving_minigame([-5, -15, -4, 0, 5]) ➞ True

# Breath meter starts at 10.
# -5 is below water, so breath meter decreases to 8.
# -15 is below water, so breath meter decreases to 6.
# -4 is below water, so breath meter decreases to 4.
# 0 is at sea level, so breath meter increases to 8.
# 5 is above sea level and breath meter is capped at 10 (would've been 12 otherwise).
# Character survives!

Examples

diving_minigame([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) ➞ True

diving_minigame([-3, -6, -2, -6, -2]) ➞ False

diving_minigame([2, 1, 2, 1, -3, -4, -5, -3, -4]) ➞ False

Notes

  • Lists may be of any length.
  • All lists are valid.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Find the Highest Integer in the List Using Recursion