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

learn more… | top users | synonyms

6
votes
1answer
65 views

Print permutations in an iterative way

I've written the following code to print all the permutations of a list. For example: \$[1, 2, 3]\$ has these permutations: \$[1,2,3]\$, \$[1,3,2]\$, \$[2,1,3]\$, \$[2,3,1]\$, \$[3,1,2]\$, and ...
8
votes
3answers
726 views

Finding the percentage of even numbers in an array

I wrote a method which computes the ratio of even to odd numbers in an array. I know it's a simple piece of code, but I wanted to see if you have any feedback for improving it. ...
3
votes
2answers
93 views

Epilogue to a binary search to find the range of matching indexes

I am trying to find out the best practice for writing a loop which does one check and at the same time has to increment a value so that the while loop will ...
1
vote
1answer
57 views

Deleting an item from a Set while iterating

I know that I'm not suppose to alter a Set as I iterate through it. But, in the deleteRow(String counter, String reading) method below, doing just that will make ...
0
votes
1answer
35 views

Adding a node to a Binary Search Tree and printing the path to it

I wrote BST insertion code with the book. The book uses recursion, but I just want to know how I can do it without recursion. My code is working, but I don't know if it is correct. ...
3
votes
2answers
87 views

Summing the digits of a sum of digits of a sum

I'm a beginner in Python, and computer languages in general, and I'm totally stumped on how to format this iteration into a function. The iteration takes the sum of someone's birth year, month, and ...
3
votes
0answers
82 views

Print the nodes of binary tree column-wise, using recursion and iteration

I am trying to code for both recursive and iterative approach to a problem. Below is a recursive code to print the nodes of binarytree column wise. Basically I have count of root as 0 and if I move ...
6
votes
3answers
443 views

Nested loop to render tiles in a grid

This code works but I'm not really sure if I wrote it good enough. It seems a bit vague but I can't really assess it properly. I'm particularly concerned with the ...
1
vote
0answers
20 views

While loops in SCSS to produce circles for an animation

I've finally written my first loop in SCSS, trying to avoid it for quite some time since I thought it was unnecessary (was writing vanilla CSS not so long ago, so I was used to manual work). However, ...
3
votes
0answers
56 views

Storing large amount of iterations (of multiple variables) in a single object

I am writing a simulation program in C++ which must be able to iterate over multiple variables over multiple classes (according by user input). The basic design of the device is as following: ...
4
votes
2answers
347 views

Square Root Calculator

I have now written a simple square root calculator using the division method: ...
0
votes
0answers
44 views

Parallel for loop in Java - follow-up 2

The previous iteration at Parallel for loop in Java 8 - follow-up. The changes are as follows: MyTask is removed. Synchronization removed. Now the user is ...
1
vote
1answer
504 views

Parallel for loop in Java 8 - follow-up

The previous and initial iteration at Parallel for loop in Java 8. Changes are as follows: ParallelLoopBody removed; ...
3
votes
2answers
210 views

Parallel for loop in Java 8

This is my first attempt to provide some syntactic sugar for doing mutually independent loop iterations in parallel. Thanks to Java 8 lambdas, I can write the parallel loops in pretty elegant fashion ...
11
votes
3answers
539 views

Predicting the next pseudorandom value

The archived material for the Stanford University course on cryptography at coursera.org includes a problem where you have to predict the next output of a weak PRG. It can be briefly restated as ...
1
vote
1answer
51 views

Changing the id field to an _id field for the lowest tweet id

I am collecting tweets using a Python script and I want to store them in a MongoDB database. I want to find out the lowest tweet id and for each tweet, I want to change the ...
1
vote
1answer
886 views

Flattening a HashMap of (String → ArrayList) to an ArrayList

I have one HashMap<String, ArrayList<String>> and I want to concatenate a key with every item in its ArrayList. ...
4
votes
1answer
34 views

Finding the next XML element

I am looking for a better way to do a while cycle with the variable update. The background is that I'm writing a StAX-line XML reader in JavaScript and want to ...
6
votes
1answer
1k views

Implementation of Tower of Hanoi iterative procedure

I have been working last night on implementing Tower of Hanoi without using recursion. I have found an algorithm on Wikipedia about the same topic here. I have implemented it and it's working fine ...
1
vote
2answers
67 views

Looking for matching names among two text files

I'm trying to enhance the execution speed of my below code. I am using only vanilla JavaScript. I would be willing to bring in additional libraries and plugins as long as they will enhance the overall ...
0
votes
1answer
89 views

Iterating over each two consecutive items of a collection [closed]

Given a collection of Foos, I'd like to return a new Collection of Bars, according to the following logic: Each Foo has a Type (let's leave it as an int for now, even though it is an Enum in reality) ...
4
votes
1answer
80 views

Optimize iteration output to text file

I've written a piece of code which combines given letters in groups of n letters, and writes all the combinations in a text file. This is my first attempt and I wonder if there is an optimal way to do ...
6
votes
1answer
1k views

Missing level of technical depth (Recursive to Iterative)

I recently have given a coding test in a reputable IT company. There were three coding questions. They refused me by saying that as we felt they didn't demonstrate the level of technical depth ...
6
votes
2answers
2k views

MemoryError in Python while combining multiple JSON files and outputting as single CSV

I have a number of JSON files to combine and output as a single CSV (to load into R), with each JSON file at about 1.5GB. While doing a trial on 4-5 JSON files at 250MB each, the code works when I ...
4
votes
2answers
130 views

Iterate through series of timers

I wrote a short program for my exercise routine. It takes a JSON array of exercise names and duration (seconds) and puts it on the screen. The JSON looks like this: ...
5
votes
4answers
444 views

Iterate over two collections of equal length

What's the best way to iterate over two collections of equal length? Currently I'm using this: ...
0
votes
1answer
95 views

Understand recursion

I'm trying to improve my understanding of recursive and iterative processes. I've started with SICP, but have branched off to try and find a few exercises when I started having difficulty. Currently ...
12
votes
1answer
7k 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
117 views

Intersection of words

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

Iteration to recursive function

In my first iterative algorithm, I do this ...
4
votes
1answer
69 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 ...
3
votes
2answers
230 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
418 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
289 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? ...
6
votes
3answers
4k 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
640 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 ...
32
votes
11answers
4k 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
247 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
72 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
210 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
193 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 ...
8
votes
1answer
555 views

Optimizing iteration through linked lists

I have a data structure composed of several std::lists, each sorted by a specific criterion. When a new item is inserted into this data structure, there is a need ...
2
votes
3answers
1k 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 ...
8
votes
4answers
2k views

Implementing an algorithm that walks the DOM without recursion

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

Coordinating camera stage movement and data readout

The purpose of this code is important, so here's a quick rundown. This code runs controls a physical machine which utilizes a camera and a moving stage holding an object to be imaged. Imaging can be ...
4
votes
2answers
162 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 ...
0
votes
1answer
311 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
3k views

Retrieving the 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 IDs, and then returns a dictionary containing the index and value ...
2
votes
1answer
271 views

Iterating X times (where X > Y) over Y element dictionary

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
2k 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 ...