Questions tagged [iteration]
Iteration is the repetition of a block of statements within a computer program, usually with mutable state.
248
questions
1
vote
2
answers
41
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'...
3
votes
2
answers
162
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 ...
0
votes
2
answers
451
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 ...
3
votes
2
answers
242
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 ...
11
votes
4
answers
2k
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:
...
3
votes
2
answers
60
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 ...
0
votes
0
answers
42
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. ...
6
votes
3
answers
596
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 ...
3
votes
0
answers
192
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 ...
4
votes
4
answers
941
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, ...
1
vote
3
answers
125
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 ...
2
votes
1
answer
56
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 ...
6
votes
1
answer
150
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 ...
2
votes
0
answers
59
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 ...
6
votes
3
answers
598
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 ...
0
votes
1
answer
79
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
...
2
votes
1
answer
74
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, ...
4
votes
1
answer
175
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 ...
4
votes
3
answers
505
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 ...
4
votes
2
answers
466
views
C program to print a triangle of stars
This C code is to print a triangle of stars.
...
9
votes
2
answers
417
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
...
3
votes
2
answers
108
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 ...
3
votes
1
answer
64
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 ...
4
votes
3
answers
233
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 ...
3
votes
2
answers
183
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 ...
4
votes
1
answer
135
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 ...
4
votes
1
answer
63
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 ...
7
votes
4
answers
413
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 ...
0
votes
1
answer
65
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
1
answer
73
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.
...
2
votes
2
answers
62
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 ...
4
votes
2
answers
201
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 ...
5
votes
2
answers
144
views
Delays using multiple for loops
I got this script which work perfectly but I got some delays because of this 2 for loops [i][j] is there any way to do the same ...
-1
votes
1
answer
45
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 ...
1
vote
1
answer
40
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 ...
-3
votes
1
answer
31
views
How to convert Recursive Approach to iterative one(Super Reduced String)? [closed]
The Question:
Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation he selects a ...
1
vote
0
answers
40
views
conditional simulation(path dependent simulation) in R
My codes are not good, I want to konw how to solve my problem efficicently and learn writing some elegant R codes.Thanks in advance!
"r" is the starting value in ...
2
votes
1
answer
26
views
Join ArcGIS tables in Python of plant greenness in response to prior month's rainfall
I am using Python 3.8 with ArcGIS Pro 2.5. This code creates a joined table where I match values for plant greenness in the current month with rainfall X number of months into the past. Each loop ...
3
votes
2
answers
5k
views
Sliding window iteration in Python
I'm creating a small library of Python utilities, and I'd like feedback on a function which allows iterating over an arbitrary iterable in a sliding-window fashion.
Relevant parts of ...
1
vote
1
answer
60
views
Which solution is good to generate a list without None results from a function? [closed]
I'm relatively new to Python and learned, that function cannot return nothing, not even None. In my application I want to iterate over a list and keep all non-...
-1
votes
1
answer
212
views
Best way to optimize multiple search on Lists java
Anyone could help me to optimize this block of code, I already did a bit improvement but since I'm really bad with this kind of algorithms this all I've done so far. Much appreciate if you can explain ...
2
votes
2
answers
108
views
Divide the last element in an array of arrays among the remaining elements
The function below is part of an overall chunk of code to partition an array of size M into sub-arrays of size N. The function in question is written to handle the case: what if M isn't divisible by ...
0
votes
1
answer
37
views
2
votes
3
answers
54
views
Flagging system: Check for changing values in a data frame
I've written the following code to check if a value in a data frame changes. I'm looking at the last 5 values. If there was no change at all I want my code to return 1, if a single one (or multiple) ...
2
votes
1
answer
327
views
Pandas iteration over rows for features calculation
I have a pandas data frame and I want to calculate some features based on some short_window, long_window and ...
1
vote
2
answers
115
views
Reduce time taken for VBA loop when copying multiple sets of input values, performing Excel formula calculations and recording results
My VBA code currently loops for 5,000 iterations and takes about 25 minutes to collect the output data in the Output excel sheet. Is there a way to reduce the time taken for instance by improving the ...
3
votes
1
answer
874
views
Extracting mail data out of MS Outlook's generic mailbox
This code extract data from generic mailbox mapped by MS Outlook
I failed at recursive iteration in subfolders, so it stays as iterative one.
Added ASCII encoding to deal with crazy national symbols ...
4
votes
4
answers
3k
views
Forming error messages from a multidimensional associative array
I'm working with an input array, which is 3 levels deep, and creating error message strings accordingly in a separate output array. Below is the code.
...
2
votes
2
answers
102
views
Listing last 3 items (or all if less than 3) in an ArrayList
Writing a code to return the last 3 items in a list, if the list has 3 or more elements, otherwise return all list items.
But the code looks really bad.
What is a better way to write the same code?
<...
2
votes
1
answer
59
views
Merge adjacent occurrences of identical elements in data collection
There is a small programming exercise to merge adjacent identical elements in a collection.
Here are two solutions (passing the tests from the exercise).
I am asking for review of the first version ...