range

Ruby's range step method causes very slow execution?

我怕爱的太早我们不能终老 提交于 2019-12-04 05:57:30
I've got this block of code: date_counter = Time.mktime(2011,01,01,00,00,00,"+05:00") @weeks = Array.new (date_counter..Time.now).step(1.week) do |week| logger.debug "WEEK: " + week.inspect @weeks << week end Technically, the code works, outputting: Sat Jan 01 00:00:00 -0500 2011 Sat Jan 08 00:00:00 -0500 2011 Sat Jan 15 00:00:00 -0500 2011 etc. But the execution time is complete rubbish! It takes approximately four seconds to compute each week. Is there some grotesque inefficiency that I'm missing in this code? It seems straight-forward enough. I'm running Ruby 1.8.7 with Rails 3.0.3.

Difference between two ranges

假装没事ソ 提交于 2019-12-04 05:52:45
I can find plenty of questions and example regarding the 'Union' and 'Intersect' VBA methods but I can't find anything much regarding a 'Set Difference' method? Does this exist (other than by using combinations of union and intersect)?. I'm trying to find a simple way of getting all of range1 excluding any of range1 that overlaps range2 without knowing the size or shape of either range. Any help would be greatly appreciated. EDIT. Attempted solution where rng1 is the red section and rng2 is the blue section (have debugged to check these are correct): rng = SetDifference(rng, highlightedColumns

How to normalize a list of positive and negative decimal number to a specific range

谁说我不能喝 提交于 2019-12-04 05:39:53
I have a list of decimal numbers as follows: [-23.5, -12.7, -20.6, -11.3, -9.2, -4.5, 2, 8, 11, 15, 17, 21] I need to normalize this list to fit into the range [-5,5] . How can I do it in python? To get the range of input is very easy: old_min = min(input) old_range = max(input) - old_min Here's the tricky part. You can multiply by the new range and divide by the old range, but that almost guarantees that the top bucket will only get one value in it. You need to expand your output range so that the top bucket is the same size as all the other buckets. new_min = -5 new_range = 5 + 0.9999999999

Convert List of Numbers to String Ranges

百般思念 提交于 2019-12-04 05:24:35
I'd like to know if there is a simple (or already created) way of doing the opposite of this: Generate List of Numbers from Hyphenated... . This link could be used to do: >> list(hyphen_range('1-9,12,15-20,23')) [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 18, 19, 20, 23]: I'm looking to do the opposite (note that 10 and 21 are included so it would be compatible with the range function, where range(1,10)=[1,2,3,4,5,6,7,8,9]): >> list_to_ranges([1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 18, 19, 20, 23]) '1-10,12,15-21,23' Eventually, I would like to have the output also incorporate a step where

Get `n` random values between 2 numbers having average `x`

纵然是瞬间 提交于 2019-12-04 05:12:17
问题 I want to get n random numbers(e.g n=16)(whole numbers) between 1 to 5(including both) so that average is x. x can be any value between (1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5). I am using PHP. e.g. Suppose I have average x= 3. Then required 16 whole numbers between 1 to 5(including both). like (1,5,3,3,3,3,2,4,2,4,1,5,1,5,3,3) Update: if x=3.5 means average of 16 numbers should be between 3.5 to 4. and if x=4 means average of 16 numbers should be between 4 to 4.5 and if x=5 means all numbers are

Get a list of numbers from range(s) of number

倾然丶 夕夏残阳落幕 提交于 2019-12-04 04:43:08
问题 I have a data frame where one column contains a range (or ranges) of numbers. I would like to turn this into a list of numbers based of the given range. Example input: "35-40" or "35-43, 45-47" This should yield: [1] 35 36 37 38 39 40 and [1] 35 36 37 38 39 40 41 42 43 45 46 47 回答1: We can do a split and with Map , get the numbers do.call(Map, c(`:`, lapply(strsplit(df1$v1, '-'), as.numeric))) #[[1]] # [1] 35 36 37 38 39 40 41 42 43 44 45 #[[2]] #[1] 43 44 45 46 47 If we need to find the

How to efficiently insert a range of consecutive integers into a std::set?

末鹿安然 提交于 2019-12-04 04:33:26
问题 In C++, I have a std::set that I would like to insert a range of consecutive integers. How can I do this efficiently, hopefully in O(n) time where n is the length of the range? I'm thinking I'd use the inputIterator version of std::insert, but am unclear on how to build the input iterator. std::set<int> mySet; // Insert [34 - 75): mySet.insert(inputIteratorTo34, inputIteratorTo75); How can I create the input iterator and will this be O(n) on the range size? 回答1: The efficient way of inserting

dom range.setStart / setEnd

本小妞迷上赌 提交于 2019-12-04 04:09:35
I am trying to bold only the text hel in this fiddle http://jsfiddle.net/yarkpakv/ but it does not seem to be working, what am I doing wrong??? var range = document.createRange(); var root_node = document.getElementById("test"); range.setStart(root_node,0); range.setEnd(root_node,3); var newNode = document.createElement("b"); range.surroundContents(newNode); <div id="test"> <p>h</p>ello </div> You need to visualize the DOM structure of your <div id="test"> element. It contains three children: A text node that contains only white space. The <p> element which contains a text node that has the

REGEX To accept numbers separated by commas, but number range is 0-32767

自闭症网瘾萝莉.ら 提交于 2019-12-04 03:48:39
I need to write a regular expression for taking input like this 23,456,22,1,32767 i.e. No commas allowed at the start or end. Spaces may come before and/or start of comma for e.g. 23, 45,56 ,67 etc. Ranges of each number should be 0-32767. Currently I am using regular expression like this [0-9]+(,[0-9]+)* . This allows for numbers separated by commas only ( not allowing spaces at all), and it does not check for the range of number. It's probably wise to do it in two steps. First check that the range is 0-99999: ^[0-9]{1,5}( *, *[0-9]{1,5})*$ Then parse the string to a list of integers using a

Combine a PostgreSQL EXCLUDE range constraint with a UNIQUE constraint

ぐ巨炮叔叔 提交于 2019-12-04 03:36:26
In PostgreSQL, how do you combine an exclusion constraint on a range column with a unique constraint on other, scalar columns. Or to put it another way, how do I ensure that the range overlap check is only done in combination with a unique check on some other columns? For example, say I have: CREATE TABLE reservation ( restaurant_id int, time_range tsrange ); I want to make sure that for each restaurant_id , there are no overlapping time_range s. I know that I can create an exclusive range constraint like this: CREATE TABLE reservation ( restaurant_id int, time_range tsrange EXCLUDE USING gist