loops

R code to assign a sequence based off of multiple variables [duplicate]

对着背影说爱祢 提交于 2020-01-30 08:09:32
问题 This question already has answers here : Recode dates to study day within subject (2 answers) Closed last month . I have data structured as below: ID Day Desired Output 1 1 1 1 1 1 1 1 1 1 2 2 1 2 2 1 3 3 2 4 1 2 4 1 2 5 2 3 6 1 3 6 1 Is it possible to create a sequence for the desired output without using a loop? The dataset is quite large so a loop won't work, is it possible to do this with the dplyr package or maybe a combination of cumsum/diff? 回答1: An option is to group by 'ID', and then

how to get program to accept only positive integer values in c

主宰稳场 提交于 2020-01-30 06:38:08
问题 writing a program that will be finding min, max, avg of values entered by user. Having trouble writing something that will check to make sure there are only postive integers entered and produce an error message. heres my for statement that is reading the input so far: for (int value = 0; value <= numofvals; ++value) { printf("Value %d: %f\n", value, val_input); scanf("%f", &val_input); } mind you I've been learning code for about 3 weeks and was just introduced to loops this week so my

How to make n-dimensional nested for-loops in Python? [duplicate]

倖福魔咒の 提交于 2020-01-30 05:07:51
问题 This question already has answers here : Avoiding nested for loops (2 answers) Iterating over an unknown number of nested loops in python (2 answers) Closed 2 years ago . I have the following situation: for x1 in range(x1, x2): for x2 in range(x3, x4): for x3 ... ... f(x1, x2, x3, ...) How to convert this to a mechanism in which I only tell python to make n nested loops where the variable name is x1, x2, x3, x4, ...? I don't want to write every possibility manually of course, since there

PHP Return Loop Result

南笙酒味 提交于 2020-01-30 02:53:14
问题 I am new to the world of coding as well as PHP and am wondering how I can use return when looping. For example I would like to return/display 1-10 however not use echo. $start = 1; $end = 11; for($start; $start < $end; $start=$start+1) { echo $start; //how can I use return? } 回答1: Well, return will exit the function, so if you put return in a loop, the loop will only do one iteration (until the return statement). You can collect all the values in an array and return the array: function

Wrapping a div around every third item in a foreach loop PHP [closed]

北慕城南 提交于 2020-01-28 09:52:31
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . //set the array $info = array( 'andy' => 'blue', 'andrew' => 'black', 'mark' => 'green', 'jane' => 'orange', 'simon' => 'red', 'joan' => 'pink', 'sue' =>

Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?

家住魔仙堡 提交于 2020-01-26 11:23:13
问题 Given a simple zero based, numerically indexed array: var list = ['Foo', 'Bar', 'Baz']; Many times, I have noticed that when someone suggests looping through variables in an array like this: for(var item in list) { ... } ...there's almost certainly someone suggesting that that's bad practice and suggests an alternative approach: var count = list.length; for(var i = 0; i < count; i++) { var item = list[i]; ... } What's the reasoning for not using the simpler version above and to use the second

How to loop getRange(“B” + row + “ :” + “J” + row) in google sheet editor script

爷,独闯天下 提交于 2020-01-26 04:55:25
问题 function onEdit(evt) { var range = evt.range; showAutoValue(range); } function showAutoValue(range){ var spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); var activeSheet = spreadSheet.getActiveSheet(); var row = range.getRow(); var column = range.getColumn(); if(column == 1 && row >= 1){ var Avalue = activeSheet.getRange("A" + row).getValue(); var B_Jrange = activeSheet.getRange("B" + row + ":" + "J" + row); if(String(Avalue).trim()){ for(var i = 0; i < B_Jrange.length; i++){ if(String(B

Repeating a function a set amount of times in python

我只是一个虾纸丫 提交于 2020-01-26 04:31:20
问题 I am doing an intro class and they are asking me to repeat a function a certain amount of times, as I said this is an intro so most of the code is written so assume the functions have been defined. I have to repeat the tryConfiguration(floorplan,numLights) the amount of time numTries requests. any help would be awesome :D thank you. def runProgram(): #Allow the user to open a floorplan picture (Assume the user will select a valid PNG floodplan) myPlan = pickAFile() floorplan = makePicture

In Python, how to create a large number of variables in a loop? [duplicate]

寵の児 提交于 2020-01-26 04:09:20
问题 This question already has answers here : How can you dynamically create variables via a while loop? [duplicate] (8 answers) Closed 5 years ago . I'm looking for a way, in python 3.2, to create a large number of variables and assign them values. Something like X = 10 Y = 10 A = 0 B = 0 while X >= 0: while Y >= 0: cell[C]X[A] = A cell[C]Y[B] = B B = B + 1 Y = Y - 1 C = C + 1 A = A + 1 X = X - 1 Which would optimally create 200 variables of cell1X1, cell1Y1, cell2X1, cell2Y2, etc etc etc. Is

Determine when two consecutive cells are blank

北慕城南 提交于 2020-01-26 03:28:15
问题 The following While loop is meant to iterate down a column until two consecutive blank cells are found. It exits as soon as curCell is empty, disregarding the value in lastCell. While debugging, I have verified that lastCell has a value and is not empty, but the loop still exits. Something wrong with my And statement? While (lastCell <> "") And (curCell <> "") lastCell = curCell ActiveCell.Offset(1, 0).Select curCell = ActiveCell Wend 回答1: You should use Or instead of And. And requires both