nested-loops

How to find out which nested for loop is better?

风格不统一 提交于 2019-12-31 03:49:32
问题 Somebody asked me a question: Which one is the fastest among the below two scenario: Case 1: assume int count = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { count++; } } Case 2: assume int count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { count++; } } In both the scenario's the final valuse of count will be 50. But I am not sure which one will be faster? I think CASE II is faster but not sure... It will be great if anyone can throw some light on it.

Determining as a function of n how often the statement incrementing the variable count is performed

可紊 提交于 2019-12-31 00:44:31
问题 Ok so I'm new to analyzing algorithms and would really appreciate any helpful tips that can be shared on how to go about this. I am trying to determine how many times count is incremented as a function of n. I have ran it in an ide and for values 1-7 the output is 1,3,6,10,15,21,28. Im just unsure how to write this as a function of n? Thanks. The loop is as follows: for(int i = 1 ; i <= n ; i++){ for (int j = 1 ; j <= i ; j++) { count++; } } 回答1: The aim of this type of exercise is to teach

Java Big O notation of 3 nested loops of log(n)

穿精又带淫゛_ 提交于 2019-12-30 18:43:29
问题 What would the Big O notation be for the following nested loops? for (int i = n; i > 0; i = i / 2){ for (int j = n; j > 0; j = j / 2){ for (int k = n; k > 0; k = k / 2){ count++; } } } My thoughts are: each loop is O(log2(n)) so is it as simple as multiply O(log2(n)) * O(log2(n)) * O(log2(n)) = O(log2(n)^3) 回答1: Yes, that is correct. One way to figure out the big-O complexity of nested loops whose bounds do not immediately depend on one another is to work from the inside out. The innermost

Creating a Christmas Tree using for loops

匆匆过客 提交于 2019-12-30 04:33:10
问题 I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code: for(int i=1;i<=10;i++){ for(int j=10;j>i;j--){ System.out.println(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); } for(int l=10;l<=1;l++){ for(int h=1;h<=10;h++){ System.out.print(" "); } } System.out.println(); } What I am trying to do is: * *** ***** ******* 回答1: Try

Break the nested (double) loop in Python [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-29 11:45:41
问题 This question already has answers here : How to break out of multiple loops? (29 answers) Closed 3 years ago . 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 a better way to break the double loop? 回答1: Probably not what you are hoping for, but usually you would want to have a break after setting find to True for word1 in buf1: find =

Alternative to nesting for loops in Python

两盒软妹~` 提交于 2019-12-29 08:08:06
问题 I've read that one of the key beliefs of Python is that flat > nested. However, if I have several variables counting up, what is the alternative to multiple for loops? My code is for counting grid sums and goes as follows: def horizontal(): for x in range(20): for y in range(17): temp = grid[x][y: y + 4] sum = 0 for n in temp: sum += int(n) print sum # EDIT: the return instead of print was a mistype This seems to me like it is too heavily nested. Firstly, what is considered to many nested

NodeJS, Promises and performance

为君一笑 提交于 2019-12-28 06:53:32
问题 My question is about performance in my NodeJS app... If my program run 12 iteration of 1.250.000 each = 15.000.000 iterations all together - it takes dedicated servers at Amazon the following time to process: r3.large: 2 vCPU, 6.5 ECU, 15 GB memory --> 123 minutes 4.8xlarge: 36 vCPU, 132 ECU, 60 GB memory --> 102 minutes I have some code similair to the code below... start(); start(){ for(var i=0; i<12; i++){ function2(); // Iterates over a collection - which contains data split up in

Python: Dynamic nested for loops each with different range

岁酱吖の 提交于 2019-12-25 18:24:56
问题 I want to create a code that can iterate over a dynamic number (N) of nested loops each with different range. For example: N=3 ranges=[[-3, -2, -1, 0, 1, 2, 3], [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], [-3, -2, -1, 0, 1, 2, 3]] for x in ranges[0]: for y in ranges[1]: for z in range[2]: variable=[x, y, z] Im new to python. As I went over similar questions posted here I have the understanding that this can be done with recursion or itertools. However, none of the answers posted solve this

python nested for-loop not executing beyond first

半世苍凉 提交于 2019-12-25 07:48:27
问题 This script is meant to read through a file and take in the number (numA) and the text next to it (sourceA). It then uses this and compares it to every other line in the file. If a match in "nums" is found but not in sources, it writes the num to a file along with the sources it appears in. with open(sortedNums, "r")as sor: for line in sor: NumsA, sourceA = line.split('####') for line in sor: if '####' in line: NumsB, sourceB = line.split('####') if (NumsA == NumsB) & (sourceA != sourceB):

Nested For Next Loops: Outer loop not iterating

北慕城南 提交于 2019-12-25 03:16:23
问题 I have a range of data from A2:A34 with various names in it that I need to copy to the range E9:E14 . I only need to copy and paste unique names (I don't need a double of the same name). I am pretty sure using a nested For Next loop is the way to go but I'm having trouble getting the outer loop to go to the next iteration. Right now this is only giving me the last name in the in range A2:A34 repeated in E9:14 . I was looking into using Exit For but when I added that in the code, the outer