logoalt Hacker News

wodenokototoday at 10:49 AM2 repliesview on HN

Why are `min(r)` and `max(r)` for range objects o(n) ?

I thought min and max where constants stored in the object. Basically you are just asking for one of the parameters it was created with.


Replies

gpugregtoday at 11:19 AM

Because the use case is very niche and nobody optimized it yet.

https://github.com/python/cpython/issues/135824#issuecomment...

`x in range(n)` is already optimized, but that was easier since the `__contains__` method already existed, but an equivalent `__min__` or `__max__` does not.

dist-epochtoday at 12:30 PM

> Basically you are just asking for one of the parameters it was created with.

See, you already made a mistake:

    >>> min(range(10, 1, -3))
    4
4 is neither the min or max of the range (their actual names are start and stop), and notice how the max is the first argument and the min is the second argument

Of course, the actual implementation of constant time min/max on range would be trivial.