conditional-statements

What condition is false in a multiple conditions if statement

空扰寡人 提交于 2019-12-06 03:46:14
I'm writing a multiple if statement in Javascript. I've 3 (or more) conditions, and I wanna doSomething() only if all these 3 conditions are true. If only one of these 3 are false, I wanna doSomethingElse(). I think my code it's right, but my problem is on another level. What if I wanna know for which condition my statement is false? E.g.: condition1=true, condition2=true, condition3=false. if (condition1 && condition2 && condition3) { doSomething(); } else { doSomethingElse(); }; I've thought that I can put another if statement in the else part. if (condition1 && condition2 && condition3) {

How does the processor handle conditions?

时光毁灭记忆、已成空白 提交于 2019-12-06 02:59:48
问题 So, SUPER low-level what does an IF() look like, how is it handled by an x86 processor? 回答1: The processor has "Branch if" instructions that when a certain condition is met it branches, and otherwise it continues on to the next instruction. So if(A) { dosomething; } would become load A into register 0 if the zero flag is set (ie, register 0 contains 0x00) then jump to endcondition) dosomething endcondition: More complex conditions ( if(A || B && C) ) become a sequence of instructions that

How to rename elements of a column based on conditional statements?

元气小坏坏 提交于 2019-12-05 18:31:02
In pandas, given this data frame: df = pd.DataFrame({'l':['a','b','a','c','b','b','a','b','b','a'], 'v':['x','x','y','y','y','x','x','y','x','y'],'n':[1,2,1,2,2,1,2,1,1,2], 'g':[0,0,0,0,0,1,1,1,1,1]}) What is the best solution to rename the elements of v based on some conditional statements applied to the data frame? Basically, for each row (regardless of whether g == 0 or g == 1 ): if df.l==a and df.n==1: df.v='val1' elif df.l==a and df.n==2: df.v='val2' elif df.l==b and df.n==1: df.v='val3' elif df.l==b and df.n==2: df.v='val4' You can simply write them out with a boolean mask: df.loc[(df.l

Anyway to shorten if ( i == x || i == y)?

半城伤御伤魂 提交于 2019-12-05 16:12:43
I tried to shorten my code, from : if(i== x || i == y || i == z ) to if (i == ( x || y || z )) I know this way is wrong because I got incorrect i in log. However, is there any method to shorten the code in objective-C ? if there is a higher probability that x==i than y==i then it's better to write it as x==i || y==i as opposed to y==i || x==i because if the first statement evaluates to true, the second one is not evaluated (it's shortcircuited) You could use a switch statement, but that doesn't really buy you a lot with only 2-3 values. switch (i) { case x: case y: case z: ....some code....

Python code for sum with condition

戏子无情 提交于 2019-12-05 14:14:14
The task is following: sum the list elements with even indexes and multiply the result by the last list's elemet. I have this oneliner solution code in Python. array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(i for i in array if array.index(i) % 2 == 0)*array[-1] if array != [] else 0 My result is -1476 ( The calculation is: 41*(-37-19+29+3-64+36+26+55+84-65) ) The right result is 1968. I can't figure it out why this code is not working correctly in this particular case. There is a repeated element 84 in the list, thus array.index does not work as you

How do I write a branchless std::vector scan?

血红的双手。 提交于 2019-12-05 12:41:52
I want to write a simple scan over an array. I have a std::vector<int> data and I want to find all array indices at which the elements are less than 9 and add them to a result vector. I can write this using a branch: for (int i = 0; i < data.size(); ++i) if (data[i] < 9) r.push_back(i); This gives the correct answer but I would like to compare it to a branchless version. Using raw arrays - and assuming that data is an int array, length is the number of elements in it, and r is a result array with plenty of room - I can write something like: int current_write_point = 0; for (int i = 0; i <

Using OR & AND in COUNTIFS

谁都会走 提交于 2019-12-05 11:37:57
问题 I would like to include an "AND" condition for one of the conditions I have in my COUNTIFS clause. Something like this: =COUNTIFS(A1:A196;{"Yes"or "NO"};J1:J196;"Agree") So, it should return the number of rows where: (A1:A196 is either "yes" or "no") AND (J1:j196 is "agree") 回答1: You could just add a few COUNTIF statements together: =COUNTIF(A1:A196,"yes")+COUNTIF(A1:A196,"no")+COUNTIF(J1:J196,"agree") This will give you the result you need. EDIT Sorry, misread the question. Nicholas is right

Multiple query conditions in Rails - if they exist.

别说谁变了你拦得住时间么 提交于 2019-12-05 10:50:33
I found a few similar questions while searching here, but when I tried to add unless to the solutions I found, things started to break... Here's what I have that works: Controller: @metrics = Metric.where("current_ratio > ?", @screen.current_ratio_min) unless @screen.current_ratio_min.nil? Once I add another .where line (of which I need to add many), @metrics = Metric.where("current_ratio > ?", @screen.current_ratio_min) unless @screen.current_ratio_min.nil? .where("current_ratio < ?", @screen.current_ratio_max) unless @screen.current_ratio_max.nil? I get an error: undefined method `where' for

Perl. Using until function

故事扮演 提交于 2019-12-05 10:19:10
I have a simple data file. Each line in the file has four elements. Some lines are filled with no blank entries. Other lines have a first entry and the remaining three are blank, or rather "filled" with a space. It is a tab delimited file. Example of the input file: . . . 30 13387412 34.80391242 sSN_FIRST 30 13387412 34.80391242 sSN5_40 30.1 30.2 30.3 30.4 31 14740248 65.60590089 s32138223_44 31 14740248 65.60590089 s321382_LAST . . . To reiterate, the "blanks" in my file actually contain a single space, if this matters. My overall goal is to "fill in" the second and third column (the fourth

filter HABTM-associated model with conditions

谁都会走 提交于 2019-12-05 10:04:44
preamble: a few days ago I asked a question to solve a HABTM-filter, I'm not able to do it even with tutorials, so "Obi Kwan Kenobi youre my only hope". What I want to achieve: Filtering Staff by GroupID which is used in StaffStaffgroup I'm having the following tablelayout staffs (a person can belong to many groups) staff_staffgroups (HABTM-linking table) staffgroups (has a groupname) The variable $tmp gets me a working array, but the problem is that Staff is a child object of StaffStaffgroups. I could parse throu and reassemble a array, but this isnt a nice solution. So I want to use the