首页 > 解决方案 > 如何使用python测试一个数字是否可以击中一系列数字X次?

问题描述

如何使用 python 执行以下操作?对于固定周期的时间序列数据,如果有可能有任何水平线(固定价格)与紫色线(一系列价格)交叉 4 次,True则返回如下图所示。

在此处输入图像描述

如果不可能有任何水平线(固定价格)与紫色线(一系列价格)交叉 4 次,False则返回如下图所示。谢谢。

在此处输入图像描述

标签: pythonnumpytime-seriesseries

解决方案


如果您可以用数学方法描述紫色线,那么您需要找到一个 y = f(x),其中对于给定的 y,您有四个或更多令人满意的 x。

现在让我们用蛮力方法来做:

max_y = 10.0 # the biggest y the purple line has
min_y = 5.0 # the smallest y purple line has
min_x = 0 # the x the purple line starts
max_x = 100 # the x the purple line ends
delta = 0.01 # the step value for testing every line

def in_purple(x, y):
    # returns if the point (x, y) is in the purple line
    pass

for y in range(min_y, max_y + delta, delta):
    counter = 0
    for x in range(min_x, max_x + delta, delta):
        if in_purple(x, y):
            counter += 1
    if counter >= 4:
        print(y) # prints the y where you have 4 or more coincidences
        break

推荐阅读