- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
给定两个大范围数组...
A = [0..23, 30..53, 60..83, 90..113]
B = [-Float::INFINITY..13, 25..33, 45..53, 65..73, 85..93]
当我执行 logical conjuction 时...
C = A.mask(B)
那么我期待
describe "Array#mask" do
it{expect(C = A.mask(B)).to eq([0..13, 30..33, 45..53, 65..73, 90..93])}
end
感觉应该是……
C = A & B
=> []
但那是空的because none of the ranges are identical .
这是一个图片示例。
.
我将 Infinity 包括在范围内是因为此问题的解决方案 typically involve converting the Range to an Array or Set .
我的当前解决方案这是我目前通过速度和准确性测试的解决方案。我正在寻找评论和/或建议的改进。第二个测试使用优秀的IceCube gem to generate an array of date ranges .我的掩码方法中有一个隐含的假设,即每个计划中出现的日期范围不重叠。
require 'pry'
require 'rspec'
require 'benchmark'
require 'chronic'
require 'ice_cube'
require 'active_support'
require 'active_support/core_ext/numeric'
require 'active_support/core_ext/date/calculations'
A = [0..23, 30..53, 60..83, 90..113]
B = [-Float::INFINITY..13, 25..33, 45..53, 65..73, 85..93]
class Array
def mask(other)
a_down = self.map{|r| [:a, r.max]}
a_up = self.map{|r| [:a, r.min]}
b_down = other.map{|r| [:b, r.max]}
b_up = other.map{|r| [:b, r.min]}
up = a_up + b_up
down = a_down + b_down
a, b, start, result = false, false, nil, []
ticks = (up + down).sort_by{|i| i[1]}
ticks.each do |tick|
tick[0] == :a ? a = !a : b = !b
result << (start..tick[1]) if !start.nil?
start = a & b ? tick[1] : nil
end
return result
end
end
describe "Array#mask" do
context "simple integer array" do
it{expect(C = A.mask(B)).to eq([0..13, 30..33, 45..53, 65..73, 90..93])}
end
context "larger date ranges from IceCube schedule" do
it "should take less than 0.1 seconds" do
year = Time.now..(Time.now + 52.weeks)
non_premium_schedule = IceCube::Schedule.new(Time.at(0)) do |s|
s.duration = 12.hours
s.add_recurrence_rule IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday, :thursday, :friday).hour_of_day(7).minute_of_hour(0)
end
rota_schedule = IceCube::Schedule.new(Time.at(0)) do |s|
s.duration = 7.hours
s.add_recurrence_rule IceCube::Rule.weekly(2).day(:tuesday).hour_of_day(15).minute_of_hour(30)
end
np = non_premium_schedule.occurrences_between(year.min, year.max).map{|d| d..d+non_premium_schedule.duration}
rt = rota_schedule.occurrences_between(year.min, year.max).map{|d| d..d+rota_schedule.duration}
expect(Benchmark.realtime{np.mask(rt)}).to be < 0.1
end
end
end
你不能用Ruby现有的核心方法来做这件事,感觉很奇怪?我错过了什么吗?我发现自己相当定期地计算范围交叉点。
我还想到,您可以使用相同的方法通过传递单个项目数组来查找两个单个范围之间的交集。例如
[(54..99)].mask[(65..120)]
我意识到我已经回答了我自己的问题,但我想我会把它留在这里作为其他人的引用。
最佳答案
我不确定我是否真的理解你的问题;我对你的 expect
声明有点困惑,我不知道为什么你的数组大小不一样。也就是说,如果你想计算两个范围的交集,我喜欢这个猴子补丁(来自 Ruby: intersection between two ranges ):
class Range
def intersection(other)
return nil if (self.max < other.begin or other.max < self.begin)
[self.begin, other.begin].max..[self.max, other.max].min
end
alias_method :&, :intersection
end
然后你可以做:
A = [0..23, 30..53, 60..83, 0..0, 90..113]
B = [-Float::INFINITY..13, 25..33, 45..53, 65..73, 85..93]
A.zip(B).map { |x, y| x & y }
# => [0..13, 30..33, nil, nil, 90..93]
这似乎是一个合理的结果......
编辑
如果你按照上面的方式对 Range
进行 monkeypatch,然后执行:
# your initial data
A = [0..23, 30..53, 60..83, 90..113]
B = [-Float::INFINITY..13, 25..33, 45..53, 65..73, 85..93]
A.product(B).map {|x, y| x & y }.compact
# => [0..13, 30..33, 45..53, 65..73, 90..93]
您会得到您指定的结果。不知道它如何在性能方面进行比较,而且我不确定排序顺序...
关于ruby - 计算ruby中两个范围(日期)数组的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27899934/
我有一个现有站点,其数据库设计不正确并且包含大量记录,因此我们无法更改数据库结构。 本期数据库主要包含用户、问题、选项、答案4个表。有一组标准的问题和选项,但对于每个用户,每组问题和选项在答案表中都有
有没有办法找出两个 CGPathRef 是否相交。就我而言,所有 CGPath 都有 closePath。 例如,我有两条路径。一条路径是旋转一定角度的矩形,另一条路径是弯曲路径。两条路径的原点会经常
我目前正在使用 JavaFX 研究不同形状之间的边界相交。我想检测两个多边形在它们的点上而不是在它们的边界上的碰撞(即 2 个多边形)。 请参见图 1:不需要的行为和图 2:需要的行为。 是否有任何现
在我的three.js 场景中,我有一些立方体。我想为用户提供用鼠标选择框的可能性。 这是重要的代码(我使用 Three.js 版本 69。): function init() { [...]
我有一个问题。我想将四边形与四边形相交。 int main(){ typedef boost::geometry::model::point_xy TBoostPoint; typedef b
在 MongoDB 中我们找到了交集的方法,但我们也想实现日期范围排除。让我解释一下。 我们有每个支持团队的每日轮值表。每个支持团队可以每 15 分钟预订一次,持续 5-25 分钟(大约)。每个团队有
目录 1、列表求并集 1. union_by 2、列表求交集 1. intersection_by
我有以下查询: select id from t1 intersect select id from t2 intersect select id from t3 id 在某些表中可能不是唯一的,所以
需要完成此实现才能使用 UseSet 类。不确定我所实现的是否100%正确。 但是我需要 Union 和 SysDiff 方面的帮助。 public class Set { private Ar
我的程序打印主构造函数,但不返回 3 个底部函数,而是返回其编译结果。我哪里出错了? import java.util.*; public class Main { public static v
我正在尝试找到两个不同列表列表的交集。换句话说,找出 list1 中的所有列表是否与列表 2 中的任何列表相交。 列表1: [[1,4],[1,6],[6,8],[8,10]] 列表2: [[],
我正在尝试从 2 个已知 BST 的交集创建一个新的 BST。我在第二种情况下的 intersect2 方法中收到 NullPointerException,位于“cur3.item.set_acco
这个问题已经有答案了: self referential struct definition? (9 个回答) 已关闭 7 年前。 我有一个脚本 a.h #include b.h type
我在 user_profile 表上运行搜索,其中包含单个索引和复合索引: SELECT ••• FROM user_profile up JOIN auth_user
我正在尝试为(公寓)列表创建过滤器,通过 apartsments_features 表与 apartment features 建立多对多关系。 我只想包括具有所有某些功能(在表格上标记为"is")的
我想从两个给定的嵌套列表中创建一个新的嵌套列表(每个列表中都有唯一的项目),以便新的嵌套列表是两个列表的最大公共(public)交集。 一个例子希望能帮助阐明我的问题: old1 = [[1,
我在 Django 中有两个模型,我不确定如何编写它们(是否有一个抽象模型并继承等等......或者有两个不同的模型)但通常我有两种类型的对象 A 和 B。 A 和 B 完全相同,因为它们只是项目。它
我有一个像这样的数组 arrays = [ ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'g'], ['a',
我正在通过向 Mario Zechner 的开源跳线游戏添加更多功能来学习 libgdx。我正在尝试制作一些带有角度的平台并遇到旋转矩形的碰撞检测问题。 我关注了this解决方案并使用多边形和我的矩形
我有一个包含对象的数组: let data = [[{a:0}, {b:1}], [{a:1}, {b:1}]] 现在我想制作一个 lodash intersection这两个数组,返回 [{b:1}
我是一名优秀的程序员,十分优秀!