nested-loops

Breaking/exit nested for in vb.net

夙愿已清 提交于 2019-11-27 11:09:40
How do I get out of nested for or loop in vb.net? I tried using exit for but it jumped or breaked only one for loop only. How can I make it for the following: for each item in itemList for each item1 in itemList1 if item1.text = "bla bla bla" then exit for end if end for end for Unfortunately, there's no exit two levels of for statement, but there are a few workarounds to do what you want: Goto . In general, using goto is considered to be bad practice (and rightfully so), but using goto solely for a forward jump out of structured control statements is usually considered to be OK, especially if

Passing argument from Parent function to nested function Python

蓝咒 提交于 2019-11-27 05:32:19
here is my code: def f(x): def g(n): if n < 10: x = x + 1 g(n + 1) g(0) When I evaluate f(0), there would be an error "x referenced before assignment". However, when I use "print x" instead of "x = x + 1" , it will work. It seems that in the scope of g, I can only use x as an "use occurrence" but not a "binding occurrence". I guess the problem is that f passes to g only the VALUE of x. Am I understanding it correctly or not? If not, can someone explain why the left side of "x = x + 1" is not defined before reference? Thanks You are understanding it correctly. You cannot use x to assign to in a

A puzzle related to nested loops

此生再无相见时 提交于 2019-11-27 04:42:06
For a given input N, how many times does the enclosed statement executes? for i in 1 … N loop for j in 1 … i loop for k in 1 … j loop sum = sum + i ; end loop; end loop; end loop; Can anyone figure out an easy way or a formula to do this in general. Please explain. Grijesh Chauhan First, I written a C code to generate sum: int main(){ int i =0, k =0, j =0, n =0; int N =0; int sum =0; N =10; for (n=1; n <= N; n++){ // unindented code here sum =0; for (i=1; i<=n; i++) for (j=1; j<=i; j++) for (k=1; k<=j; k++) sum++; printf("\n N=%d sum = %d",n, sum); } printf("\n"); } Simple compile and generate

Java : Cartesian Product of a List of Lists

一笑奈何 提交于 2019-11-27 04:21:09
问题 I have a problem that is really kind of a general programming question, but my implementation is in Java, so I will provide my examples that way I have a class like this: public class Foo { LinkedHashMap<String, Vector<String>> dataStructure; public Foo(LinkedHashMap<String, Vector<String>> dataStructure){ this.dataStructure = dataStructure; } public String[][] allUniqueCombinations(){ //this is what I need to do } } I need to generate a nested array from my LinkedHashMap that represents

Is using labels in JavaScript bad practice?

二次信任 提交于 2019-11-27 04:18:44
问题 I just found out about using label s in JavaScript, such as: for (var i in team) { if(i === "something") { break doThis: //Goto the label } else { doThat(); } } doThis: //Label doIt(); I've not heard about this until now and I can't find much information online about it and I'm beginning to think there is a reason for that. It seems to me like this is similar to a GOTO statement in other languages and would be considered bad practice. Would I be right in assuming this? 回答1: Those are loop

Find out which combinations of numbers in a set add up to a given total

佐手、 提交于 2019-11-27 04:00:28
问题 I've been tasked with helping some accountants solve a common problem they have - given a list of transactions and a total deposit, which transactions are part of the deposit? For example, say I have this list of numbers: 1.00 2.50 3.75 8.00 And I know that my total deposit is 10.50 , I can easily see that it's made up of the 8.00 and 2.50 transaction. However, given a hundred transactions and a deposit in the millions, it quickly becomes much more difficult. In testing a brute force solution

Single Line Nested For Loops

我只是一个虾纸丫 提交于 2019-11-27 02:59:24
Wrote this function in python that transposes a matrix: def transpose(m): height = len(m) width = len(m[0]) return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ] In the process I realized I don't fully understand how single line nested for loops execute. Please help me understand by answering the following questions: What is the order in which this for loop executes? If I had a triple nested for loop, what order would it execute? What would be equal the equal unnested for loop? Given, [ function(i,j) for i,j in object ] What type must object be in order to use this for loop

Java 8 nested loops with streams & performance

纵饮孤独 提交于 2019-11-27 01:24:25
问题 In order to practise the Java 8 streams I tried converting the following nested loop to the Java 8 stream API. It calculates the largest digit sum of a^b (a,b < 100) and takes ~0.135s on my Core i5 760. public static int digitSum(BigInteger x) { int sum = 0; for(char c: x.toString().toCharArray()) {sum+=Integer.valueOf(c+"");} return sum; } @Test public void solve() { int max = 0; for(int i=1;i<100;i++) for(int j=1;j<100;j++) max = Math.max(max,digitSum(BigInteger.valueOf(i).pow(j))); System

In python is there an easier way to write 6 nested for loops?

[亡魂溺海] 提交于 2019-11-27 00:22:41
问题 This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this: for y in range(3): for x in range(3): do_something() for y1 in range(3): for x1 in range(3): do_something_else() would there be an easier way to do this? I know that this code works but when you indent instead of using 2 spaces, like me, it can get to be a problem. Oh in the example there were only 4 nested for loops to make things

Alternatives to nested ifelse statements in R

微笑、不失礼 提交于 2019-11-26 22:50:27
问题 Suppose we have the following data. The rows represent a country and the columns ( in05:in09 ) indicate whether that country was present in a database of interest in the given year ( 2005:2009 ). id <- c("a", "b", "c", "d") in05 <- c(1, 0, 0, 1) in06 <- c(0, 0, 0, 1) in07 <- c(1, 1, 0, 1) in08 <- c(0, 1, 1, 1) in09 <- c(0, 0, 0, 1) df <- data.frame(id, in05, in06, in07, in08, in09) I want to create a variable firstyear which indicates the first year in which the country was present in the