python - 如何使用python测试一个数字是否可以击中一系列数字X次?
问题描述
如何使用 python 执行以下操作?对于固定周期的时间序列数据,如果有可能有任何水平线(固定价格)与紫色线(一系列价格)交叉 4 次,True
则返回如下图所示。
如果不可能有任何水平线(固定价格)与紫色线(一系列价格)交叉 4 次,False
则返回如下图所示。谢谢。
解决方案
如果您可以用数学方法描述紫色线,那么您需要找到一个 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
推荐阅读
- python - Python Selenium 转换为 Powershell
- r - 正则表达式从感兴趣的单词中提取由字符串分隔的数字及其度量单位
- android - 关闭 AlertDialog.Builder
- jquery - 航点未触发
- maven - 马文
标记奇怪的行为 - javascript - 在带有十字符号的文本框中搜索结果
- css - CSS 内联工作,但不是来自样式表
- javascript - D3.JS 中的可折叠图:多个根点
- unit-testing - vue.js unit:test w test-utils 和 Jest:我如何在方法中测试 - window.open()?
- sql-server - 如何在 SQL 中将五个表与另一个依赖项连接起来