nested-loops

Big O for 3 nested loops

落花浮王杯 提交于 2019-11-29 13:39:15
Another Big O notation question...What is the Big O for the folling code: for (int i = n; i > 0; i = i / 2){ for (int j = 0; j < n; j++){ for (int k = 0; k < n; k++){ count++; } } } My Thoughts: So breaking it down, I think the outside loop is O(log2(n)) , then each of the inner loops is O(n) which would result in O(n^2 * log2(n)) Question #1 is that correct? Question #2: when combining nested loops is it always just as simple as mulitply the Big O of each loop? When loop counters do not depend on one another, it's always possible to work from the inside outward. The innermost loop always

Alternative to nesting for loops in Python

淺唱寂寞╮ 提交于 2019-11-29 11:39:32
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 loops in Python ( I have certainly seen 2 nested loops before). Secondly, if this is too heavily nested,

Breaking out of an outer loop from an inner loop in javascript

爱⌒轻易说出口 提交于 2019-11-29 11:07:29
while(valid){ for(loop through associative array){ if(!valid){ break; } } } I have tried to find a way to break out of the while loop from the if statement. So far, the best method seems to be the goto method that is non-existant in Javascript. What is the best way to cause the if statement to break out of both of the loops it is nested in? Thanks in advance for the help! Depending on what your conditionals are, it should be easy to set the iterator of your for-loop to something that would break it, and set your while condition to false. For example, while(someBoolean){ for(var i = 0; i < size

How to make a IEnumerable method parallel method

ε祈祈猫儿з 提交于 2019-11-29 06:45:52
Following to this post , I want parallelize this method : public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers) { foreach (var cpt in computers) { foreach (var log in cpt.GetLogs()) { yield return log; } } } I want the method "yield returns" a log when one of the method GetLogs is finished. If I have 4 computers which returns : Computer 01 : "a", "b", "c", "d", "e" Computer 02 : "1", "2", "3", "4", "5" Computer 03 : "alpha", "beta", "gamma", "delta", "epsilon" Computer 04 : "I", "II", "III", "IV", "V" With the "sequential method", the output is : a b c d e 1 2 3 4 5 alpha

Nested Loop Python

寵の児 提交于 2019-11-29 05:13:43
count = 1 for i in range(10): for j in range(0, i): print(count, end='') count = count +1 print() input() I am writing a program that should have the output that looks like this. 1 22 333 4444 55555 666666 7777777 88888888 999999999 With the code I have written I am pretty close, but the way my count is working it just literally counts up and up. I just need help getting it to only count to 9 but display like above. Thanks. You're incrementing count in the inner loop which is why you keep getting larger numbers before you want to You could just do this. >>> for i in range(1, 10): print str(i)

How to break from nested loops in Ruby?

拜拜、爱过 提交于 2019-11-29 02:47:49
assume the following ruby code: bank.branches do |branch| branch.employees.each do |employee| NEXT BRANCH if employee.name = "John Doe" end end NEXT BRANCH is of course pseudocode. is there a way that i can break out of a parent loop, the way one can do so in Perl, for example (by employing loop labels)? thanks in advance. Catch and throw might be what you are looking for: bank.branches do |branch| catch :missingyear do #:missingyear acts as a label branch.employees.each do |employee| (2000..2011).each do |year| throw :missingyear unless something #break out of two loops end end end #You end

Java 8 nested loops with streams & performance

左心房为你撑大大i 提交于 2019-11-28 19:42:01
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.out.println(max); } My solution, which I expected to be faster because of the paralellism actually took

.BAT break out of multiple nested loop, after finishing the respective list

▼魔方 西西 提交于 2019-11-28 14:45:52
I know breaking out of a nested loop is fairly easy, however, I'm not sure how to do it when I'm working with multiple lists of servers. Here's the scenario: Goal: Search for sessions on a server matching a specific user ID, and also kill any disconnected sessions found Problem: I have multiple lists of farms. I want to cycle through the lists until I find the users session, and then stop when that list is finished (not stop when the session is cleared, they may have multiple sessions in the farm). Farmlist1.txt farmlist2.txt farmlist3.txt If the session is found in farmlist2.txt, I want to

Speed up Python2 nested loops with XOR

浪尽此生 提交于 2019-11-28 14:02:32
问题 The answer of the question this is marked duplicate of is wrong and does not satisfy my needs. My code aims to calculate a hash from a series of numbers. It is easier to understand the structure in the form of a matrix. If I have 16 numbers starting from 29 the structure will be: (start=29, length=4) 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44 The given algorithm specifies the the hash will be the XOR of the numbers given in bold: 29, 30, 31, 32, //, 33, 34, 35, //, 36, 37,

Limit input data to achieve a better Big O complexity

心不动则不痛 提交于 2019-11-28 12:58:57
问题 You are given an unsorted array of n integers, and you would like to find if there are any duplicates in the array (i.e. any integer appearing more than once). Describe an algorithm (implemented with two nested loops) to do this. The question that I am stuck at is: How can you limit the input data to achieve a better Big O complexity? Describe an algorithm for handling this limited data to find if there are any duplicates. What is the Big O complexity? Your help will be greatly appreciated.