Created
November 7, 2018 09:32
-
-
Save richardARPANET/d930d10cacc0273c8813607e3448802a to your computer and use it in GitHub Desktop.
impl_min_max_functions_in_python.py
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
""" | |
Python min/max functions (O(n) complexity). | |
""" | |
mylist = [1, 2, 0, 3] | |
def test_min(): | |
assert min(mylist) == min_(mylist) | |
def test_max(): | |
assert max(mylist) == max_(mylist) | |
def min_(list_): | |
min_val = None | |
for item in list_: | |
if min_val is None: | |
min_val = item | |
if item < min_val: | |
min_val = item | |
return min_val | |
def max_(list_): | |
max_val = None | |
for item in list_: | |
if max_val is None: | |
max_val = item | |
if item > max_val: | |
max_val = item | |
return max_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment