nested-loops

Generate all tuples with C - better way than nested loops?

南笙酒味 提交于 2019-12-11 01:33:37
问题 I have an array double x[] of length 11 and a function f(double x[]) . I want to find the minimum of the function f() by discretization. So for given values val1, val2, ..., valn I need a loop trough all the tuples of x in {val_1, ..., val_n}^11. I could easily use 11 nested loops, but is this really the most efficient I could do? Edit: To clarify things: the function f() is defined on an 11 dimensional set. I want to evaluate the function on the vertices of the an 11 dimensional grid. For a

Many nested `for` cycles depending on a variable

ⅰ亾dé卋堺 提交于 2019-12-11 01:29:01
问题 I want to put as many for cycles in themselfs depending of value in a variable. For example, if the variable @var = 1 , I need to perform: for letter1 in @range do something end If the variable @var = 2 : for letter1 in @range for letter2 in @range do something end end If the variable @var = 3 for letter1 in @range for letter2 in @range for letter3 in @range do something end end end Is there a smarter/less code way than this code below? I don't wanna repeat myself all over again. if @var == 1

VBA Multiple loops match conditions

大憨熊 提交于 2019-12-10 22:30:03
问题 I apologize if this is a duplicate as I have been searching and haven't found an answer. I am new to VBA and how they structure loops. I am trying to do a search and compare. I need to compare the values in the first row to see if they match the second row and if not then keep moving on to the next row. See my code below (it runs without error just doesn't find any values that do exist as I can search it manually and find them) This data set could be really large so I want to write this as

PHP read in every 10 out of 100 lines

a 夏天 提交于 2019-12-10 17:55:19
问题 I'm trying to read in a text file of 5000 lines. However I only want to get the first 10 lines of every 100 lines. So line 1-10, line 101 - 110, line 200 - 210 etc. I just cant figure out the logic to use. $count = count(file($text)) for ($i = 0; $i < $count; $i++) { $test = fgets($f_text) } 回答1: Use % 100 and only print lines where $n % 100 > 0 && $n % 100 <= 10 . $lines = file($text); foreach ($lines as $n => $line) { if ($n % 100 > 0 && $n % 100 <= 10) { echo $line; // or whatever } } A

C pointer arithmetic snippet

元气小坏坏 提交于 2019-12-10 16:59:06
问题 I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical constructs C has to offer. The following little bit keeps popping up in my code, with varying values of X and Y : ptr[X]--; while(ptr[X]) { ptr[X]--; ptr += Y; } ptr is of type char * , and I can't really make assumptions about the state of the array at

combinations: avoiding multiple nested foreach

≯℡__Kan透↙ 提交于 2019-12-10 13:58:36
问题 When you need to check/have combinations of array elements, how can you avoid nesting foreach? Example code: $as = array($optionA1, $optionA2) $bs = array($optionB1, $optionB2) $cs = array($optionC1, $optionC2) foreach ($as as $a) { foreach ($bs as $b) { foreach ($cs as $c) { $result = $this->method($a, $b, $c); if ($result) etc } } } Anyone with alternative approaches that can avoid nesting? 回答1: You could write your own Iterator class which implements the Iterator interface. You could then

C++ nested iterators

百般思念 提交于 2019-12-10 10:45:33
问题 Is it OK to have a nested iterator like the following? for (vector<type>::iterator i = list.begin(); i != list.end(); ++i) { for (vector<type>::iterator j = i; j != list.end(); ++j) { ... } } Note that j starts at i , and not list.begin() . Since the iterator is random access, can I guarantee that both i and j will have the same order? is there a better way of doing this? 回答1: Your code is correct. Both iterators will have the same order and incrementing j doesn't affect i as long as you don

Better equivalent of this crazy nested python for loop

◇◆丶佛笑我妖孽 提交于 2019-12-10 03:36:42
问题 for a in map: for b in map[a]: for c in map[b]: for d in map[c]: for e in map[d]: print a+b+c+d+e The above code is used to create all paths of certain length in a graph. map[a] represents the points you can reach from point a. How can I change it to simulate having an arbitrary number of loops? This is like a cartesian product (itertools.product) where at each iteration your choice for the next element is limited to those in map[current_point]. 回答1: map = { 'a': ['b', 'c'], 'b': ['c', 'd'],

Nested WHILE loops in Python

≡放荡痞女 提交于 2019-12-09 18:20:38
问题 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 know I can do this with FOR loop like this >>> for a in range(1,5): for b in range(1,5): print a,b 1 1 1 2 .. .. .. .. // Other lines omitted for brevity 4 4 But, what is wrong with WHILE loop? I guess I

Java Repeated output from nested for loop [closed]

℡╲_俬逩灬. 提交于 2019-12-08 11:17:17
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I created this package, and the problem is when is runs it repeats the same catalog entries between bins. I think the problem is in the detailedInventory method. SHOULD BE: It should create a new random bin item (type, title, artist) for Bin B, not use the same element from Bin A. Other than this, the output is