How do you check whether a range contains a subset of another range?
Be careful using this with large ranges but this is an elegant way to do it:
(x.to_a & y.to_a).empty?
Check if an array is subset of another array in Ruby
Use sets. Then you can use set.subset?
. Example:
require 'set'
a1 = Set[3,6,4]
a2 = Set[1,2,3,4,5,6,7,8,9]
puts a1.subset?(a2)
Output:
true
See it working online: ideone
Check if two ranges overlap in ruby
Two ranges overlap for a given range A when:
- range B starts within range A,
- range B ends within range A or
- range B starts before range A and ends after range A
Examples:
Range A |-----|
|-----| Case 1
|-----| Case 2
|-| Case 1 + 2
|---------| Case 3
Looking closer the rule is: Two ranges overlap when Range B starts before the range A ends and range B ends after the range A starts.
def ranges_overlap?(range_a, range_b)
range_b.begin <= range_a.end && range_a.begin <= range_b.end
end
How to determine if one array contains all elements of another array
a = [5, 1, 6, 14, 2, 8]
b = [2, 6, 15]
a - b
# => [5, 1, 14, 8]
b - a
# => [15]
(b - a).empty?
# => false
How to check whether a string contains a substring in Ruby
You can use the include?
method:
my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end
How to select array elements in a given range in Ruby?
You can use ranges in the array subscript:
arr[100..200]
Subset a table by whether two columns fall within any reference range in a lookup table in R
If I understand you correctly, you want to find variants which (for at least one range in table2, r) meet at least one of the following conditions:
table1$end
is within rtable1$pos
is within rtable1$pos
andtable2$end
are on opposite sides of r
Below are schematic depictions of all possibilities with r delimited by brackets and a variant represented by hyphens together with the numbers of conditions met.
-[- ] 1.
[ -]- 2.
[ - ] 1. and 2.
-[---]- 3.
Please note that the code below assumes that (unlike table1) table2[, 2]
<= table2[, 3]
.
# simplified table1
table1 <- data.frame(pos=c(2431, 98041, 8743), end=c(2100, 99100, 9000))
table2 <- data.frame(exon='Exon', from=c(2001, 8700), to=c(2500, 8750))
is.within <- apply(table1[, c('pos', 'end')], 1, function(x) {
x <- sort(x) # make sure x[1] <= x[2], i.e. pos <= end
any((x[1] >= table2[, 2] & x[1] <= table2[, 3]) | # 1.
(x[2] >= table2[, 2] & x[2] <= table2[, 3]) | # 2.
(x[1] <= table2[, 2] & x[2] >= table2[, 3])) # 3.
})
table1[is.within, ]
(Ruby) If the array intersection operator ( & ) is inefficient, why is it available?
&
is not a particularly inefficient method. I think you misunderstood the criticism of the accepted answer.
Your preferred solution is inefficient because it converts the ranges to arrays.
A range such as 1..10000
has a relatively small memory footprint - it only stores the start and end points. But if you convert it to an array, you allocate memory for all 10,000 entries.
Related Topics
Safe Navigation Equivalent to Rails Try for Hashes
Dynamically Create Class Attributes with Attr_Accessor
Rails: Update Model Attribute Without Invoking Callbacks
Refactoring Activerecord Models with a Base Class Versus a Base Module
Running Phantomjs from a Ruby on Rails Application
Memory Usage Increase with Ruby 2.1 Versus Ruby 2.0 or 1.9
How to Do Sti and Still Use Polymorphic Path Helpers
Rendering a JSON Object of a Join-Model and Its Associated Models
Does Begin . . . End While Denote a 'Block'
How to Detect Browser Type and Its Version
Sending a Delete Request from Sinatra
What Rails Plugins Are Good, Stable and *Really* Enhance Your Code
Built in Way to List Directories in a Directory in Ruby