Questions tagged [loops]

21024 questions
1

votes
3

answer
36

Views

How to loop two DateTime by minutes? JavaScript

var start = '13.04.2019 10:00:00'; var end = '14.04.2019 16:00:00'; I need to loop minute by minute. please help me. // var start = '13.04.2019 10:00:00'; // var end = '14.04.2019 16:00:00'; var start = new Date(2019,05,13,11,00,00,00); var end = new Date(2019,05,14,16,00,00,00); var loop = start; w...
Mizhar Raja
1

votes
3

answer
43

Views

Render multiple elements based on a number rather than mapping through an array

I'm familair with mapping through and array to render something for each number in an array. But what is an effective way to accomplish the same thing using a number. I've found myself in a situation where I have an integer representing number of pages and a component which takes the prop pageNumbe...
Barry Michael Doyle
1

votes
1

answer
348

Views

looping through a dictionary of dataframes in python

First of all, I wrote this function: def writing_in_excel(path, df, sheet_name): writer = pd.ExcelWriter(path, datetime_format='m/d/yyyy') df.to_excel(writer, sheet_name = sheet_name, index=False, freeze_panes = (0,1)) writer.save() writer.close() Then, I have a dictionary of dataframes: import pand...
Yun Tae Hwang
0

votes
2

answer
23

Views

(Java) I can't figure out how to populate an array with a counter controlled loop which is required

I have to: Create an array that models a deck of cards. For example, “1_of_diamonds” represents the ace of diamonds, “2_of_diamonds” represents the 2 of diamonds, up to “13_of_diamonds”, which represents the King of diamonds. The suits clubs, hearts and spades are represented in a simila...
Benji
1

votes
1

answer
55

Views

Square root loop in python

I need to take an input of a number greater than 2, and take the square root until the square root is less than two. I need a print statement that includes the count of the times the square root of the number was taken as well as the output. What I have so far is: import math input_num = float(input...
Europa14
1

votes
3

answer
46

Views

How do I loop through column names to compute statistics?

say I have data that looks like this rating repair model 5 0 1 4 0 0 2 1 1 5 1 0 I want to be able to find the mean of rating for every time repair ==1. However I actually have 200 column names I want to do this for that do not follow a specifi...
Megan
1

votes
1

answer
46

Views

Write a program that adds together all the numbers between 0 and 100. Print the result

I want to write a program that adds all the numbers between 0 and 100 but my code does not add everything correctly. How do I add the next number to the number and then print the sum? This is the code I have: for(int i = 0; i
Ahum
1

votes
4

answer
78

Views

How can I save the 'previous' and 'next' iteration of an object in a loop?

I am working on a program that grabs a number before, and after an incidence of a string and then compares those numbers - but it can only happen if the incidence of that string is there. I've tried iterating through the object.keys (maybe I didn't understand how correctly) I've tried nesting loops...
Jason Harder
1

votes
3

answer
41

Views

Check for duplicated objects in an array populated dynamically

I'm trying to build a data structure where all elements would be grouped based on an object key. Everything works fine except that I can't check if the new array has the data duplicated, as it's outside the for..of loop. I'm looking for a way to prevent pushing a further object if the new array alre...
Rico Letterman
1

votes
2

answer
42

Views

How to add into map in range loop

package main import ( 'fmt' ) func main() { m := make(map[int]int, 4) m[1] = 0 m[2] = 0 for k, _ := range m { i := 10 + k m[i] = 0 } fmt.Println(m) fmt.Println('len:', len(m)) } This code returns: 8 or 10 or 6 as length of map after loop. Video is here, playgroud here. I see that new added elements...
Vladimir Kovpak
1

votes
3

answer
50

Views

Javascript: Loop Through Two Arrays and Sum Elements in Array Via Function

I have the following problem: Suzuki is a monk who climbs a large staircase to the monastery as part of a ritual. Some days he climbs more stairs than others depending on the number of students he must train in the morning. He is curious how many stairs might be climbed over the next 20 years and h...
PineNuts0
1

votes
2

answer
39

Views

Can someone explain the intuition of loops when used on dataframes?

I want to fully understand what each line of code does as I am currently not 100% sure. The code is from a tutorial series I am watching from PythonProgramming.net. I believe the first 'If' statement is adding columns to the act_min_wage dataframe and then renaming them? The 'else' statement seems...
Andrew Andreas
1

votes
2

answer
57

Views

Python while loop is pausing

I am trying to do multiple while loops but somehow they don't work. I already searched the internet but none of the problems I found has the same issue. So here is the code containing only the necessary information. I am basically opening a socket, giving an in input (i\n) and receiving the output i...
Loeli
1

votes
3

answer
62

Views

For some reason loop doesn't print out correct array values

I'm having some trouble getting the correct output when printing an array. Essentially what I'm trying to do is set up an array in the main method, then send that array to another method that would print out something like this: 89 12 33 7 72 42 76 49 69 85 61 23 With it being 3...
Confused Student
1

votes
2

answer
51

Views

Looping through a python dictionary and manipulate each value

I am a fairly new python user and I am stuck on a problem. Any guidance would be greatly appreciated. I have a pandas data frame with three columns 'ID', 'Intervention', and 'GradeLevel'. See code below: data = [[100,'Long', 0], [101,'Short', 1],[102,'Medium', 2],[103,'Long', 0],[104,'Short', 1],[...
Jake
1

votes
1

answer
34

Views

need explanation of a simple function days + = 1

I cannot follow this code, and was hoping someone can explain it to me def negative(temperatures): days = 0 for t in temperatures: if t < 0: days += 1 return days Can someone please explain line 5? so from what I understand the function scans for negative temperatures in the array 'temperatures',...
italy
1

votes
2

answer
47

Views

Creating an infinite for loop

I wanted to test if python would let me run this code: listWords = [] for i in range (len(listWords)+1): listWords.append('New word') print ('End loop') The reason being I want to create a programme that tries to solve a solution and then adds new items to a list for testing. Why does python NOT up...
EML
1

votes
1

answer
30

Views

R: Loop through columns, select value from a column and write it to a new column in same row

I have dataframe in following format id var1 val1 status1 var2 val2 status2 var3 val3 status3 123 a 12 false b 23 true c 34 true Here I want to go though each column of row, get the first occurance of status true for a variable and save it to a new row. Here is the expe...
Malintha
1

votes
2

answer
35

Views

Multiple data in one user input

I am having a trouble with multiple numbers in one user input. The idea is to multiply each user`s number with a predetermined one. Does it even possible to loop through? in1 = input('Enter ') in2 = 10 def count(): s = [x*in2 for x in in1] print(s) count() With input like this '5 5 5' terminal gave...
Dmitri_V
0

votes
0

answer
23

Views

Print triangle with width and height parameters

I am quite new to programming and Java. I need to print an asteriks and dot triangle with respect to width and height parameters. So far, I can print only asteriks triangle with an only one parameter (height). (Assume 6 is given as parameter to the function) Here is my code: public static void main...
oddly
-1

votes
0

answer
23

Views

How to create a loop on linear regression in Rstudio?

I have data that looks something like this, there is time series data for many Rute over multiple RASK. Rute Year Month RASK Fare A 2017 1 10 38 A 2017 2 9 37 A 2017 3 11 40 A 2017 4 12 42 B 2017 5 13 45 B 2017 6 1...
0

votes
0

answer
17

Views

Loop takes too long to replace values

I have a 9 mln line of code where I have to replace every ' . ' with the number in the line above. Meaning that if column 1 contains ' 7 ', the dot below should be replaced by that. But if column 3 contains ' 44 ', the subsequent values have to be replaced by 44 and so on. Problem: at the the moment...
Patrick Lombardo
0

votes
0

answer
20

Views

How to loop through individual cells of array

Have inherited a macro that I'm not 100% on, and I need to fix it. Essentially it should check if each of the reference cells are populated, if true - copy to plan cell, if isEmpty, do nothing. However seems it copies regardless. Have tried adding for each loop but doesn't seem to be taking effe...
John D
1

votes
2

answer
67

Views

Loop every alternate column in table in jQuery

Is there a way to loop through every alternate column in a table? I would like to loop through the TDs of a table but only those that are in columns 2, 4, 6 and 8. I could also add a class to the THs or COLs of these columns if that helps. I am looking for something like the following (just to dem...
user2571510
1

votes
2

answer
27

Views

Replace the values of a dataframe with for loop

The question is: 1) If there is a data frame with 5 rows as follow: x = c(0,1,3,4,8) y = c(3,4,6,8,9) z = c(5,7,3,6,10) xyz = data.frame(rbind(x,y,z)) x 0 1 3 4 8 y 3 4 6 8 9 z 5 7 3 6 10 I want to replace the numbers with 0 if they are smaller than the mean of every row using for loop...
WU BO
1

votes
4

answer
741

Views

While-loop within a while-loop python

I am trying to write a while-loop within a while-loop, and for some reason it is not working as it should. I know I'm probably missing something really trivial here, but I just don't understand how its not working! The purpose of the loop is to compare two strings to see whether or not they contain...
Laura Quinn
1

votes
2

answer
1.4k

Views

Why doesn't my bouncy ball program stop after the 5th bounce?

I nested a for loop inside a while to keep track of the time. And the while loop keeps track of the bounces if a certain condition is met. As long as that condition is met the for loop will keep counting. But once the condition is met the loop is suppose to stop. However, it continues regardless of...
Alcamech
1

votes
4

answer
208

Views

Using list's elements in loops in r (example: setDT)

I have multiple data frames and I want to perform the same action in all data frames, such, for example, transform all them into data.tables (this is just an example, I want to apply other functions too). A simple example can be (df1=df2=df3, without loss of generality here) df1
Laura K
1

votes
4

answer
535

Views

How to display each elements of loop to table row?

how to return each element of loop. I mean i want display each name of this loop to row text. It is only return last element. How can i return all of this? func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellFullname = tableview.dequeueReusableCe...
Neo Teo
0

votes
3

answer
36

Views

Unable to exit while loop (as data validation) while testing for string input in homework problem in C++

I'm trying to write a simple program where I see if a user qualifies for a loan. I'm trying to do validation to make sure the user doesn't enter gibberish, and when I do, it doesn't exit the while loop even when it's not true anymore. The while loop doesn't work. #include using namespace std; int...
Shah Jacob
3

votes
4

answer
82

Views

Get previous number in loop when saved to variable

still new to learning JavaScript. I want to loop through some values. If value is greater or equal to a certain number, then break the loop and get the previous number. For example: var time = [14, 35]; var interval = 40; // in minutes var startTime = [10, 40]; var timeFormatted = '2019-02-04 '+t...
pixie123
1

votes
1

answer
38

Views

How to build filenames in a nested loop

I am trying to access 4k images and crop some ROI based areas (4 ROI in my case) and store them in some directory. So far everything is working okay except the loops handling of the filename. Below is my code attached. I am accessing N 4k images, crop and resize them to my desired resolution. In the...
Rakeh Sheikh
1

votes
2

answer
42

Views

Error:-too many values to unpack (expected 2), while trying to iterate over two columns in a Data Frame

for L,M in laundry1['latitude'],laundry1['longitude']: print('latitude:-') print(L) print('longitude:-') print(M) i am trying to iterate over the two columns of a data-frame, assigning there value to L & M and printing there value but it shows error of 'too many values to unpack (expected 2) ' view...
Adarsh singh
1

votes
3

answer
70

Views

How to create matrix in python of repeating number?

I want to: Create a vector list from 0 to 4, i.e. [0, 1, 2, 3, 4] and from that Create a matrix containing a 'tiered list' from 0 to 4, 3 times over, once for each dimension. The matrix has 4^3 = 64 rows, so for example T = [0 0 0 0 0 1 0 0 2 0 0 3 0 0 4 0 1 0 0 1 1 0 1 2 0 1 3...
Gabriela
1

votes
3

answer
43

Views

For loops that check for empty range

When dealing with recursive equations in mathematics, it is common to write equations that hold over some range k = 1,...,d with the implicit convention that if d < 1 then the set of equations is considered to be empty. When programming in R I would like to be able to write for loops in the same wa...
Ben
1

votes
1

answer
39

Views

PHP Get every Nth item in loop but group them by sets of 6

I am using Slick Slider to build a slider of events. Each 'slide' consists of a group of 6 events of different sizes (shown in the picture below): PHP: Slick Slider JS: $('#eventsList').slick({ dots: true, arrows: false, infinite: true, slidesToShow: 1, slidesToScroll: 1 }); This code works fine if...
cpcdev
1

votes
1

answer
31

Views

JavaScript reserved keywords not correctly being passed through function

I'm attempting to build a page where a user can mark cities on a map that they have visited, storing the data on Firebase for future visits. The issue I've run into is with cities such as 'New York' or 'New Orleans' that begin with reserved keywords. This is the function that runs on page load to ad...
boc4life
1

votes
3

answer
23

Views

Nested Json php for loop status[0] issue

Hi I have a Json format like below: { 'result':0, 'status':[ { 'id':'00001', 'vid':'yes' }, { 'id':'00002', 'vid':'yes' }, { 'id':'00003', 'vid':'no' } ] } I want to use the json_decode function in PHP to decode the id values such as below: 00001 00002 00003 here is the code I am using: $url = file_...
MALKAVIAN
1

votes
6

answer
82

Views

Manipulate Array of objects

I have an array of objects that appears as follows: var dataArray = [{ 'moment': '18 January 2019', 'val': 53 }, { 'moment': '18 January 2019', 'val': 69 }, { 'moment': '18 January 2019', 'val': 52 }, {'moment': '21 January 2019', 'val': 52 }, { 'moment': '21 January 2019', 'val': 52 }, { 'moment':...
Jonathan Fall

View additional questions