mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
improve max
and min
This commit is contained in:
parent
eaccba8622
commit
b3cbb121a4
@ -16,11 +16,39 @@ def round(x, ndigits=0):
|
|||||||
def abs(x):
|
def abs(x):
|
||||||
return -x if x < 0 else x
|
return -x if x < 0 else x
|
||||||
|
|
||||||
def max(a, b):
|
def max(*args):
|
||||||
return a if a > b else b
|
if len(args) == 0:
|
||||||
|
raise TypeError('max expected 1 arguments, got 0')
|
||||||
|
if len(args) == 1:
|
||||||
|
args = args[0]
|
||||||
|
args = iter(args)
|
||||||
|
res = next(args)
|
||||||
|
if res is StopIteration:
|
||||||
|
raise ValueError('max() arg is an empty sequence')
|
||||||
|
while True:
|
||||||
|
i = next(args)
|
||||||
|
if i is StopIteration:
|
||||||
|
break
|
||||||
|
if i > res:
|
||||||
|
res = i
|
||||||
|
return res
|
||||||
|
|
||||||
def min(a, b):
|
def min(*args):
|
||||||
return a if a < b else b
|
if len(args) == 0:
|
||||||
|
raise TypeError('min expected 1 arguments, got 0')
|
||||||
|
if len(args) == 1:
|
||||||
|
args = args[0]
|
||||||
|
args = iter(args)
|
||||||
|
res = next(args)
|
||||||
|
if res is StopIteration:
|
||||||
|
raise ValueError('min() arg is an empty sequence')
|
||||||
|
while True:
|
||||||
|
i = next(args)
|
||||||
|
if i is StopIteration:
|
||||||
|
break
|
||||||
|
if i < res:
|
||||||
|
res = i
|
||||||
|
return res
|
||||||
|
|
||||||
def all(iterable):
|
def all(iterable):
|
||||||
for i in iterable:
|
for i in iterable:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user