← run

he-003

1.000
1/1 tests· basic
Challenge · difficulty 2/5
# HumanEval/3

Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level.

```python
from typing import List


def below_zero(operations: List[int]) -> bool:
    """ You're given a list of deposit and withdrawal operations on a bank account that starts with
    zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
    at that point function should return True. Otherwise it should return False.
    >>> below_zero([1, 2, 3])
    False
    >>> below_zero([1, 2, -4, 5])
    True
    """
```

<!-- imported from OpenAI HumanEval (HumanEval/3) -->
tests/test_he_003.py
# Auto-generated from OpenAI HumanEval HumanEval/3. Do not edit by hand.
from solution import *  # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import below_zero as candidate

METADATA = {
    'author': 'jt',
    'dataset': 'test'
}


def check(candidate):
    assert candidate([]) == False
    assert candidate([1, 2, -3, 1, 2, -3]) == False
    assert candidate([1, 2, -4, 5, 6]) == True
    assert candidate([1, -1, 2, -2, 5, -5, 4, -4]) == False
    assert candidate([1, -1, 2, -2, 5, -5, 4, -5]) == True
    assert candidate([1, -2, 2, -2, 5, -5, 4, -4]) == True


def test_he_003():
    check(candidate)
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
from typing import List


def below_zero(operations: List[int]) -> bool:
    """ You're given a list of deposit and withdrawal operations on a bank account that starts with
    zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
    at that point function should return True. Otherwise it should return False.
    >>> below_zero([1, 2, 3])
    False
    >>> below_zero([1, 2, -4, 5])
    True
    """
    balance = 0
    for op in operations:
        balance += op
        if balance < 0:
            return True
    return False
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The task is to implement a function `below_zero` that takes a list of operations (deposits and withdrawals) and returns `True` if at any point the account balance falls below zero, starting from zero balance.

Let me trace through the examples:
1. `below_zero([1, 2, 3])`: 
   - Start: 0
   - After 1: 1 (not below zero)
   - After 2: 3 (not below zero)
   - After 3: 6 (not below zero)
   - Return: False

2. `below_zero([1, 2, -4, 5])`:
   - Start: 0
   - After 1: 1 (not below zero)
   - After 2: 3 (not below zero)
   - After -4: -1 (below zero!)
   - Return: True

So I need to:
1. Start with balance = 0
2. Iterate through each operation
3. Add the operation to the balance
4. Check if balance < 0 at any point
5. Return True if it ever goes below zero, False otherwise

Let me write the solution:

```python
from typing import List


def below_zero(operations: List[int]) -> bool:
    balance = 0
    for op in operations:
        balance += op
        if balance < 0:
            return True
    return False
```

This should work correctly for the given test cases.