conditional-statements

Summing first 2 elements in a Python list when the length of the list is unknown

拟墨画扇 提交于 2019-11-30 21:13:19
问题 I am working on the following Python list exercise from codingbat.com: Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0. Examples: sum2([1, 2, 3]) → 3 sum2([1, 1]) → 2 sum2([1, 1, 1, 1]) → 2 My solution below works: def sum2(nums): if len(nums)>=2: return nums[0] + nums[1] elif len(nums)==1: return nums[0] return 0 But I wonder if there's any way to solve

When does #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) in dxvahd.h Microsoft header file become true

泪湿孤枕 提交于 2019-11-30 20:50:14
Hi I am having 2 VC++ solutions "A" & "B" (VS2008) both are having the same codebase (with just few lines of code different). Using DXVAHD.h in both. dxvahd.h is a standard Microsoft header file. If we open this header file, we see there is a conditional if " #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) " I see that in VC++ solution "A", the above conditional #if statement is false, hence the whole dxvahd header file gets greyed out & is not even compiled!! Whereas in another solution "B", this conditional #if is true,hence no issues & its working fine. Can anyone kindly let me know

Numpy: Filtering rows by multiple conditions?

∥☆過路亽.° 提交于 2019-11-30 20:40:57
I have a two-dimensional numpy array called meta with 3 columns.. what I want to do is : check if the first two columns are ZERO check if the third column is smaller than X Return only those rows that match the condition I made it work, but the solution seem very contrived : meta[ np.logical_and( np.all( meta[:,0:2] == [0,0],axis=1 ) , meta[:,2] < 20) ] Could you think of cleaner way ? It seem hard to have multiple conditions at once ;( thanks Sorry first time I copied the wrong expression... corrected. you can use multiple filters in a slice, something like this: x = np.arange(90.).reshape(30

Conditional summation in Crystal Reports

青春壹個敷衍的年華 提交于 2019-11-30 19:39:34
问题 I have some rows with a price and quantity and I'm looking to sum only the prices where the quantity > 5 itemname price Qty ---------------------------------- apple 20 2 watermelon 10 3 bango 22 6 hashesh 3 9 Given the above data, the sum I'd like to get is 22+3=25. How do I write a formula to do this? 回答1: create a formula field named calculation to the side of quantity and enter the following text: // {@calculation} If quantity>5 then price else 0 now take the sum of {@calculation} .

PostgreSQL create a new column with values conditioned on other columns

我是研究僧i 提交于 2019-11-30 19:11:29
I use PostgreSQL 9.1.2 and I have a basic table as below, where I have the Survival status of an entry as a boolean (Survival) and also in number of days (Survival(Days)) . I have manually added a new column named 1-yr Survival and now I want to fill in the values of this column for each entry in the table, conditioned on that entry's Survival and Survival (Days) column values. Once , completed the database table would look something like this: Survival Survival(Days) 1-yr Survival ---------- -------------- ------------- Dead 200 NO Alive - YES Dead 1200 YES The pseudo code to input the

Does C# support if codeblocks without braces?

余生颓废 提交于 2019-11-30 17:42:23
How would C# compile this? if (info == 8) info = 4; otherStuff(); Would it include subsequent lines in the codeblock? if (info == 8) { info = 4; otherStuff(); } Or would it take only the next line? if (info == 8) { info = 4; } otherStuff(); Yes, it supports it - but it takes the next statement , not the next line . So for example: int a = 0; int b = 0; if (someCondition) a = 1; b = 1; int c = 2; is equivalent to: int a = 0; int b = 0; if (someCondition) { a = 1; } b = 1; int c = 2; Personally I always include braces around the bodies of if statements, and most coding conventions I've come

Declaring an implicitly typed variable inside conditional scope and using it outside

瘦欲@ 提交于 2019-11-30 17:08:36
问题 In the simplified code below, if(city == "New York City") { var MyObject = from x in MyEFTable where x.CostOfLiving == "VERY HIGH" select x.*; } else { var MyObject = from x in MyEFTable where x.CostOfLiving == "MODERATE" select x.*; } foreach (var item in MyObject) { Console.WriteLine("<item's details>"); } The variable MyObject is not accessible outside conditional block. How can I iterate outside the if..else ? 回答1: Let's clarify your confusing question. The problem is that you have two

ARM IT conditional instruction assembler (armcc)

£可爱£侵袭症+ 提交于 2019-11-30 16:35:59
why below compiles : ITE EQ MRSEQ R0, MSP MRSNE R0, PSP but this not: ITT NE MRSNE R0, PSP MRSEQ R0, MSP Is it possible, that both MRSNE R0, PSP and MRSEQ R0, MSP execute (this is my case)? This compiles: ITT NE MRSNE R0, PSP MRSNE R0, MSP Is it ARM standard? artless noise but this not: ITT NE MRSNE R0, PSP MRSEQ R0, MSP First you have some concept issues. What is ITT all about? First some history. The early ARM CPUs did not support Thumb (16bit), nor Thumb2 (mix 16/32bit) encoding. For the pure ARM, a large part (4 leading bits) are dedicated to conditional execution. The Thumb instruction

Better way to write “matching balanced parenthesis” program in Ruby

♀尐吖头ヾ 提交于 2019-11-30 16:20:05
问题 This method is supposed to take a string and detect if the brackets '(' '{' '[' in the string are closing properly with the corresponding (opposite) brackets. First, is there a more elegant, compact way to write this bit without using all the "or"s (||): split_array.each do |i| if (i == "{" || i == "(" || i == "[") left.push(i) else (i == "}" || i == ")" || i == "]") right.push(i) end end My second question is, is this code terrible (see below)? It seems I should be able to write this in way

ARM IT conditional instruction assembler (armcc)

泄露秘密 提交于 2019-11-30 15:58:56
问题 why below compiles : ITE EQ MRSEQ R0, MSP MRSNE R0, PSP but this not: ITT NE MRSNE R0, PSP MRSEQ R0, MSP Is it possible, that both MRSNE R0, PSP and MRSEQ R0, MSP execute (this is my case)? This compiles: ITT NE MRSNE R0, PSP MRSNE R0, MSP Is it ARM standard? 回答1: but this not: ITT NE MRSNE R0, PSP MRSEQ R0, MSP First you have some concept issues. What is ITT all about? First some history. The early ARM CPUs did not support Thumb (16bit), nor Thumb2 (mix 16/32bit) encoding. For the pure ARM,