Last active
March 18, 2021 15:12
-
-
Save Diapolo10/1b17a31e4fed36d7dc8cffa27ef9446c to your computer and use it in GitHub Desktop.
Python min-function example implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from typing import Callable, Iterable, T | |
def my_min(iterable: Iterable[T], key: Callable[[T, ...], Any]=lambda x: x) -> T: | |
iterator = iter(iterable) | |
try: | |
lowest = key(next(iterator)) | |
except StopIteration: | |
raise ValueError("Iterable is empty") | |
for value in iterator: | |
value = key(value) | |
if lowest > value: | |
lowest = value | |
return lowest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment