loops

R - loop and break after value found for each Rows

不问归期 提交于 2021-02-08 07:58:53
问题 I have a matrix of 0-1. What I would like to do is to loop into this matrix and search for the 1. Each time a 1 has been found, to simply jump or pass that row, in order to only record 1 value per row. What I am trying to know if the first episode of the sequence is a 1 . I was thinking there might be a solution with break But I am unsure how to use it properly. So this is my first matrix SoloNight = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,

Java turning two arrays into one two-dimensional array

瘦欲@ 提交于 2021-02-08 07:06:11
问题 I have two arrays with the same length in my program, courseGrades and courseCredits, that look something like this: courseGrades[] = {A, A, B, A, C, A}; courseCredits[] = {1, 2, 1, 3, 2, 1}; I want to combine them into one two-dimensional array that would display like this: A A B A C A 1 2 1 3 2 1 How would I do this in Java? Additionally, how would you refer to each element in this array? Sorry for the very simple question, I'm still a beginner at java. 回答1: You can create a Pair class,

Java turning two arrays into one two-dimensional array

陌路散爱 提交于 2021-02-08 07:05:17
问题 I have two arrays with the same length in my program, courseGrades and courseCredits, that look something like this: courseGrades[] = {A, A, B, A, C, A}; courseCredits[] = {1, 2, 1, 3, 2, 1}; I want to combine them into one two-dimensional array that would display like this: A A B A C A 1 2 1 3 2 1 How would I do this in Java? Additionally, how would you refer to each element in this array? Sorry for the very simple question, I'm still a beginner at java. 回答1: You can create a Pair class,

Why does del list[0] in a for-loop only delete half the list?

青春壹個敷衍的年華 提交于 2021-02-08 06:34:47
问题 I have the following code and it only deletes half the list using a for loop. Why does this happen? Also, how should I delete the first element of the list WHILE traversing through it? list = [1,2,3,4,5,6,7,8,9,10] for x in list: print list[0] del list[0] print list Output: 1 2 3 4 5 [6, 7, 8, 9, 10] 回答1: The problem is that you delete from the list but the list-iterator doesn't know that and happily processes the "remaining" list. So in the first iteration the iterator is at index 0 and you

Why does del list[0] in a for-loop only delete half the list?

自古美人都是妖i 提交于 2021-02-08 06:34:02
问题 I have the following code and it only deletes half the list using a for loop. Why does this happen? Also, how should I delete the first element of the list WHILE traversing through it? list = [1,2,3,4,5,6,7,8,9,10] for x in list: print list[0] del list[0] print list Output: 1 2 3 4 5 [6, 7, 8, 9, 10] 回答1: The problem is that you delete from the list but the list-iterator doesn't know that and happily processes the "remaining" list. So in the first iteration the iterator is at index 0 and you

Spot it algorithm - js

ⅰ亾dé卋堺 提交于 2021-02-08 05:17:33
问题 In the game Dobble ( "Spot it") , there is a pack of 57 playing cards, each with 8 different symbols on them. The system is that any two cards chosen at random will have just one matching symbol. This spurred me on to the mathematical background behind the system and so I wrote an simple algorithm: let getCards = function(symbolCount) { let n = symbolCount - 1, cards = [], card = [] for (i = 0; i < n + 1; i++) card.push(i) cards.push(card) for (j = 1; j <= n; j++) { let card = [1] for (k = 1;

Printing a table in Python

人盡茶涼 提交于 2021-02-08 05:15:14
问题 I have an assignment to create a 10 by 10 table in Python and I'm using the end "\t" within my print function to prevent it from creating a new line. But, I need it to start a new line after of course 10 characters. How can I make it do that? Here's my code: product = 0 for x in range(1,11): for y in range(1,11): product = x * y print(product, end="\t") I need it to look something like this: 1 2 3 4 5 6 ... 2 4 6 8 10 12 ... 3 6 9 12 15 18 ... 回答1: This is using alignement options from the

Printing a table in Python

空扰寡人 提交于 2021-02-08 05:14:41
问题 I have an assignment to create a 10 by 10 table in Python and I'm using the end "\t" within my print function to prevent it from creating a new line. But, I need it to start a new line after of course 10 characters. How can I make it do that? Here's my code: product = 0 for x in range(1,11): for y in range(1,11): product = x * y print(product, end="\t") I need it to look something like this: 1 2 3 4 5 6 ... 2 4 6 8 10 12 ... 3 6 9 12 15 18 ... 回答1: This is using alignement options from the

Python looping with try and except

左心房为你撑大大i 提交于 2021-02-08 05:02:06
问题 I am trying to write a program that reads numbers input by the user until the user types done. If the user types a non-number other than "done," I want to return an error message like "please enter a number number. When the user types "done", I want to calculate the total of the numbers, the number count and the average. I have tried to create a while loop with try and except to catch the non-numeric error other than done. That is part of the trick, a string entry is an error unless the

Python looping with try and except

不问归期 提交于 2021-02-08 05:01:54
问题 I am trying to write a program that reads numbers input by the user until the user types done. If the user types a non-number other than "done," I want to return an error message like "please enter a number number. When the user types "done", I want to calculate the total of the numbers, the number count and the average. I have tried to create a while loop with try and except to catch the non-numeric error other than done. That is part of the trick, a string entry is an error unless the