Iteration is the repetition of a block of statements within a computer program, usually with mutable state.

learn more… | top users | synonyms

9
votes
1answer
120 views

Recursion vs iteration of tree structure

Some recursive code is part of a particularly slow path of a project. Out of curiosity I was playing around with reimplementing the code using stack context iteration instead of recursion. Below are ...
3
votes
1answer
90 views

Intersection of words

I get a map from DB called doc1 and I also have the ...
6
votes
3answers
103 views

Iteration to recursive function

In my first iterative algorithm, I do this ...
4
votes
2answers
57 views

Is there a way to shorten a set of conditionals like this?

I have a long set of IF statements that basically set a null object Value to 0, I feel that because i am doing the same action each time their has to be a simple ...
2
votes
2answers
118 views

Improve performance while Traversing List

I was trying to write a Java program to compare previous records while traversing. It returns true if element exists previously, else false. e.g. if elements are ...
3
votes
1answer
71 views

Drawing and moving flowers

So I've got this module called risar, which draws. But that's not really of importance. I wrote this code which sets 20 flowers on the background. Now they need to move downwards by 5 units. The code ...
3
votes
2answers
117 views

Optimizing a vector loop

Visual Studio somehow optimizes the below code to be 20 times faster (release with optimization vs. release with no optimization). What could it be doing? ...
3
votes
3answers
592 views

Reversing a string in-place - recursion or iteration?

I am practicing my recursion skills as I think I am really weak in this area. I have written the following Java class whose purpose is to reverse a string but to do it 'in-place' - i.e. no additional ...
3
votes
1answer
109 views

Help improve my my python code for Exponential Smoothing Sumitted to Statsmodel

I just submitted this pull request to statsmodels (a Python toolkit for statistical and econometric modeling). It adds support for exponential smoothing of time ...
31
votes
10answers
2k views

Perform instruction in loop every time except the last time?

The specific issue is this: I am writing to a file and want to output a new line after each line written. If I use a normal loop without any further checks, this will create a blank line at the very ...
7
votes
4answers
169 views

Looping Strategy: Change behaviour after first iteration

I have often encountered the following situation. I want to call a method in a Loop, but want to skip the call at the first run. It do not have to be a method, it also can be some lines of code that I ...
2
votes
1answer
53 views

lexical scope v let in scheme functions

I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time. I got some help on making Peano Multiplication linear -- however ...
4
votes
2answers
125 views

Integer Partition using an iterator

I tried implementing Integer Partition (enumerating all possible sets of whole numbers whose sum is a given integer). Looking at @rolfl's answer, I reasoned that with a stack data structure, we ...
3
votes
1answer
95 views

Is a Recursive-Iterative Method Better than a Purely Iterative Method to find out if a number is prime?

I made this program in C that tests if a number is prime. I'm as yet unfamiliar with Algorithm complexity and all that Big O stuff, so I'm unsure if my approach, which is a combination of iteration ...
2
votes
3answers
114 views

Eliminating nested each

I have an unusual data structure that I need to search - the structure is the result of parsing a JSON file returned from an HTTP request. The top-level object is an Array, but from top to bottom I ...
4
votes
3answers
424 views

Implementing an algorithm that walks the DOM without recursion

Here's a simple algorithm that walks the DOM given a node: ...
4
votes
2answers
134 views

Python line condenser function

This is a function I just wrote that tries to condense a set of strings into grouped lines. It actually works, but looks ugly. Is there a better way to achieve the same thing? Take 4 filtering empty ...
1
vote
3answers
167 views

How to flatten the nested for loops?

The problem I am facing is: I need to interate through a bunch of lists, and there are separated conditions which needs to be satisfied by the list. conditons are not independent. I care about the ...
2
votes
3answers
939 views

More efficient way to retrieve first occurrence of every unique value from a csv column?

A large .csv file I was given has a large table of flight data. A function I wrote to help parse it iterates over the column of Flight ID's, and then returns a ...
2
votes
1answer
201 views

Iterating X times (where X > Y) over Y element dictionary - is there a nicer way of doing this?

I need to write a procedure, that will take a dictionary consisting of server names and their available slots and a number of slots required, and redispatch it over the servers, so that every server ...
3
votes
3answers
985 views

Most efficient way to iterate through arrays/can I use .times method in place of while method?

I created a quick program to test the precision of randomness generated by the .rand method. The primary question is whether I can use the ...
1
vote
0answers
151 views

How to add faster object to an array [duplicate]

I have a list with contentlets and want all languages from every contentlet (I get these with the method languageAPI.getAllValuesByKey(key , contentList). I get a ...
1
vote
3answers
246 views

jQuery iterate or two-step?

I have an array of values of the form: configs = ["26W", "27W", "28W"...] They correspond to some of the a elements in a ...
13
votes
4answers
1k views

Recursive and iterative Euclidean algorithms

Is this code too cryptic? Simple yes or no question. Feedback is optional. ...
1
vote
1answer
384 views

Both an Iterative and a Recursive f(x)

A function f is defined by the rule that f(n) = n if n < 3 and f(n) = f(n-1) + 2f(n-2) + 3f(n-3) if n>=3. Write a recursive and an iterative process for computing f(n). I wrote the ...