conditional-statements

How does this implementation of semaphore work?

自古美人都是妖i 提交于 2019-12-04 15:11:34
问题 Merry Xmas! I'm reading the The Little Book of Semaphores. There is an implementation of semaphores in C in the book that I don't completely understand. See below for code. There is this wakeups variable. The author explains: wakeups counts the number of pending signals; that is, the number of threads that have been woken but have not yet resumed execution. The reason for wakeups is to make sure that our semaphores have Property 3, described in Section 4.3 and Property 3: if there are threads

Including multiple conditions in for-loop

我怕爱的太早我们不能终老 提交于 2019-12-04 15:00:18
I am trying to specify two conditions as a part of a for loop. It seems like the second condition in the second for loop is not being considered. My code runs this way: for (i in 1:nrow(mydata)) { for (j in 1:nrow(mydata) && j!=i ) { Statements.... } Statements... } Could you please tell me if this is the right Syntax in R? Thanks! To answer your question, you need: for (i in 1:nrow(mydata)) { for (j in 1:nrow(mydata) ) { if(j != i) { Statements.... } } Statements... } However, there is probably a nicer way achieving what you want to do, but I would need more details. This could do the trick:

SQL Conditional JOIN column [duplicate]

寵の児 提交于 2019-12-04 14:41:59
This question already has answers here : SQL Conditional AND (4 answers) Closed 5 years ago . I want to determine the JOIN column based on the value of the current row. So for example, my job table has 4 date columns: offer_date, accepted_date, start_date, reported_date. I want to check against an exchange rate based on the date. I know the reported_date is never null, but it's my last resort, so I have a priority order for which to join against the exchange_rate table. I'm not quite sure how to do this with a CASE statement, if that's even the right approach. INNER JOIN exchange_rate c1 ON c1

Rails 3 sort after .where condition using NOT NULL

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 13:03:20
I have a ranking which shows the fastest User: @users = User.find.sort { |a, b| b.finished_at <=> a.created_at } Now I hat to add some code to prevent getting an error because of finished_at beeing nil until the user finished. @users = User.find(:all, :conditions => "finished_at IS NOT NULL").sort { |a, b| b.finished_at <=> a.created_at } But sort doesn't work. Is this way of chaining methods flawed? And is find(:all, :conditions => "finished_at IS NOT NULL") the rails 3 way or outdated? Thanks in advance DGM The rails 3 way to query this would be: @users = User.where('finished_at IS NOT NULL'

Conditional content in Mandrill template

徘徊边缘 提交于 2019-12-04 12:31:32
I am handing over dictionary keys (key value pair) to a service which in turn utilizes the api to send the email via Mandrill. Now, if my key is blank then i dont want it to be included in the email. Like in the following scenario, i only want the link text to display if my key has some value. <a href=" |UPDATE_PROFILE| " target="_blank" >change subscription preferences</a> How can i write it some thing like or even is this possible? if *|UPDATE_PROFILE|* IS NOT EMPTY BEGIN <a href="*|UPDATE_PROFILE|*" target="_blank">change subscription preferences</a> END I have found the answer here: http:/

Run code after some time has passed or a condition is met

爱⌒轻易说出口 提交于 2019-12-04 12:12:36
What is the best and DRYest way to write code that can be executed either when some time has passed (say, 5 seconds) or some condition is met (e.g. bool = true ) - whichever comes first . The five seconds start counting from when the script first ran, and the boolean is a global that is changed by another function. I don't think you can combine the time out and the bool-check in one statement, but another good way is also good. Pseudo code: if (bool = true OR timePassed = 5000): runCode() You can set a timeout and cancel it if the function is called before the time limit is reached. var

R multiple conditions in row selection of matrix [duplicate]

孤人 提交于 2019-12-04 11:52:41
This question already has answers here : Closed 6 years ago . Possible Duplicate: R: subset() logical-and operator for combining conditions should be & not && I have a simple question, but I don't know how to solve this... I want to select all rows where value_1 > 0 and value_2 > 0. Now I have this code: dataOnBoth<-data[data$value_1 > 0,][data$value_2 > 0,] When I head this data, ordering on log2_fold_change, I have This output: gene_id sample_1 sample_2 status value_1 value_2 log2_fold_change 86 uc001aen.1 q1 q2 NOTEST 0.0619347 0 -1.79769e+308 150 uc001ahx.1 q1 q2 NOTEST 0.0432199 0 -1

Dynamic Or Clause Linq

血红的双手。 提交于 2019-12-04 11:30:24
问题 Today we currently have a statement like this: var Query = (from dp in db.Patients select dp); var UserID = User.Identity.GetUserId(); if (User.IsInRole("Administrator")) { Query = Query.Where(x => x.AdministratorID == UserID); } if (User.IsInRole("Counselor")) { Query = Query.Where(x => x.CounselorID == UserID); } if (User.IsInRole("Physician")) { Query = Query.Where(x => x.PhysicianID == UserID); } The problem is we have Users that can have multiple roles. If a User is both an Counselor and

querying WHERE condition to character length?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:49:57
问题 I have a database with a large number of words but i want to select only those records where the character length is equal to a given number (in example case 3): $query = ("SELECT * FROM $db WHERE conditions AND length = 3"); But this does not work... can someone show me the correct query? 回答1: Sorry, I wasn't sure which SQL platform you're talking about: In MySQL: $query = ("SELECT * FROM $db WHERE conditions AND LENGTH(col_name) = 3"); in MSSQL $query = ("SELECT * FROM $db WHERE conditions

Drop duplicates of one column based on value in another column, Python, Pandas

被刻印的时光 ゝ 提交于 2019-12-04 08:12:34
I have a dataframe like this: Date PlumeO Distance 2014-08-13 13:48:00 754.447905 5.844577 2014-08-13 13:48:00 754.447905 6.888653 2014-08-13 13:48:00 754.447905 6.938860 2014-08-13 13:48:00 754.447905 6.977284 2014-08-13 13:48:00 754.447905 6.946430 2014-08-13 13:48:00 754.447905 6.345506 2014-08-13 13:48:00 754.447905 6.133567 2014-08-13 13:48:00 754.447905 5.846046 2014-08-13 16:59:00 754.447905 6.345506 2014-08-13 16:59:00 754.447905 6.694847 2014-08-13 16:59:00 754.447905 5.846046 2014-08-13 16:59:00 754.447905 6.977284 2014-08-13 16:59:00 754.447905 6.938860 2014-08-13 16:59:00 754