Share Your Experience: Take the 2024 Developer Survey

# Questions tagged [iteration]

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

264 questions
Filter by
Sorted by
Tagged with
294 views

### Calculate the base 10 log with loops in Java without using Math

I'm looking for a simple way to calculate the logarithm without using java.lang.Math, just out of interest. Here's my try (I adopted some code parts from this ...
346 views

### Another cash machine with special bill values in Java

Task: An ATM dispenses banknotes with the following values/denominations: {5, 30, 35, 40, 150, 200}. The user should be able to repeatedly enter an amount divisible by 5 without leaving a remainder. ...
1 vote
87 views

### Code that performs 2500 simulations by running a regression after randomly assigning 10 friends to each observation in a data took 20 days to run

I am running the following code to conduct a simulation. I have a dataset of around 100K observations. The simulation does the following: It randomly assign 10 "friends" to each observation ...
• 11
1 vote
96 views

### Electro-thermal simulation

I'm working on an electro-thermal simulation with variable parameters. Model require the calculation of all the electrical and thermal quantities at each time step, between a starting and ending time. ...
• 21
1k views

### Determine if it is EU summer time in Java

I wanted to write a utility method for learning purposes to determine if a given date (year, month and day) falls within EU summer time or not, without using any of the Java library methods. Is here ...
1 vote
90 views

### Shrink an array of double elements (uniformly)

Is there a way to avoid the first loop in the method shrink? (if and while...) The utility ...
768 views

...
• 153
1k views

### Knock down tall buildings

I'm trying to solve the following problem: As you know Bob likes to destroy the buildings. There are N buildings in a city, ith building has ai floors. Bob can do the following operation. Choose a ...
• 212
1 vote
72 views

### Increment sequence value for multiple styles

I've found several examples of code to increment various sequence values but they are usually specific to either alpha characters or numeric characters. I wanted something that could increment a ...
• 155
185 views

### Increasing writing speed to file inside a for-loop for large files

I have a .csv file containing 100 millions records, I would to create multiple files from this file, after certain condition satisfied in each line, I came to this code below, but it is slow, I think, ...
• 309
224 views

### Count the number of mismatches between two arrays [closed]

This function may compute the amount of unequal elements of two char-arrays of the length n: ...
2k views

### std::ranges zip view to iterate over two equal size ranges at the same time

Since c++20 std::ranges doesn't have a zip view like range-v3 does, I am working on implementing a basic version of such a zip view to iterate over two ranges at the same time. The ranges should be ...
• 123
138 views

I've just started learning Golang and was solving some problems which required me to read lines of text from a file. I decided to abstract away the reading part so that I can use Go's ...
• 1,509
1 vote
73 views

### Create a random list of values with corresponding tokens

I have this code that uses loops and random generators to create a random list of values (operators, strings, integers, floats, and new lines) and a corresponding list of the correct tokens to test a ...
• 111
46 views

### Grouping countries and years

Here is an input array that contains country name, year and value. I need to group values by country and years. I use this approach that works for me, but I wonder how to optimize it: ...
160 views

### JavaScript functions to generate Pythagorean triples

I am sure you know what Pythagorean triples are, in the extremely unlikely case that you don't know: A Pythagorean triple consists of three positive integers a, b, and c, such that $$a^2 + b^2 = c^2$$...
• 3,148
82 views

### Locate Primitive Value in Nested Sequence Type - Iterative version is slower than equivalent recursive function

I'm looking for advice on how to improve the performance and/or style of these two search functions. This is just for fun and for practice: they are already fast enough for any application I would ...
1 vote
72 views

### flatten API response, and renaming prop - is there a solution by iterating key/value pair?

I want to sum up and flatten the structure of an API response. Since I didn't succeed by iterating over the key/value pairs of input.statistic I came up with another solution which does the job but I'...
407 views

### Prime numbers iterator

I am not familiar with iterators. I am confused with the traits approach and the traditional one. I don't know which one I should use in 2021. I wrote a minimal ...
• 1,099
3k views

### Add Properties from one array of Objects to another array of Objects based on property id

I do have my code running but again I'm wondering if there is a nicer solution since mine seems to be clumsy and I'm wondering if there is a better way like chaining (but I can't work out how to do ...
526 views

### Python – return list (or not?) of prime numbers between two numbers

I'm trying to learn how to write code well in Python. I've been tinkering with a function that will return a list of all prime numbers between the two parameters. This is the best I could come up with ...
3k views

### Restructuring JSON to create a new JSON where properties are grouped according to similar values

I have a JSON structure in the following example format: ...
67 views

### concise way to build an object from an array of objects

The following code is working as expected but I'm wondering if there's a way to use Object.assign, or some other approach, to build the ...
• 469
139 views

### How do I make my html object-to-table array script run faster? (Pandas)

I have a data frame that has several columns, one of which contains html objects (containing tables). I want a column of table arrays. My problem is that this piece of code takes a long time to run. ...
• 21
734 views

### Euclidean algorithm to determine GCD (greatest common divisor) in C -- alternative method

Been looking into some simple algorithms, I came up with the following piece of code in C to implement the Euclidean algorithm iteratively (I like to be clear here, I don't want the recursive ...
1k views

### Algorithm for auto tiling of tiles in a 2D tile map

Introduction I have implemented an algorithm that applies auto tiling to a 2D tile map for a game, selecting the correct tiles according to their neighborhood. Given a string representation in a ...
1k views

### Given two vectors of ints, determine whether one vector is a prefix of the other

Given two vectors of ints, write a program to determine whether one vector is a prefix of the other. For vectors of unequal length, compare the number of elements of the smaller vector. For example, ...
• 209
1 vote
134 views

### Is there some way to simplify this loop

I have a cycle which goes from -55 to -1, then from 1 to 55 before going back to -55 again. I want to make a loop going from an arbitrary point in this loop to another arbitrary point, but never as ...
• 238
62 views

### Iterating through a list to generate calendars

I'm iterating through a list to generate calendars, the issue is each generator function has the same this.addToCalendar() function. I'd like to know how I could ...
• 153
345 views

### JavaScript: A drawing program in OOP

I made a little drawing program in OOP. The game consists of a canvas which is basically a 2d array where the user can draw various shapes on it. There can be multiple shapes overlapped on top of each ...
• 325
64 views

### Python speed up dataframe writes

I'm aware that iterating frames in pandas is a very slow operation. I've heard vectorization speeds things up. I found a project that I'm using for sentiment analysis on reddit. The Calculate ...
• 121
653 views

### Generate random string to match the goal string with minimum iteration and control statements

The following is my python code to find the number of iterations needed to generate random string iteratively to match the input string. Here I have repeated a similar kind of for loop in both the ...
• 61
217 views

### JS create array by comparing 2 dictionaries

I have 2 dictionaries that I have to compare, if an element of one dictionary exists in the other dictionary, then add the element to an array but only one time... here the working code ...
96 views

### Extract values from English numerals, e.g. "nine million and one"

Though many have done it the other way around, I have not seen such code in many places. And, to be honest, I don't know why this cluster of if-statements, ...
461 views

### Calculator that finds the area under a curve

The program is meant to collect from the user: The function under which to calculate the area The left and right boundaries of the region The amount and position of rectangles to use to approximate ...
609 views

### Microsoft OA | Longest Substring Without 3 Contiguous Occurrences of Letter

For the following question, does my JS Solution seem correct? I'm looking for some peer review. Q: Given a string s containing only a and b, find longest substring of s such that s does not contain ...
2k views

### C program to print a triangle of stars

This C code is to print a triangle of stars. ...
• 199
515 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 ...
• 101
132 views

### Cyclomatic Complexity (complexity)

I have a program for finding shortest distance/path and I got a correct answer but I am getting an issue i.e., "Function 'shortestPath' has a complexity of 9. Maximum allowed is 6." This is ...
100 views

### Adding CSS to each page on WordPress

I just started to use WordPress to start building a website, but as I need to use the get_header() function to retrieve the ...
• 43
273 views

### PHP comment system

Basics I created a simple comment system. My goal was it to create a system that can easily be used on everyone's server without having to install a load of programs. I also tried to create it as ...
409 views

### Computing the Kronecker product over a tensor chain

I'm working on some Python code and have a few functions which do similar things, and the only way I've found of writing them is quite ugly and not very clear. In the example below, the goal is to ...
258 views

### Checking for duplicate email in api response data

I am working on a small internal app that works along an external API for provisioning purpose. The code essentially is comprised of a series of forms where the user inputs data, this data is then ...
• 43
73 views

### Improving the speed of the injection of class element selectors to HTML tables (sometimes nested) at scale through loop iteration or other methods

Background and Code I am using the following JavaScript code to inject CSS classes into a webpage after the page has loaded. These classes act as selectors that I can use to pinpoint a table cell to ...
502 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 ...
162 views

### Recursively displaying json data on website [closed]

I am displaying the data from a json file onto a website, with recursive and iterative tables side by side. i have the iterative code working, but I have no idea on how to make it recursive. Here is ...
1 vote
83 views

### Reduce database touches for one to many data pull

I have the following function which works properly and returns all of the rows from a specific table, then loops through each row to add data related to it as a subarray. ...
67 views

### String data manipulated given CSV variables

The Requirements: List the expensable items sorted by date and location given two variables: 'categories' and 'expenses'. The Categories variable provides the following information in order: Category ...
• 279
286 views

### Maxcounters in JavaScript

I am trying to solve this question: MaxCounters. Solving it is straightforward, but solving it fast enough is proving very difficult. How can I improve the performance of this code? At the moment it ...
• 143