Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.
-3
votes
0answers
12 views
Python loop — help! [on hold]
This is the code that I have so far:
i for range(10)
j for range(10+i,end)
print(j,end="")
10
11,12
13,14,15
16,17,18,19
20,21,22,23,24
25,26,27,28,29,30
31,32,33,34,35,36,37
...
0
votes
2answers
106 views
Which loop code is better practice?
def minval(xs):
minum = xs[0]
for i in range(len(xs)):
if xs[i] < minum: #difference in code: xs[i]
minum = xs[i]
return minum
or
def minval(xs):
minum = ...
0
votes
0answers
20 views
Approximating square root (loops and functions exercise)
Just looking for some feedback on my solution for Tour of Go Exercise 24 - Loops and Functions. Is there anything that looks like a wrong way to do things in go? anything I could have done better?
...
4
votes
5answers
82 views
which of these 2 blocks is better?
I am looking for some opinions about these two block of code. They do the same thing, they are just written differently. Which one you recon is "better", "faster", more "readable", gives you more ...
3
votes
0answers
42 views
Copying folder contents from several folders
I have a file directory which contains folders for every month, and within those folders are several text files. In this context I would like to copy all of the text files from my January folder into ...
4
votes
2answers
191 views
Please look at this for-loop
I have made a for-loop for a certain task.
I have a listbox and drag items to it (.png image file, in my case).
The for-loop will the go through each item and convert it to .webp through a command ...
2
votes
5answers
120 views
Can I make this VB code run faster?
I need help making this code run faster. I am using it to import a CSV file into Excel. It works, but it is very slow. The file is almost 20MB. Any help is appreciated.
{Sub OpenTextFile()
Dim ...
0
votes
1answer
32 views
Filtering a long list of files through a set of ignore patterns using iterators
I have a backup job that walks across a huge directory with potentially millions of files. Python's os.walk() works just fine.
Now, I have implemented a feature to ignore files based in a black list ...
2
votes
2answers
66 views
Lots of Continues in an Inner Loop and Dictionary of Actions
I'm using ABCPDF (Version 8) to update an existing PDF document and I believe its API is hurting me in this situation and was curious whether anyone had any comments on how this looks as it appears ...
2
votes
2answers
14 views
How to make the end of loop more readable in Ruby
There are some blocks in my code.
To make it easier to read, I often append comment after the end
such as
end # end upto
end # fileopen
Because sometimes the indentation still not easy for ...
0
votes
2answers
53 views
Create nested named object using variables as key
I am trying to create a new object with nested value pairs. My initial challenge is that I have a huge object containing lots of junk data, and I'd like to strip away the value pairs that I do not ...
1
vote
4answers
123 views
Optimal way of skipping certain values in a for loop
What would be the fastest way of skipping certain values in a for loop? This is what I intend to do in its most basic form, with about 20 more keys to omit.
for (var key in arr ) {
if ( key != ...
1
vote
3answers
150 views
Improve nested loop for bio-statistics calculation
I am doing a bio-statistics calculation and the following code works. However, can someone help to improve the messy nested loop?
for(int i=0; i<NN; i++) {
for (int j=0; j<NN; j++) {
...
3
votes
2answers
90 views
Differences between using a do-while vs. while and initializing variables
When I use a do-while loop, I can create a variable userInput, but don't need to initialize it until the do-while loop because it is not used until then.
import java.io.*;
public class Bla
{
...
1
vote
2answers
67 views
Is there a more efficient way of executing PDO statements?
I am working on a small method that saves uploaded images and records the images in a database.
public function setEventImages($event_id){
foreach($_FILES['event-images']['tmp_name'] as ...
0
votes
0answers
37 views
Displaying overlapping blocks using d3js
The code takes in nodes with different start time as input and assigns the position such that they will not overlap. As I have coded with too many loops and conditions. Can anyone review the code and ...
0
votes
1answer
64 views
How can i iterate cell value of csv file using python? [closed]
import csv
ifile=csv.reader(open('C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv','rb'),delimiter=';')
for line in ifile:
row = line
if [True for cell in row if cell=='']:
...
3
votes
0answers
101 views
Reducing cyclomatic complexity
I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).
Here is my code:
public void myMethod(){
try{
// do something
...
6
votes
1answer
191 views
How to avoid duplication and to keep code clean
Here is my C# code that draws scanline of the image to System.Drawing.Graphics. It's quite simple and it's optimized in order to merge neigbour samples with the same color into single rectangle ...
3
votes
4answers
150 views
Detecting Tic-Tac-Toe win: Enumerate all possibilities or use nested loops?
Currently I have a game that does this and then checks for victory conditions in victoryTable
victoryTable[0] = rows[0][0] + rows[0][1] + rows[0][2];
victoryTable[1] = rows[1][0] + rows[1][1] + ...
4
votes
2answers
79 views
Optimizing if-statement with for-loop
I have been staring at this code for awhile now and I am thinking there is a way to optimize it (namely the if-else statement with the for-loops). Any advice would be greatly appreciated.
...
5
votes
2answers
434 views
Can this foreach loop be improved?
Can the below foreach loop be improved? Since I use the same method twice, maybe use continue or break?
int? index = null;
int count = 0;
foreach (Break b in breaks)
{
if (index.HasValue)
{
...
4
votes
3answers
133 views
Does this loop unrolling make sense?
I am coding a Hanning filter which is a recursive relation:
y[i] = 0.25*(x[i] + 2*x[i-1] + x[i-2])
And here is my code were I attempt to unroll 3 times:
public void HanningFilter(float[] array) { ...
0
votes
1answer
61 views
Looping over a list [closed]
I'm new to Python, and I have a small section of code which works fine when outside of a function:
list = [["M4342","Joe"],["M4343","Melanie"],["M4344","Alex"],["","David"]]
for value in list:
...
4
votes
2answers
93 views
Idiomatic loop and break condition
I am calling a C library via P/Invoke. That library needs to be called in sucession with an increasing unsigned int, and returns a pointer. That pointer is then mapped to a managed type, until it ...
1
vote
2answers
88 views
Optimization of a while-loop searching for words in a dictionary
This is my first question here. I'm using an open source program called MElt which lemmatize (give the lemma example:giving-->give) of words. MElt works on Linux and its programmed in Perl and ...
2
votes
2answers
95 views
Change one column data from List<>
I have this code:
for (int i = 0; i < lst.Count(); i++)
{
lst[i].ColumnOrder = (short)i;
}
But I was trying to find a way to do it via LINQ or something else with better performance. I'm ...
0
votes
1answer
69 views
Avoiding dynamic RegEx creation in JavaScript
This function's job is to replace several text smilies, e.g. :D, :), :love with the appropriate smiley image.
In my opinion my code has several issues, yet the main problem is that a lot of quite ...
2
votes
1answer
111 views
Optimising If statement and for loops
I have two solutions (in jython) to take two pictures and copy and paste alternate sections of one picture to the other, like so:
for x in range (0,w):
for y in range (0,h):
...
1
vote
1answer
69 views
Is there any better way to fetch two tables in one statement or query?
The 'Do' method is for prepared statements and returns (by fetchall()). I am also using two looping statements. Is there any better way to fetch two tables in one statement or query? If not, what ...
1
vote
2answers
53 views
Object iteration and appending to form
In this code, I'm getting the json from backend. Once I find the json availability, I loop and make 5 columns. Once the columns are made, I append after the legend of my form.
Can this be improved ...
2
votes
4answers
308 views
Simplify Code — For Each Loop
I am trying to return a value (a.tnAddress) from a custom class based on a lookup (foreach loop). Depending on the type of transaction, I will need to do the foreach loop based on different ...
1
vote
2answers
108 views
Is there a better, more efficient way to write this loop?
While working with someone else's code I've isolated a time-killer in the (very large) script. Basically this program runs through a list of thousands and thousands of objects to find matches to an ...
0
votes
0answers
23 views
The object iteration having static values and lengthy codes
I am converting a existing object in to new object to fit for handlebarsJs.
the loop func i use is here:
var mainLink = {"links":[]}; x = 0;
//mainLin is the final object
for(key ...
1
vote
2answers
88 views
Optimizing Java code for setting color of specific keywords in a textview
I'm developing an Android app to view C-programs.I wanted to give a simple color scheme to the text which is stored in database,retrieved as string and then passed on to the textview.
The code I ...
2
votes
2answers
102 views
Refactoring same loop logic with different input data
I have these two methods:
public static String buildLine(String[] values) {
StringBuilder lineBuilder = new StringBuilder();
for (int i = 0; i < values.length - 1; i++) {
...
1
vote
1answer
114 views
Video/article slider.
I have created a video/article slider. It works fine, but I have a feeling it's not quite a standard solution.
I would appreciate it if someone could review my code, so that if there are mistakes, I ...
2
votes
2answers
49 views
I'm practicing turning for loops into recursive functions. what do you think?
I am trying to turn this function:
collection = ['hey', 5, 'd']
for x in collection:
print(x)
Into this one:
def printElement(inputlist):
newlist=inputlist
if len(newlist)==0:
...
1
vote
1answer
27 views
Should I use and loop, or just a loop?
I build a code like this
if(listObj.Any(x => x.id < 0))
{
foreach(ModelClass item in listObj)
{
if(item.id < 0)
{
// code to create a new Obj in the ...
2
votes
1answer
53 views
Tcl loop idea: for_index_item
I often need to loop through a list of items and need both the index position, and the item itself. Typically:
set names {John Paul George Ringo}
set i 0
foreach name $names {
puts "$i - $name"
...
1
vote
2answers
100 views
A replacement for this infinite loop?
/**
* Returns returns a pseudo-random Gaussium number. Forumla:
* {@code (RanGassium * 3) + mean }</ br> 3 is hardcoded for 97% accuracy.
*
* @param mean
* mean of returning ...
2
votes
1answer
130 views
Review a simple sprite sheet animation script
This is just a simple script for testing sprite sheet animations. I'm trying to improve my code style, so I'm curious about suggestions anyone might have about best practices or readability.
...
7
votes
2answers
233 views
What's the most idiomatic way to call a function an arbitrary number of times with the previous result?
Basically I have a function of the type 'a -> 'a (an optimization function on a AST) and I want to call it (passing the previous result) until it returns the same thing as the input.
Right now I ...
1
vote
2answers
79 views
Making a 2D dict readable
I am building a 2d dictionary with an altered list at the end Afterwords I want to find the min of each list. Is there a better way to do it? Better being faster or at least less of an eyesore.
...
-1
votes
3answers
199 views
dead code and infinite loops with java for loop [closed]
I have a for loop that loops infinity when i don't breakout of it and only loops once when i do break out of it. I'm sure im missing something simple.
The if and else should be identical aside from ...
1
vote
0answers
59 views
Ascii Table in brainfuck
I made my first brainfuck program today, which prints the numbers 0-255 and the corresponding character.
I was wondering if I could improve my program, as I'm repeating my self a lot (eg. 3 x copy ...
1
vote
1answer
47 views
Matlab: speeding up repetitive initialisation with structured data?
I need to speed up this code: it creates a multilinear function. I considered the below methods but no idea which with the largest benefit.
replacing dummy variables with things such as ...
0
votes
1answer
58 views
Range of all variables in List in Prolog
I have a list in Prolog like this:
Puzzle = [ A1, A2, A3,
B1, B2, B3,
C1, C2, C3 ].
And I want every variable in the list to be a number in the range of 1 to 9.
Currently ...
1
vote
1answer
89 views
High School Java Class: Pong Project External Reviewer
Panelball class:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Panelball extends JPanel implements Runnable {
private static final long ...
1
vote
3answers
124 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 ...