Sobes.tech
Junior — Senior

Rewrite the function using idiomatic Python

livecode

Task condition

The function foo(length: int) should return all even numbers from 0 to length (inclusive), considering the sign of the parameter: if length is negative, the result should also be negative. The implementation should be in a "Python-style" using list comprehension and improved code readability.

def foo(length: int = 10) -> list[int]:
    result: list[int] = []
    is_negative = length < 0
    for i in range(abs(length) + 1):
        if i % 2 == 0:
            result.append(i if not is_negative else -i)
    return result

Rewrite this function using Python features such as list comprehension, ternary expressions, and clear variable names to make the code shorter and more understandable.

Rewrite the function using idiomatic Python - Live… - sobes.tech