for-loop

Algorithm complexity: if/else under for loop

余生长醉 提交于 2021-02-07 08:00:34
问题 I am wondering if in a situation like the following (an if/else statement under a for loop) the complexity would be O(n) or O(n^2): for character in string: if character==something: do something else: do something else. Thank you! 回答1: It will be O(n) if 'do something' and 'do something else' are O(1) O(n^2) if 'do something' and 'do something else' are O(n) Basically the complexity of the for loop will depend on the complexity of it components and the no. of loops. 回答2: It depends what you

Algorithm complexity: if/else under for loop

不问归期 提交于 2021-02-07 08:00:27
问题 I am wondering if in a situation like the following (an if/else statement under a for loop) the complexity would be O(n) or O(n^2): for character in string: if character==something: do something else: do something else. Thank you! 回答1: It will be O(n) if 'do something' and 'do something else' are O(1) O(n^2) if 'do something' and 'do something else' are O(n) Basically the complexity of the for loop will depend on the complexity of it components and the no. of loops. 回答2: It depends what you

How to get position of the sub string in string in batch

二次信任 提交于 2021-02-07 05:59:28
问题 Get The position of the substring, Set str1=This is Test string Set sstr=Test Here i need to get the position of "Test"(8). Thanks... 回答1: @echo OFF SETLOCAL Set "str1=This is Test string" Set "sstr=Test" SET stemp=%str1%&SET pos=0 :loop SET /a pos+=1 echo %stemp%|FINDSTR /b /c:"%sstr%" >NUL IF ERRORLEVEL 1 ( SET stemp=%stemp:~1% IF DEFINED stemp GOTO loop SET pos=0 ) ECHO Pos of "%sstr%" IN "%str1%" = %pos% (This returns "9", counting the first position as "1". The definition is in the mind

Replace elements in numpy array avoiding loops

好久不见. 提交于 2021-02-07 03:00:16
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

瘦欲@ 提交于 2021-02-07 02:59:44
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

走远了吗. 提交于 2021-02-07 02:59:35
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

C++: Multiple exit conditions in for loop (multiple variables): AND -ed or OR -ed?

戏子无情 提交于 2021-02-06 14:52:32
问题 For loops and multiple variables and conditions. I am using a for loop to set source and destination indexes to copy items in an array. for(int src = 0, dst = 8; src < 8, dst >= 0; src ++, dst --) { arr2[dst] = arr1[src]; } Something like that anyway. (AND) || (||) My question is about the exit conditions. There are two here. src < 8 and dst >= 0 . Are these conditions AND-ed ( && ) or OR-ed ( || ). To further explain, are the conditions evaluated like this: (src < 8) && (dst >= 0) Or are

How to use multiple XMLHttpRequest?

青春壹個敷衍的年華 提交于 2021-02-05 20:11:32
问题 I need to get 8 JSON from 8 different URL. I stored the query string that I have to change in an Array and I loop through it with a for loop. Here is my code: var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; var request = new XMLHttpRequest(); for (var i = 0; i < index.length; i++) { var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i]; request.open("GET", url); request.onload = function() { var data =

How to use multiple XMLHttpRequest?

半世苍凉 提交于 2021-02-05 20:09:41
问题 I need to get 8 JSON from 8 different URL. I stored the query string that I have to change in an Array and I loop through it with a for loop. Here is my code: var index = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]; var request = new XMLHttpRequest(); for (var i = 0; i < index.length; i++) { var url = "https://wind-bow.glitch.me/twitch-api/channels/" + index[i]; request.open("GET", url); request.onload = function() { var data =

C++ weird for loop syntax [duplicate]

ε祈祈猫儿з 提交于 2021-02-05 12:36:55
问题 This question already has answers here : 'colon' and 'auto' in for loop c++? need some help understanding the syntax (3 answers) Closed 3 years ago . std::string decodeMorse(std::string morseCode) { // ToDo: Accept dots, dashes and spaces, return human-readable message std::string decoded; for( auto p : morseCode ) { if( p == '.' ) decoded += MORSE_CODE[ "." ]; else if( p == '-' ) decoded += MORSE_CODE[ "-" ]; } return decoded; } This is a code extract from https://www.codewars.com Can