AT
Size: a a a
AT
𝕬
КБ
𝕬
КБ
ᅠ
𝕬
БГ
БГ
𝕬
import operator
prices = [5, 11, 3, 50, 60, 90]
transaction_number = 2
def neighbours(arr, i):
res = []
if i < len(arr) - 1:
res.append(arr[i + 1])
if i > 0:
res.append(arr[i - 1])
return res
def _extremums(func, name):
def inner(arr):
return {(name, i, x) for i, x in enumerate(arr) if all(func(x, y) for y in neighbours(arr, i))}
return inner
lows = _extremums(operator.lt, 'low')
highs = _extremums(operator.gt, 'high')
extremums = sorted(lows(prices) | highs(prices), key=lambda x: x[1])
print(sum(x[1] for x in sorted(((i, abs(x[2] - y[2])) for i, (x, y) in enumerate(zip(extremums, extremums[1:]))), key=lambda x: x[1])[-transaction_number:]))
K
𝕬
ᅠ
import operator
prices = [5, 11, 3, 50, 60, 90]
transaction_number = 2
def neighbours(arr, i):
res = []
if i < len(arr) - 1:
res.append(arr[i + 1])
if i > 0:
res.append(arr[i - 1])
return res
def _extremums(func, name):
def inner(arr):
return {(name, i, x) for i, x in enumerate(arr) if all(func(x, y) for y in neighbours(arr, i))}
return inner
lows = _extremums(operator.lt, 'low')
highs = _extremums(operator.gt, 'high')
extremums = sorted(lows(prices) | highs(prices), key=lambda x: x[1])
print(sum(x[1] for x in sorted(((i, abs(x[2] - y[2])) for i, (x, y) in enumerate(zip(extremums, extremums[1:]))), key=lambda x: x[1])[-transaction_number:]))
K
¯
import operator
prices = [5, 11, 3, 50, 60, 90]
transaction_number = 2
def neighbours(arr, i):
res = []
if i < len(arr) - 1:
res.append(arr[i + 1])
if i > 0:
res.append(arr[i - 1])
return res
def _extremums(func, name):
def inner(arr):
return {(name, i, x) for i, x in enumerate(arr) if all(func(x, y) for y in neighbours(arr, i))}
return inner
lows = _extremums(operator.lt, 'low')
highs = _extremums(operator.gt, 'high')
extremums = sorted(lows(prices) | highs(prices), key=lambda x: x[1])
print(sum(x[1] for x in sorted(((i, abs(x[2] - y[2])) for i, (x, y) in enumerate(zip(extremums, extremums[1:]))), key=lambda x: x[1])[-transaction_number:]))
K
КБ
𝕬
ᅠ