range

How to find range overlap in python?

百般思念 提交于 2019-11-27 21:51:39
What is the best way in Python to determine what values in two ranges overlap? For example: x = range(1,10) y = range(8,20) (The answer I am looking for would be the integers 8 and 9.) Given a range, x, what is the best way to iterate through another range, y and output all values that are shared by both ranges? Thanks in advance for the help. EDIT: As a follow-up, I realized that I also need to know if x does or does not overlap y. I am looking for a way to iterate through a list of ranges and and do a number of additional things with range that overlap. Is there a simple True/False statement

How do I summarize array of integers as an array of ranges?

試著忘記壹切 提交于 2019-11-27 21:48:59
问题 I'd like to take input such as: [1,2,4,5,6,7,9,13] and turn it into something like the following: [[1,2],[4,7],[9,9],[13,13]] Each sub-array represents a range of integers. 回答1: Functional approach using Enumerable#chunk: ranges = [1, 2, 4, 5, 6, 7, 9, 13] .enum_for(:chunk) # .chunk for Ruby >= 2.4 .with_index { |x, idx| x - idx } .map { |_diff, group| [group.first, group.last] } #=> [[1, 2], [4, 7], [9, 9], [13, 13]] How it works: once indexed, consecutive elements in the array have the same

group by range in mysql

烂漫一生 提交于 2019-11-27 21:22:19
Table: new_table user_number | diff 2 | 0 1 | 28 2 | 32 1 | 40 1 | 53 1 | 59 1 | 101 1 | 105 2 | 108 2 | 129 2 | 130 1 | 144 |(result) v range | number of users 0-20 | 2 21-41 | 3 42-62 | 1 63-83 | 2 84-104 | 1 105-135| 0 136-156| 3 select t.range as [range], count(*) as [number of users] from ( select case when diff between 0 and 20 then ' 0-20' when diff between 21 and 41 then ' 21-41' when diff between 42 and 62 then ' 42-62' when diff between 63 and 83 then ' 63-83' when diff between 84 and 104 then ' 84-104' when diff between 105 and 135 then ' 105-135' else '136-156' end as range from

Remove consecutive duplicates from dataframe

丶灬走出姿态 提交于 2019-11-27 21:20:16
问题 I have a data frame that I want to remove duplicates that are consecutive (in base). I know rle may be helpful here but can't think of how to use it. The example output will help to illuminate what I'm asking for. Generate sample data: set.seed(12) samps <- sample(1:5, 20, T) dat <- data.frame(v1=LETTERS[samps], v2=month.abb[samps]) dat[10, 2] <- "Mar" Sample data: v1 v2 1 A Jan 2 E May 3 E May 4 B Feb 5 A Jan 6 A Jan 7 A Jan 8 D Apr 9 A Jan 10 A Mar 11 B Feb 12 E May 13 B Feb 14 B Feb 15 B

Initialize std::array with a range (pair of iterators)

风流意气都作罢 提交于 2019-11-27 21:19:42
How can I initialize an std::array from a range (as defined by a pair of iterators)? Something like this: vector<T> v; ... // I know v has exactly N elements (e.g. I just called v.resize(N)) // Now I want a initialized with those elements array<T, N> a(???); // what to put here? I thought array would have a constructor taking a pair of iterators, so that I could do array<T, N> a(v.begin(), v.end()) , but it appears to have no constructors at all! I know I can copy the vector into the array, but I'd rather initialize the array with the vector contents directly, without default-constructing it

How can I pattern match on a range in Scala?

巧了我就是萌 提交于 2019-11-27 21:01:24
In Ruby I can write this: case n when 0...5 then "less than five" when 5...10 then "less than ten" else "a lot" end How do I do this in Scala? Edit: preferably I'd like to do it more elegantly than using if . Inside pattern match it can be expressed with guards: n match { case it if 0 until 5 contains it => "less than five" case it if 5 until 10 contains it => "less than ten" case _ => "a lot" } class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i } val C1 = new Contains(3 to 10) val C2 = new Contains(20 to 30) scala> 5 match { case C1() => println("C1"); case C2() => println

Create Chart from Array data and not range

你离开我真会死。 提交于 2019-11-27 20:10:27
is it possible to create a Chart (e.g. Double Y-Axis Line Chart) not from Ranges, but from Array data? If so, how? Yes. You can assign arrays to the XValues and Values properties of a Series object on a chart. Example: Dim c As Chart Dim s As Series Dim myData As Variant Set c = ActiveChart ' Assumes a chart is currently active in Excel... Set s = c.SeriesCollection(1) myData = Array(9, 6, 7, 1) ' or whatever s.Values = myData You can assign arrays to chart series in Excel 2007 onwards but in previous versions I believe there is a 255 character limit for the length of each series. A method I

How can I generate a random number in a certain range?

有些话、适合烂在心里 提交于 2019-11-27 19:59:39
How can I create an app that generates a random number in Android using Eclipse and then show the result in a TextView field? The random number has to be in a range selected by the user. So, the user will input the max and min of the range, and then I will output the answer. Fantômas To extend what Rahul Gupta said: You can use the Java function int random = Random.nextInt(n) . This returns a random int in the range [0, n-1] . I.e., to get the range [20, 80] use: final int random = new Random().nextInt(61) + 20; // [0, 60] + 20 => [20, 80] To generalize more: final int min = 20; final int max

Designing non-overlapping date-time events

巧了我就是萌 提交于 2019-11-27 19:39:48
问题 I have the following problem: Events have a "begin" and "end" time and an amount. I use MySQL DATETIME for both. Now if I have a constraint that says "no overlapping events" I need to make some checks etc. but how to design that? The user only needs 5-mins precision or so, but i want to do calculations with seconds since that is "simpler"/"cleaner" If I have an event (A) with start-end "YYYY-MM-DD 12:00:00"-"YYYY-MM-DD 14:00:00" and another (B) "YYYY-MM-DD 14:00:00"-"YYYY-MM-DD 16:15:00" ->

VBA: Selecting range by variables

可紊 提交于 2019-11-27 19:32:26
I want to select the formatted range of an Excel sheet. To define the last and first row I use the following functions: lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row In the next step I want to select this area: Formula should look like this: Range(cells(1, 1), cells(lastRow, lastColumn).Select However, this is not working. Maybe somebody has an idea what is wrong with it. Thanks a lot! Nikesh Rastogi I recorded a macro with 'Relative References' and this is what I got : Range("F10")