control-structure

Python - If not statement with 0.0

痞子三分冷 提交于 2019-12-04 22:34:16
I have a question regarding if not statement in Python 2.7 . I have written some code and used if not statements. In one part of the code I wrote, I refer to a function which includes an if not statement to determine whether an optional keyword has been entered. It works fine, except when 0.0 is the keyword's value. I understand this is because 0 is one of the things that is considered 'not'. My code is probably too long to post, but this is an analogous (albeit simplified) example: def square(x=None): if not x: print "you have not entered x" else: y=x**2 return y list=[1, 3, 0 ,9] output=[]

ruby catch-throw and efficiency

寵の児 提交于 2019-12-04 08:06:13
catch in Ruby is meant to jump out of deeply nested code. In Java e.g. it is possible to achieve the same with Java's try-catch meant for handling exceptions, it is however considered poor solution and is also very inefficient. In Ruby for handling exceptions we have begin-raise-rescue and I assume it is also to expensive to use it for other tasks. Is Ruby's catch-throw really a more efficient solution then begin-raise-rescue or are there any other reasons to use it to break nested blocks instead of begin-raise-rescue ? In addition to being the "correct" way to get out of control structures,

PL/pgSQL control structures for lists / arrays

吃可爱长大的小学妹 提交于 2019-12-04 06:16:10
问题 Is it possible to use something like this in Postgres? This is the example from PL/SQL what I want to do: PROCEDURE CREATE_PAYMENT(P_AMOUNT IN NUMBER, P_INVOICE_LIST IN SIMPLEARRAYTYPE, P_AMOUNT_LIST IN NUMBER_TABLE -- pass list of amounts . . .) s_chk_amnt NUMBER; invoice_list SIMPLEARRAYTYPE; amount_list NUMBER_TABLE; BEGIN -- check if amount list is null or contains zeros IF p_amount_list IS NOT NULL AND p_amount_list.COUNT <> 0 THEN FOR r IN p_amount_list.FIRST..p_amount_list.LAST LOOP s

Useful alternative control structures?

可紊 提交于 2019-12-03 02:16:30
问题 Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my most common desire is something like a "split while" (I have no idea what to actually call this): { foo(); } split_while( condition ) { bar(); } The semantics of this code would be that foo() is always run, and then the condition is checked. If true, then bar() is run and we go back to the first block (thus running foo(

Useful alternative control structures?

拥有回忆 提交于 2019-12-02 14:14:27
Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my most common desire is something like a "split while" (I have no idea what to actually call this): { foo(); } split_while( condition ) { bar(); } The semantics of this code would be that foo() is always run, and then the condition is checked. If true, then bar() is run and we go back to the first block (thus running foo() again, etc). Thanks to a comment by reddit user zxqdms , I have learned that Donald E. Knuth writes

PL/pgSQL control structures for lists / arrays

若如初见. 提交于 2019-12-02 08:32:02
Is it possible to use something like this in Postgres? This is the example from PL/SQL what I want to do: PROCEDURE CREATE_PAYMENT(P_AMOUNT IN NUMBER, P_INVOICE_LIST IN SIMPLEARRAYTYPE, P_AMOUNT_LIST IN NUMBER_TABLE -- pass list of amounts . . .) s_chk_amnt NUMBER; invoice_list SIMPLEARRAYTYPE; amount_list NUMBER_TABLE; BEGIN -- check if amount list is null or contains zeros IF p_amount_list IS NOT NULL AND p_amount_list.COUNT <> 0 THEN FOR r IN p_amount_list.FIRST..p_amount_list.LAST LOOP s_chk_amnt := s_chk_amnt + p_amount_list(r); END LOOP; END IF; Can I declare a list of characters and

Is it possible that a variable declared after the main has file scope?

空扰寡人 提交于 2019-11-29 14:31:00
After running this code: #include <stdio.h> int x; int main(void) { printf("%d\n",x); return 0; } int x=5; I expected the output should be 0 . Because of the sequence control structure of the program int x; should be executed first and then 0 is printed and finally int x=5; should be executed. But it is giving the output 5 . How is the program accesses 5 for the x in printf ? The first acts as a forward declaration, and the later acts as the actual definition. Variable default values declared outside of functions get set before main ever runs. So what you are seeing is the correct behavior.

PHP “or” Syntax

瘦欲@ 提交于 2019-11-28 20:08:48
I've seen this a lot: $fp = fopen($filepath, "w") or die(); But I can't seem to find any real documentation on this "or" syntax. It's obvious enough what it does, but can I use it anywhere? And must it be followed by die() ? Are there any caveats to using or when you could use something like if (file_exists($filepath)) $fp = fopen($filepath,"r"); else myErrMessage(); I know it seems like a silly question, but I can't find any hard and fast rules for this. Thanks. It's a logical operator and can be used in any logical expression. http://php.net/manual/en/language.operators.logical.php tacone

Can the for loop be eliminated from this piece of PHP code?

孤街浪徒 提交于 2019-11-28 12:22:40
I have a range of whole numbers that might or might not have some numbers missing. Is it possible to find the smallest missing number without using a loop structure? If there are no missing numbers, the function should return the maximum value of the range plus one. This is how I solved it using a for loop: $range = [0,1,2,3,4,6,7]; // sort just in case the range is not in order asort($range); $range = array_values($range); $first = true; for ($x = 0; $x < count($range); $x++) { // don't check the first element if ( ! $first ) { if ( $range[$x - 1] + 1 !== $range[$x]) { echo $range[$x - 1] + 1

PHP “or” Syntax

爱⌒轻易说出口 提交于 2019-11-27 12:45:29
问题 I've seen this a lot: $fp = fopen($filepath, "w") or die(); But I can't seem to find any real documentation on this "or" syntax. It's obvious enough what it does, but can I use it anywhere? And must it be followed by die() ? Are there any caveats to using or when you could use something like if (file_exists($filepath)) $fp = fopen($filepath,"r"); else myErrMessage(); I know it seems like a silly question, but I can't find any hard and fast rules for this. Thanks. 回答1: It's a logical operator