nested-loops

How to insert into nested vector without invalidating iterator(s)

I have some boolean expressions to evaluate and process. Maybe this would have all been better with Boost, but I'm still learning STL and didn't go that way. I'm now learning about iterator validation, or INvalidation as the case may be. Is there a way to insert a new element into this nested vector below safely? If you don't want t...

Number of nested loops at runtime

I am trying to output all the possible unique integer combinations from 1 to max for a set number of integers. So for 3 integers and a max of 4 I would get: 123 124 134 234 I am doing this with nested for loops but I want to allow the user to input the number of integers at runtime. right now I have if(numInts >6); for(int x = 1; x...

Why should I avoid using Java Label Statements?

Everywhere across the internet people say that you should avoid using label statements in java. However, I find them very useful in some cases, namely nested loops. I cannot find satisfactory answers as to why not to use them. I think that alternatives to labels often reduce either readability, or performance, or both. So what makes lab...

Populate ruby Array1 with Array2 String element if and only if Array2 element matches Hash value(not key).

I have a ruby hash: VALS = { :one => "One", :two => "Two" } and an Array: array2 = ["hello", "world", "One"] Question: How can I populate a new array1 so that it only pulls in any values in array2 that match exactly the values in VALS? For example, I have tried: array2.each_with_index do |e,i| array1 << e if VALS[i] ~= e end Alon...

python skipping inner loop in nested for loop

I am using some python to do some variable name generation. For some reason I am only getting part of what I need. import sys import csv params = csv.reader(open('params.csv'), delimiter=',', skipinitialspace=True) flags_r = [] flags_w = [] numbers_r = [] numbers_w = [] station = ['AC1','DC1','DC1'] drive = ['','Fld','Arm'] for i in ...

Recursive function in java - N nested loops with changing indicies

Similar to this: http://stackoverflow.com/questions/426878/is-there-any-way-to-do-n-level-nested-loops-in-java I want to create a recursive function, which generates N nested loops, where the indicies depend on the depth of the loop. So basically, I want to do this recursively: // N = 3, so we want three nested loops for(int i1 = 0; i...

Printing a nested for-loop pattern in java

Goal: 1234 2345 3456 4567 5678 i have the pattern down but it doesnt println after length(4): int i; int a; for (i = 1; i <= 5; i++) { for (a = i;a<=i+3;a++) { System.out.print(a); } } My output is: 12342345345645675678 ...

Two collections and a for loop. (Urgent help needed) Checking an object variable against an inputted String value. Nullpointerexception

Hi there, I'm relatively new to java, I'm certain the error is trivial. But can't for the life of me spot it. I have an end of term exam on monday and currently trying to get to grips with past papers! Anyway heregoes, in another method (ALGO_1) I search over elements of and check the value H_NAME equals a value entered in the main. W...

Efficiency of nested Loop

See the following snippet: Long first_begin = System.currentTimeMillis(); // first nested loops for (int i = 0; i < 10; i++) { for (int j = 0; j < 1000000; j++) { // do some stuff } } System.out.println(System.currentTimeMillis() - first_begin); // second nested loops Long seconde...

I want all three shapes on the same line...please help...!!!!

#Top half of triangle for rows in range (5): for row in range (12): print("-", end='') print() for row in range (5): stars=0 while stars<=row: print("*", end='') stars=stars+1 print() for row in range(5): star=4 while star>=row: print("*", end='') ...

Trying to set up nested while loops using a boolean switch

I'm trying to set up a while loop that will ask the user for the employee name, hours worked and hourly wage until the user enters 'DONE'. Eventually I'll modify the code to calculate the weekly pay and write it to a list, but one thing at a time. The problem is once the main while loop executes once, it just stops. Doesn't error out but...

Break the nested(double) loop in python

I use the following method to break the double loop in Python. for word1 in buf1: find = False for word2 in buf2: ... if res == res1: print "BINGO " + word1 + ":" + word2 find = True if find: break Is there better way to break the double loop? ...

Nasty deep nested loop in Rails

I have this nested loop that goes 4 levels deep to find all the image widgets and calculate their sizes. This seems really inefficient and nasty! I have thought of putting the organization_id in the widget model so I could just call something like organization.widgets.(named_scope), but I feel like that's a bad short cut. Is there a bett...

string combinations

I would like to generate a combination of words. For example if I had the following list: {cat, dog, horse, ape, hen, mouse} then the result would be n(n-1)/2 cat dog horse ape hen mouse (cat dog) (dog horse) (horse ape) (ape hen) (hen mouse) (cat dog horse) (dog horse ape) (horse ape hen) etc Hope this makes sense...everything I found...

can someone please show me how to convert this pseudocode to python using nested loops

N=5 For(rate=0.05, rate <= 0.15, rate = rate +0.05) For(principle=1000, principle <= 1500, principle=principle + 1000) simple = principle * (1 + rate * N) #Where N is the number of years compound = principle * (1 + rate) ^ N print simple + " " + compound End For End For ...

Jquery - $(this) in nested loops

Hi, I can't figure out how to do something in Jquery. Let's say I have a form with many select drop-downs and do this... $('#a_form select').each(function(index) { }); When inside this loop I want to loop over each of the options, but I can't figure out how to do this, is it something like this....? $('#a_form select').each(fu...

Dividing sections inside an omp parallel for : OpenMP

Hi, I have a situation like: #pragma omp parallel for private(i, j, k, val, p, l) for (i = 0; i < num1; i++) { for (j = 0; j < num2; j++) { for (k = 0; k < num3; k++) { val = m[i + j*somenum + k*2] if (val != 0) for (l = start; l <= end;...

Need help on nested loop of queries in php and mysql?

Hi, I am trying to get do this: <?php $good_customer = 0; $q = mysql_query("SELECT user FROM users WHERE activated = '1'"); // this gives me about 40k users while($r = mysql_fetch_assoc($q)){ $money_spent = 0; $user = $r['user']; // Do queries on another 20 tables for($i = 1; $i<=20 ; $i++){ $tbl_name = 'd...

Alternative to Nested Loop For Comparison

I'm currently writing a program that needs to compare each file in an ArrayList of variable size. Right now, the way I'm doing this is through a nested code loop: if(tempList.size()>1){ for(int i=0;i<=tempList.size()-1;i++) //Nested loops. I should feel dirty? for(int j=i+1;j<=tempL...

Nested WHILE loops in Python

I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> while a < 4: a=a+1 while b < 4: b=b+1 print a, b 1 1 1 2 1 3 1 4 I am expecting the outer loop to loop through 1,2,3 and 4. And I kno...