Questions tagged [iteration]

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

Filter by
Sorted by
Tagged with
2 votes
2 answers
293 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. ...
Tobias Grothe's user avatar
1 vote
1 answer
84 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 ...
Joe's user avatar
  • 11
1 vote
0 answers
79 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. ...
ppinha's user avatar
  • 21
4 votes
1 answer
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 ...
Tobias Grothe's user avatar
1 vote
1 answer
86 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 ...
Tobias Grothe's user avatar
5 votes
2 answers
729 views

Fast unpack of SAM flag bitfield to named symbols

...
Crispy13's user avatar
  • 153
3 votes
1 answer
988 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 ...
driver's user avatar
  • 212
1 vote
1 answer
63 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 ...
G_Hosa_Phat's user avatar
2 votes
2 answers
106 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, ...
Khaled's user avatar
  • 309
-2 votes
2 answers
179 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: ...
HeapUnderStop's user avatar
2 votes
1 answer
897 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 ...
thebear8's user avatar
  • 123
0 votes
1 answer
117 views

Readlines abstraction for Golang

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 ...
smac89's user avatar
  • 1,467
1 vote
2 answers
67 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 ...
Finn E's user avatar
  • 111
0 votes
1 answer
45 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: ...
user264870's user avatar
2 votes
0 answers
126 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$$...
Ξένη Γήινος's user avatar
2 votes
1 answer
69 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 ...
Quack E. Duck's user avatar
1 vote
2 answers
59 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'...
BreadcrumbPie's user avatar
3 votes
2 answers
345 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 ...
nowox's user avatar
  • 1,015
0 votes
2 answers
2k 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 ...
BreadcrumbPie's user avatar
3 votes
2 answers
466 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 ...
Ramen Enthusiast's user avatar
11 votes
4 answers
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: ...
fruitlessartery's user avatar
3 votes
2 answers
65 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 ...
knot22's user avatar
  • 357
0 votes
0 answers
47 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. ...
szoriac's user avatar
6 votes
3 answers
678 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 ...
j3141592653589793238's user avatar
4 votes
0 answers
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 ...
Christian Ivicevic's user avatar
4 votes
4 answers
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, ...
Natalo77's user avatar
  • 209
1 vote
3 answers
133 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 ...
eirikdaude's user avatar
2 votes
1 answer
59 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 ...
Bonteq's user avatar
  • 153
6 votes
1 answer
312 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 ...
Joji's user avatar
  • 315
2 votes
0 answers
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 ...
johnny 5's user avatar
  • 121
5 votes
3 answers
639 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 ...
Jerin06's user avatar
  • 61
0 votes
1 answer
163 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 ...
manuelBetancurt's user avatar
2 votes
1 answer
94 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, ...
niamulbengali's user avatar
4 votes
1 answer
418 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 ...
therealjohndoe17's user avatar
4 votes
3 answers
592 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 ...
Benny Joo's user avatar
3 votes
2 answers
1k views

C program to print a triangle of stars

This C code is to print a triangle of stars. ...
JJJohn's user avatar
  • 199
9 votes
2 answers
469 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 ...
Vaney Rio's user avatar
  • 101
3 votes
2 answers
125 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 ...
L.Nyamagoud's user avatar
3 votes
1 answer
72 views

I need a better way to add css per 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 ...
maneroto's user avatar
4 votes
3 answers
267 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 ...
user avatar
3 votes
2 answers
352 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 ...
Dan Goldwater's user avatar
4 votes
1 answer
243 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 ...
Berny's user avatar
  • 43
4 votes
1 answer
71 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 ...
Dodzi Dzakuma's user avatar
7 votes
4 answers
501 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 ...
Martins Micheal's user avatar
0 votes
1 answer
135 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 ...
Samatar Mohamed's user avatar
1 vote
1 answer
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. ...
GrumpyCrouton's user avatar
2 votes
2 answers
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 ...
amalgamate's user avatar
4 votes
2 answers
277 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 ...
jackdaw tech's user avatar
-1 votes
1 answer
47 views

Best way of converting a dynamic number of list items into variables in Python [closed]

I have a simple scrapy spider that crawls a page and returns H1 on the pages. Since, each page is unique, one cannot know how many h1's will be on the page. Since the scrapy spider returns a list, I ...
Sam's user avatar
  • 103
1 vote
1 answer
62 views

Iterate over a short list, pairing the elements with the sequential elements of a longer list [closed]

I am a relatively new programmer. I am trying to use iter and cycle to cycle over a list (key) and 'pair' each item with each ...
partial_mask's user avatar

1
2 3 4 5 6