for-loop

Why a variable can be accessible outside the loop in Python? [duplicate]

荒凉一梦 提交于 2019-12-31 01:05:08
问题 This question already has answers here : Scoping in Python 'for' loops (6 answers) Closed 4 years ago . Consider this example: for iter in xrange(10): myvar = iter print myvar # 9 Here myvar is clearly outside the loop? But it is still accessible. If this is Perl, it will throw an error. What's the reason behind such feature in Python? Is it harmful? What's the best practice then, to declare a variable before looping? 回答1: There is no new scope created by the for loop (Ruby also behaves the

PL/SQL FOR LOOP IMPLICIT CURSOR

丶灬走出姿态 提交于 2019-12-30 18:41:20
问题 There are 2 tables EMPLOYEES and DEPARTMENTS with department_id as primary key for DEPARTMENTS and foreign key on EMPLOYEES . I want to print all the employee names that belong to a particular department. I know it can be easily achieved by JOINS or EXPLICIT cursors. I thought why not try with FOR loop and a IMPLICIT cursors. My question is if it is syntactically correct to write INTO like this. If so why is not assigning any values? DECLARE emp_dept_id employees.department_id%TYPE; emp_emp

Difference between moving an Iterator forward with a for statement and a while statement

好久不见. 提交于 2019-12-30 17:27:49
问题 When I use an Iterator of Object I use a while loop (as written in every book learning Java, as Thinking in Java of Bruce Eckel): Iterator it=... while(it.hasNext()){ //... } but sometime i saw than instead somebody use the for loop : Iterator it=... for (Iterator it=...; it.hasNext();){ //... } I don't' understand this choice: I use the for loop when I have a collection with ordinal sequence (as array) or with a special rule for the step (declared generally as a simple increment counter++ ).

Java for each loop being flagged as UR anomaly by PMD

安稳与你 提交于 2019-12-30 16:23:21
问题 I would like to confirm if this is a bug on PMD? How do I file a ticket if it is. public static void main(final String[] args) { for (final String string : args) { string.getBytes(); //UR Anomaly } for (int i = 0; i < args.length; i++) { args[i].getBytes(); } } Lines 1-3 are being flagged as UR anomaly, while rewriting it to iterate with a local variable is fine. Would like to eliminate as much PMD violations, but it is inconvenient to have to resort to old loop construct as a workaround.

Java for each loop being flagged as UR anomaly by PMD

牧云@^-^@ 提交于 2019-12-30 16:23:08
问题 I would like to confirm if this is a bug on PMD? How do I file a ticket if it is. public static void main(final String[] args) { for (final String string : args) { string.getBytes(); //UR Anomaly } for (int i = 0; i < args.length; i++) { args[i].getBytes(); } } Lines 1-3 are being flagged as UR anomaly, while rewriting it to iterate with a local variable is fine. Would like to eliminate as much PMD violations, but it is inconvenient to have to resort to old loop construct as a workaround.

A random number between 1 and 4 that's not another random number

女生的网名这么多〃 提交于 2019-12-30 16:19:28
问题 For some reason the following code doesn't work. var a1 = Math.floor(Math.random()*4+1); //Answer2 for(a2 = 0; a2 != a1 && a2 != 0; a2 = Math.floor(Math.random()*4+1)){ alert(a2); } I'm trying to get "a2" to be a int from 1-4 but not equivalent to "a1". What's wrong? Thanks in advance!!! EDIT: Thank you all for your help! Here's my final result: var a1, a2; a1 = Math.floor(Math.random()*4+1); a2 = a1; while(a2 == a1){ a2 = Math.floor(Math.random() * 3 + 1); alert(a2); } 回答1: How about a while

A random number between 1 and 4 that's not another random number

喜你入骨 提交于 2019-12-30 16:19:10
问题 For some reason the following code doesn't work. var a1 = Math.floor(Math.random()*4+1); //Answer2 for(a2 = 0; a2 != a1 && a2 != 0; a2 = Math.floor(Math.random()*4+1)){ alert(a2); } I'm trying to get "a2" to be a int from 1-4 but not equivalent to "a1". What's wrong? Thanks in advance!!! EDIT: Thank you all for your help! Here's my final result: var a1, a2; a1 = Math.floor(Math.random()*4+1); a2 = a1; while(a2 == a1){ a2 = Math.floor(Math.random() * 3 + 1); alert(a2); } 回答1: How about a while

Return the first word with the greatest number of repeated letters

僤鯓⒐⒋嵵緔 提交于 2019-12-30 14:44:12
问题 This is a question from coderbyte’s easy set. Many people asked about it already, but I’m really curious about what’s wrong with my particular solution (I know it’s a pretty dumb and inefficient one..) Original question: Have the function LetterCountI(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever

Why getting error in bash for loop?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 14:20:12
问题 I am trying to run the below code using for loop but i am getting syntax error. Please help. Input Format: The first line of the input contains an integer N, indicating the number of integers. The next line contains N space-separated integers that form the array A. read n sum=0 for (( i=1; i<= "$n" ; i++ )) do read val sum ^= $val; done echo $sum Below is the error message solution.sh: line 4: ((: i<= 1 : syntax error: invalid arithmetic operator (error token is " ") 回答1: The printf "%s" "$n"

Converting nested FOR loops to PARFOR loop matlab

扶醉桌前 提交于 2019-12-30 11:15:48
问题 I have these nested for-loops that I would like to convert to parfor: row = 1; for i = 5 : 0.2 : 5.4 col = 1; for j = 2 : 0.5 : 2.5 matrx(row, col) = i * j; col = col + 1; end row = row + 1; end Does anyone any way in which this would be possible? 回答1: I hope you're only displaying an extremely simplified version of your code, but anyways, the secret to parfor can be found by listening to Matlab numerous messages and reading the documentation. Start by learning good Matlab coding practices,