for-loop

C++ weird for loop syntax [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-02-05 12:34:33
问题 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

For Loops and stopping conditions

人走茶凉 提交于 2021-02-05 12:32:29
问题 Can anyone explain why the answers are what they are please? The first one i guess its because the stopping condition is already reached so it skips the statement but question 13 why would it not print 4321? 12.What output is produced by the following code: int n; for (n = 1; n > 4; n++) System.out.print(n); a) 12345 b) 1234 c) 0 ---> for loop never runs because stopping condition already met. d) It produces no output* 13.What output is produced by the following code: int n; for (n = 4; n > 0

List comprehension not working [duplicate]

我与影子孤独终老i 提交于 2021-02-05 12:24:54
问题 This question already has answers here : What does “list comprehension” mean? How does it work and how can I use it? (5 answers) List comprehension returning values plus [None, None, None], why? [duplicate] (4 answers) Appending item to lists within a list comprehension (7 answers) List comprehension output is None [duplicate] (3 answers) Closed 3 years ago . I want to put the unique items from one list to another list, i.e eliminating duplicate items. When I do it by the longer method I am

How do I get a cumulative sum using cumsum in MATLAB?

假装没事ソ 提交于 2021-02-05 12:21:59
问题 This is code for i = 1 : 5 b = i; a=cumsum(b); end fprintf('%f \n', a); I expected 1 + 2 + 3 + 4 + 5 = 15 so I would print 15 at the end. But it output 5.000000. If i code "a = cumsum (b)" outside the for loop, it will not be calculated How can I get the value I want 1 + 2 + 3 + 4 + 5? Thanks you 回答1: cumsum performs something like integration, where each element of the output is the sum of all elements up to that position (including) of the input vector. Your code doesn't work because you

For loop doesn't work for web scraping Google search in python

混江龙づ霸主 提交于 2021-02-05 12:21:26
问题 I'm working on web-scraping Google search with a list of keywords. The nested For loop for scraping a single page works well. However, the other for loop searching keywords in the list does not work as I intended to which scrap the data for each searching result. The results didn't get the search outcome of the first two keywords, but it got only the result of the last keyword. Here is the code: browser = webdriver.Chrome(r"C:\...\chromedriver.exe") df = pd.DataFrame(columns = ['ceo', 'value'

List comprehension not working [duplicate]

夙愿已清 提交于 2021-02-05 12:20:45
问题 This question already has answers here : What does “list comprehension” mean? How does it work and how can I use it? (5 answers) List comprehension returning values plus [None, None, None], why? [duplicate] (4 answers) Appending item to lists within a list comprehension (7 answers) List comprehension output is None [duplicate] (3 answers) Closed 3 years ago . I want to put the unique items from one list to another list, i.e eliminating duplicate items. When I do it by the longer method I am

for loop to find consonants in a string

倾然丶 夕夏残阳落幕 提交于 2021-02-05 12:00:23
问题 Using a for loop, how do I go about making all consonants in a string uppercase? I think I should do something like this: String str = "fish$" String strConsonants = "f, s, h"; for (int x = 0; x < str.length(); x++) { if(((str.charAt(x) == (strConsonants)) { System.out.print("FiSH$"); } } 回答1: use String.contains() method from String API. the followingcode would work for your present input. usually, if you want to find all the consonents, have an char[] of consonents or String with all the

C language Changing For Loop to While loop

旧城冷巷雨未停 提交于 2021-02-05 11:53:41
问题 How to make for loop to while loop Is it possible to change for loop in array to while loop ? change for(i = 0; i < 12; ++i) //for loop for selecting only positive { if(a[i] > 0) printf("%d ", a[i]); //display positive only using if statement } to int i = 0, a[12]; while(i < 12) { if(a[i] > 0) { printf("%d ", a[i]); } i++; } but it doesn't display the positive same as the negative if changed.. 回答1: int i; for(i=0; i < count; i++) { // Work item here } is functionally equivalent to int i=0;

Printing list of numbers as an array from for loop Python

别等时光非礼了梦想. 提交于 2021-02-05 11:53:37
问题 With the code below, it prints the value 'phase' one by one. I am trying to print these values as an array outside of for loop. import math Period = 6.2 time1 = datafile1[:,0] magnitude1 = datafile1[:,1] for i in range(len(time1)): print(i,time1[i]) floor = math.floor((time1[i]-time1[0])/Period) phase = ((time1[i]-time1[0])/Period)-floor print (phase) It is printing like this: 0.002 0.003 0.004 0.005 I would like it to print like this: [0.002, 0.003, 0.004, 0.005] 回答1: This would be the least

JavaScript for loop only executed one time

丶灬走出姿态 提交于 2021-02-05 11:49:26
问题 Assistance with my code is greatly appreciated. This seems really, really simple but I can't at all see what the problem is. Just for testing purposes, I want to output to the console each of the 9 values of "j"from the inner for loop, as well as each of the 9 values of "i" in the outer for loop, for a total of 81 outputs. But, it's only returning the first value of "j" (9) one time. What am I doing wrong? var getPalindrome = function(){ for (var i=9;i>0; i--){ for (var j=9;j>0;j--){ return (