Questions tagged [iteration]

Iterations are the successive repetitions in loops such as for, foreach or while. Questions with this tag are often concerned about how to best handle a collection of data.

Wiki

Definition: Iteration is the repetition of a block of statements within a computer program.

In most computer programming languages, an iteration is a process that allows code to be executed repeatedly based on a given Boolean condition. Loops can be thought of as an iteration statements.

Examples

  • for
  • foreach
  • while
  • do while

Tag usage

The tag can be used for all programming related problems in implementing iterative approach to call a programming code repeatedly. The tag can be used in problems in implementing iteration using programming constructs and loops. Tag can also be used with other tags , , and .

Read more

10795 questions
3
votes
4 answers

Find monotonic sequences in a list?

I'm new in Python but basically I want to create sub-groups of element from the list with a double loop, therefore I gonna compare the first element with the next to figure out if I can create these sublist, otherwise I will break the loop inside…
taonico
  • 97
  • 3
  • 9
3
votes
5 answers

Evaluating Polynomial coefficients

I'm trying to write a function that takes as input a list of coefficients (a0, a1, a2, a3.....a n) of a polynomial p(x) and the value x. The function will return p(x), which is the value of the polynomial when evaluated at x. A polynomial of degree…
Snarre
  • 597
  • 5
  • 14
  • 22
3
votes
3 answers

Iterating with IEnumerable vs List

I just found a couple of c# code refactoring examples on the internet, and stumbled upon this particular piece of code. Can anyone explain to me, why Method2() would be better than Method1()? Method #1 - Doing multiple iterations on…
T Jensen
  • 85
  • 1
  • 6
3
votes
1 answer

Retrieving a specific set element in Python

Essentially this is what I'm trying to do: I have a set that I add objects to. These objects have their own equality method, and a set should never have an element equal to another element in the set. However, when attempting to insert an element,…
Mala
  • 14,178
  • 25
  • 88
  • 119
3
votes
1 answer

FindBug says this concurrent map doesnt need synchronized

So I've recently discovered FindBug, but it's making me think I don't know what I'm doing in a couple of places. This is one of them private Map map = new ConcurrentHashMap(); public void method1(){ …
Jay Smith
  • 471
  • 3
  • 17
3
votes
3 answers

For Each Loop Iteration Count

Can we keep track of our iteration in our loop when we use a For Each? I like to use the For Each loops for looping through my objects but I cannot seem to find a way to keep an index of where I'm at in the loop. Unless of course I create my own ...
user2379049
3
votes
4 answers

How to re-assign items in a list in Python?

I want to re-assign each item in a list in Python. In [20]: l = [1,2,3,4,5] In [21]: for i in l: ....: i = i + 1 ....: ....: But the list didn't change at all. In [22]: l Out[22]: [1, 2, 3, 4, 5] I want to know why this…
zfz
  • 1,597
  • 1
  • 22
  • 45
3
votes
3 answers

What are the differences between "each", "foreach", "collect" and "map"?

It seems like there are a lot of ways to loop over an Enumerable in Ruby. Are there any differences between each, foreach, or in collect, map or other similar methods? Or is there a reason I shouldn't use certain methods in certain situations?
Addison
  • 3,791
  • 3
  • 28
  • 48
3
votes
4 answers

How to write a function to process arrays as input element by element and returns array

I am trying to write a function and I want it to return one element when the input is element and an array of outputs if the input is array such that each element of output array is associated with the same place in input array. I am giving a dummy…
Cupitor
  • 11,007
  • 19
  • 65
  • 91
3
votes
3 answers

Do lots of operations on multiple objects of the same type

I have two graphical objects (say some kind of Tables) and I want to set their styles. The trivial code is as follows: table1.BorderWidth = 2; table1.BorderColor = Color.GloriousPink; table2.BorderWidth = 2; table2.BorderColor =…
John NoCookies
  • 1,041
  • 3
  • 17
  • 28
3
votes
4 answers

Assign value to iteration variable in C#?

I have the following code in C# utilizing foreach. In one loop I am modifying a List, and in another, a string array. We can't directly assign a value or null to the iteration variable, but we can modify its properties, and the modifications are…
Brij
  • 11,731
  • 22
  • 78
  • 116
3
votes
4 answers

Using enumerateObjectsAtIndexes or a for-loop to iterate through all indexes of an NSArray BEFORE a given index

What's the most concise way to iterate through the indexes of an NSArray that occur before a given index? For example: NSArray *myArray = @[ @"animal" , @"vegetable" , @"mineral" , @"piano" ]; [myArray enumerateObjectsAtIndexes:@"all before index…
zakdances
  • 22,285
  • 32
  • 102
  • 173
3
votes
5 answers

Most efficient way to add new keys or append to old keys in a dictionary during iteration in Python?

Here's a common situation when compiling data in dictionaries from different sources: Say you have a dictionary that stores lists of things, such as things I like: likes = { 'colors': ['blue','red','purple'], 'foods': ['apples',…
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
3
votes
8 answers

How would I have an array of teams play each other all once and once only?

I am not sure how to have them play each other once and only once. For example, here is a sample of teams (I will add scoring later, looking for mostly logic help): class MyTeams { String teamName; int wins; int losses; } public…
K2SO Ghost Spirit
  • 1,291
  • 3
  • 11
  • 11
3
votes
2 answers

Looking for a more efficient way to reorganize a massive CSV in Python

I've been working on a problem where I have data from a large output .txt file, and now have to parse and reorganize certain values in the the form of a .csv. I've already written a script that input all the data into a .csv in columns based on what…
Adam Barthelson
  • 1,088
  • 1
  • 11
  • 20