【问题标题】:Any way to replicate the OR operator with only ANDs?有什么方法可以只用 AND 复制 OR 运算符?
【发布时间】:2017-08-18 16:27:18
【问题描述】:

所以我试图找到一种方法来允许在我的应用程序的前端进行 OR 操作,然后在后端服务器上转换条件,然后该服务器将与 Intrinio(财务数据 API)对话。我尝试使用的端点(以节省 api 调用信用)仅允许 AND 运算符,但我希望有某种解决方法。

这是我尝试使用的端点:http://docs.intrinio.com/#securities-search-screener

这只是一个示例条件,我将发送到我的服务器进行翻译:

open_price > 10 && (current_volume > 1000000 || current_volume > average_volume)

附言 open_price、current_volume 和 average_volume 都是 Intrinio 生态系统中使用的标签

我可能还应该提到他们没有 NOTs

我恳请你们在发表评论之前先查看链接...

【问题讨论】:

标签: c# logic set-theory


【解决方案1】:

一般来说,你不能。

在此 API 中,您只能否定原子条件,将 ~gt~ 更改为 ~lte~ 等,例如。 G。将open_price~gt~10 更改为open_price~lte~10

但是你不能在这个 API 中否定非原子条件,特别是你不能使用德摩根定律。

n 个变量的布尔函数只有 3n (+1) 个布尔函数,可以使用合取和原子否定来表示。 n 个变量的布尔函数总数为 22ⁿ。顺便说一句,它们都可以使用合取和非原子否定来表达。因此,存在无法使用合取和原子否定来表达的布尔函数。

您应该发出两个或更多请求,然后在 API 客户端上合并结果。 有几种方法可以做到这一点,它们在请求数量、检索的记录总数(某些记录可能被检索多次)和所需的 API 调用信用(它等于条件的数量)方面有所不同。


假设原始数据如下所示(让我们跳过open_price 属性)。

{
  {
   "identifier": "A-OK",
   "current_volume": 1000002, "average_volume": 1000001
  },
  {
   "identifier": "B-OK",
   "current_volume": 1000001, "average_volume":  999999
  },
  {
   "identifier": "D-OK",
   "current_volume":  999999, "average_volume":  999998
  },
  {
   "identifier": "E-OK",
   "current_volume": 1000001, "average_volume": 1000002
  },
  {
   "identifier": "G-NO",
   "current_volume":  999999, "average_volume": 1000001
  },
  {
   "identifier": "H-NO",
   "current_volume":  999998, "average_volume":  999999
  }
}

您需要检索标识符为 A-OKB-OKD-OKE-OK 的记录。

方法一(两个请求,结果集不相交)

current_volume~gt~1000000
current_volume~gt~average_volume

如您所见,我们只是在客户端上执行析取,而不是在服务器上...

方法2(三个请求,结果集不相交)

current_volume~gt~1000000,current_volume~gt~average_volume
current_volume~gt~1000000,current_volume~lte~average_volume
current_volume~lte~1000000,current_volume~gt~average_volume

在这种方法中,我们依赖于这个事实:A||B 等于 (A && B) ^^ (A && !B) ^^ (!A && B)

方法3(两个请求,结果集不相交)

current_volume~gt~1000000
current_volume~lte~1000000,current_volume~gt~average_volume

在这种方法中,我们依赖于这个事实:A||B 等于 A ^^ (!A && B)

方法4(两个请求,结果集不相交)

在您的特定情况下,存在一种依赖于实数顺序关系的传递性的方法。

average_volume~gt~1000000,current_volume~gt~1000000
average_volume~lte~1000000,current_volume~gt~average_volume

但是,这种方法在 API 调用信用方面并没有带来多少利润。

比较

+----------+----------+-----------+--------------+----------+
| Approach | Number of| Records   | Resultsets   | Total    |
|          | requests | retrieved | disjointness | credits  |
+----------+----------+-----------+--------------+----------+
|  # 1     |    2     |   6 (3+3) |    no        |    2     |
|  # 2     |    3     |   4       |    yes       |    6     |
|  # 3     |    2     |   4       |    yes       |    3     |
|  # 4     |    2     |   4       |    yes       |    4     |
+----------+----------+-----------+--------------+----------+

【讨论】:

    【解决方案2】:

    DeMorgan's lawsa && b等价于!(!a || !b)

    你可以把and想象成

    "a b都是true"

    或等价的

    ab 都不是false

    【讨论】:

    • @G3tinmybelly 那么你就完蛋了
    • 哈哈哈,原来如此
    【解决方案3】:

    先检查average_volume和1000000中哪个值较大,然后根据结果选择条件进行检查:

    if(average_volume <= 1000000) 
        //use this condition: open_price > 10 && current_volume > 1000000
    else 
        //use this condition: open_price > 10 && current_volume > average_volume
    

    【讨论】:

    • 他们有哪些运营商?
    • 等于、大于、大于等于、小于、小于等于、包含
    • 可以用开关吗?
    • 喜欢 switch 语句?
    • 是的,还是必须是 if 语句
    猜你喜欢
    • 1970-01-01
    • 2021-11-08
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多