conditional

C++ Conditional operator performance

為{幸葍}努か 提交于 2019-12-23 15:48:41
问题 I've a conditional statement expensive_foo() which is false in 99.9% cases. And I have a conditional statement bar which is true in ~50% cases. And I want some action be done if both statements are true. So I almost certainly know that expensive_foo() is false and I want to check it only if bar is true. Will the code below check the expensive_foo() ONLY if bar is true? Or it will check expensive_foo() every time? if ( bar && expensive_foo() ) { ... } Or I need to make a structure like this:

XSL - Does evaluating conditional expressions “shortcut”?

偶尔善良 提交于 2019-12-23 12:42:41
问题 Given an XSL 'If' statement: <xsl:if test="a = 'some value' and b = 'another value'"> If a does not equal 'some value' , is the value of b still checked? (As if the first test is false , the only outcome of the and is false .) This is what languages like C# do - I was wondering if the same goes in XSL. Does it depend on the engine/parser? 回答1: Yes, it is called lazy-evaluation or short-circuiting, and xsl supports it. See: http://www.w3.org/TR/xpath#booleans An and expression is evaluated by

Python: Inline if statement else do nothing

此生再无相见时 提交于 2019-12-23 11:41:23
问题 Assigning a Django Model's field to a value if it matches a condition. g = Car.objects.get(pk=1234) g.data_version = my_dict['dataVersion'] if my_dict else expression_false # Do nothing?? How do I do nothing in that case? We can't do if conditional else pass . I know I can do: if my_dict: g.data_version = my_dict['dataVersion'] but I was wondering if there was a way to do inline expression_true if conditional else do nothing . 回答1: No, you can't do exactly what you are describing, as it

Bash Compound Conditional, With Wildcards and File Existence Check

一世执手 提交于 2019-12-23 10:55:01
问题 I've mastered the basics of Bash compound conditionals and have read a few different ways to check for file existence of a wildcard file, but this one is eluding me, so I figured I'd ask for help... I need to: 1.) Check if some file matching a pattern exists AND 2.) Check that text in a different file exists. I know there's lots of ways to do this, but I don't really have the knowledge to prioritize them (if you have that knowledge I'd be interested in reading about that as well). First

How do I conditionally include a file in a Sphinx 'toctree'? [duplicate]

会有一股神秘感。 提交于 2019-12-23 10:06:32
问题 This question already has answers here : Conditional toctree in Sphinx (4 answers) Closed 4 years ago . I would like to include one of my files in my Sphinx TOC only when a certain tag is set, however the obvious approach fails: .. toctree:: :maxdepth: 5 index core utils oec plotting install news glossary .. only:: private_version todo Is there a simple way to accomplish this? 回答1: In a past I had a need to be able to compile two documentations from the same source file: a public and a

Which of `if x:` or `if x != 0:` is preferred in Python?

偶尔善良 提交于 2019-12-23 10:05:49
问题 Assuming that x is an integer, the construct if x: is functionally the same as if x != 0: in Python. Some languages' style guides explicitly forbid against the former -- for example, ActionScript/Flex's style guide states that you should never implicitly cast an int to bool for this sort of thing. Does Python have a preference? A link to a PEP or other authoritative source would be best. 回答1: The construct: if x: is generally used to check against boolean values. For int s the use of the

Conditional sum in SQL Server

给你一囗甜甜゛ 提交于 2019-12-23 09:29:32
问题 I have this table in MSSQL: ID OP1 OP2 OP3 330 0 0 1 331 1 0 0 332 3 2 0 OP's are options. There can go from 1 to 9, 0 means that question was not answer. How can i "sum" in a column called "Answered", like so: ID OP1 OP2 OP3 T 330 0 0 1 1 331 1 0 0 1 332 3 2 0 2 A value over 0, means answered. I try with CASE WHEN, IF statements. 回答1: Use CASE : SELECT Id, OP1, OP2, OP3, (CASE WHEN OP1 > 0 THEN 1 ELSE 0 END + CASE WHEN OP2 > 0 THEN 1 ELSE 0 END + CASE WHEN Op3 > 0 THEN 1 ELSE 0 END) AS T

Difference between MySQL IS NOT NULL and != ''

不想你离开。 提交于 2019-12-23 09:04:15
问题 Is there any difference between MySQL IF (myText IS NOT NULL) THEN and IF (myText != '') THEN 回答1: Yes there is a big difference between a NULL value and a blank/empty value. Here's one resource that describes the differences. When myText IS NULL : myText IS NOT NULL evaluates to FALSE myText != '' evaluates to NULL (which essentially behaves the same as FALSE would in this specific case you wrote) However, you should not get into the habit of treating them the same, since most of the time

Create duplicate rows based on conditions in R

廉价感情. 提交于 2019-12-23 07:29:17
问题 I have a data.table that looks like this dt <- data.table(ID=c("A","A","B","B"),Amount1=c(100,200,300,400), Amount2=c(1500,1500,2400,2400),Dupl=c(1,0,1,0)) ID Amount1 Amount2 Dupl 1: A 100 1500 1 2: A 200 1500 0 3: B 300 2400 1 4: B 400 2400 0 I need to duplicate each row that has a 1 in the Dupl column and replace the Amount1 value with the Amount2 value in that duplicated row. Besides that I need to give that duplicated row the value 2 in Dupl. This means it should look like this: ID

find the starting and ending indices of values greater than 0 in a list

烈酒焚心 提交于 2019-12-23 06:41:19
问题 I am trying to find the start and stop index of numbers > 0 in list cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] I am getting the indexes of values > 0 followed by index of value = 0. Desired output: (0 2),(7 9), (14 17).. Actual output: (2 3), (7 8).. My code cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5) for i in range(0,len(cross)): if cross[i]==0: while(cross[i-1]>0): i+=1 print(i-1,i) 回答1: How about using some flags to track where you