Questions tagged [iteration]
Iteration is the repetition of a block of statements within a computer program, usually with mutable state.
232
questions
38
votes
11answers
31k 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 ...
29
votes
2answers
61k 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 ...
24
votes
7answers
9k views
FizzBuzz in Javascript
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is ...
20
votes
5answers
4k views
Count Even Digits In Number
I was asked to complete this CodeStepByStep problem. Here's a quick summary:
Write a function named count_even_digits that accepts two integers as
parameters ...
19
votes
1answer
2k views
for_each for tuple-likes
I've implemented a C++14 for_each for tuple-like objects. It's similar to std::for_each in that it also returns the functor once ...
17
votes
4answers
2k views
Recursive and iterative Euclidean algorithms
Is this code too cryptic? Simple yes or no question. Feedback is optional.
...
12
votes
10answers
7k views
Tests for palindromes in C and C++
A question I've been asked on interactive phone screens is to code a test to determine whether a string is
a palindrome or not. I am providing two examples here: one in C and one in C++. They both ...
12
votes
3answers
7k views
Implementing an algorithm that walks the DOM without recursion
Here's a simple algorithm that walks the DOM given a node:
...
12
votes
2answers
6k views
Progress report for a long-running process using 'yield' [closed]
I have a function that processes a large amount of data. I call this function as part of a wider process via a command line script in which many similar but shorter jobs are conducted in sequence.
I ...
11
votes
3answers
1k views
Generating sequential alphanumeric values that match a certain pattern
I'm working on generating large volumes of test data for some performance testing. Part of this is going to involve generating a lot of values that represent an "MBI", which is a certain kind of alpha-...
11
votes
3answers
745 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 ...
11
votes
2answers
566 views
Parallelized for loop in Bash
I am using a Bash script to execute a Python script multiple times. In order to speed up the execution, I would like to execute these (independent) processes in parallel. The code below does so:
<...
10
votes
4answers
1k views
Shuffling multiple iterators in order
I have a Python function w/ specified functionality, and two proposed approaches - which do you prefer, and why? (performance, simplicity, clarity, etc) (both use ...
10
votes
3answers
3k views
Find the Nth number divisible by only 2,3,5, or 7
I recently participated in a small friendly competition and this was one of the questions:
A number whose only prime factors are 2, 3, 5 or 7 is called a humble
number. The first 20 humble ...
10
votes
2answers
342 views
Comma formatted list printer
I'm working on a list practice project from the book "Automate the Boring Stuff with Python" which asks for this:
Write a function that takes a list value as an argument and returns a
...
10
votes
1answer
4k views
Remove all values duplicates from list
I'm new to programming in general, started on boxing day. I'm trying to
learn python.
The list remove method only removes the first instance of the value found.
In the code below I attempted to ...
9
votes
2answers
7k views
Filtered Iterator
I have written a wrapper for Java's Iterator class using Java 7, which is designed to only iterate items that match a certain filter.
When the filter is null, all ...
8
votes
3answers
3k 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.
...
8
votes
4answers
8k 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:
...
8
votes
2answers
2k views
Interleave two lists of strings
I've got a fairly simple task here, but I think it's worth trying to perfect the implementation, because I can probably learn a thing or two about using .NET's enumerables correctly in the process.
I ...
8
votes
3answers
7k 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 ...
8
votes
2answers
9k views
Median Filter Implementation In Python
I implemented median filter in Python in order to remove the salt & pepper noise from the images. It is working fine and all but I would love to hear your advice or opinions.
...
8
votes
2answers
370 views
Generate Braille characters from a dictionary
Problem
I would like to generate Braille characters from a dictionary as below which store the n-th active bits:-
...
8
votes
1answer
232 views
Saving and resuming position while iterating over a container
Background:
I'm in the process of writing a relatively simple behavior tree driven AI system for a game that I'm working on. Basically, the behavior tree is made up of individual gameplay tasks (move,...
8
votes
1answer
1k 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 ...
7
votes
4answers
500 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 ...
7
votes
4answers
358 views
Finding the first pair of prime numbers that have some specified difference
I tried printing the first pair of prime number in between a list of numbers with a particular difference. But my run time is still too high (for large range of numbers). I want to reduce the run time ...
7
votes
1answer
248 views
Map unique raw words to a list of code words
Problem
Write a function that replaces the words in raw with the words in code_words such that the first occurrence of each ...
7
votes
1answer
105 views
A database of books, part 1
Backstory
I collect books. A lot of books. Whitepapers, (programming) cookbooks, transcripts, important letters, overviews, you name it. Usually in PDF, otherwise my house would be too small.
To keep ...
7
votes
2answers
5k views
Generate 6 random numbers for lottery in PHP
This program generates 6 random numbers between 0 and 45 and the numbers cannot be repeated. Can this code be improved?
...
7
votes
5answers
3k views
Find number repeated odd times in array
I completed the following exercise:
You are given an array of repeating numbers. All numbers repeat in an even way, except for one. Find that odd occurring number.
...
7
votes
2answers
272 views
Search and Replace (FreeCodeCamp intermediate algorithm scripting)
I completed this challenge Search and Replace and passed the tests:
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the ...
7
votes
2answers
1k views
An iterator returning all possible partitions of a list in Java
Given a list of \$n\$ objects and an integer \$k \in \{ 1, 2, \dots, n \}\$, this iterator generates all possible ways of partitioning the elements in the list into exactly \$k\$ disjoint, non-empty ...
6
votes
2answers
360 views
Choosing a clubName
Please can someone advise me, how I could use a loop for this code, in the interests of code reuse.
My code is as follows
...
6
votes
3answers
835 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 ...
6
votes
5answers
369 views
CombinedIterable for Java
This class combines multiple instances of Iterable into one and iterates through them in the order they are given to ...
6
votes
2answers
541 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 ...
6
votes
2answers
777 views
Iterate faster through the Android file system and find specific files using java multithreading
I'm trying to extract specific files and visualise them in a list/grid. For that purpose I'm starting a Service to do the background job, and when it finishes it fires an Intent which will be captured ...
6
votes
2answers
93 views
6
votes
2answers
6k 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 ...
6
votes
3answers
549 views
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 we'...
6
votes
1answer
618 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 \$[3,...
6
votes
1answer
4k views
Check if multiple lists contain an element from a large external list
I have a list of lists (many tokenised sentences). For anyone who doesn't know what tokenised sentences are, my list is like so:
...
6
votes
1answer
166 views
Checking game objects versus each other to determine targets, comparing arrays
I have a flight class that looks like this:
...
6
votes
1answer
3k 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 (...
6
votes
2answers
11k views
Iterating an array of nodes, each of which can have children
I've written some C# (7) code to iterate through a Node[], listing all of its nodes in order (i.e. child elements are listed immediately after the parent).
Each <...
6
votes
1answer
714 views
Counting nodes in a binary tree with odd values, using an iterative algorithm
I've to simulate exactly a recursive algorithm with an iterative one.
Assuming that I have a binary search tree that contains only a key and two references at his right and left child I want to do ...
6
votes
1answer
69 views
Undoing corrections to a big dataframe
I have 2 dataframes. The first one (900 lines) contains corrections that have been applied to a deal. The second dataframe (140,000 lines) contains the list of deals with corrected values. What I am ...
6
votes
1answer
431 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 ...