for-loop

Turning a recursive function into a for loop?

亡梦爱人 提交于 2020-01-09 11:23:59
问题 Does every recursive function have an equivalent for loop? (Both achieve the same result). I have this recursive function: private static boolean recur(String word, int length) { if(length == 1 || length == 2) return false; if(length == 0) return true; if(words[length].contains(word.substring(0, length))) return recur(word.substring(length), word.length() - length); return recur(word, length-1); } Given that words is a Set[], and where words[i] = a set with words of length i. What am trying

Turning a recursive function into a for loop?

对着背影说爱祢 提交于 2020-01-09 11:23:25
问题 Does every recursive function have an equivalent for loop? (Both achieve the same result). I have this recursive function: private static boolean recur(String word, int length) { if(length == 1 || length == 2) return false; if(length == 0) return true; if(words[length].contains(word.substring(0, length))) return recur(word.substring(length), word.length() - length); return recur(word, length-1); } Given that words is a Set[], and where words[i] = a set with words of length i. What am trying

Converting for loops to while loops in python

元气小坏坏 提交于 2020-01-09 11:23:09
问题 I am struggling to find an efficient way to convert these for loops to a working set of while loops. Any Suggestions? I am using 2.7 def printTTriangle(height): for row in range(1,height+1): # print row T's for col in range(1,row+1): print 'T', print Thank you all for your help! 回答1: It's like this: def printTTriangle(height): row = 1 while row < height+1: col = 1 while col < row+1: print 'T', col += 1 print row += 1 Here's how I did it. For example, let's convert this line: for row in range

Use a variable in a 'for' loop

醉酒当歌 提交于 2020-01-09 11:22:05
问题 I have following code: @echo off SET ITER=0 for %%i in (%*) do ( SET ITER+=1 ECHO %ITER% ) The output is (for three arguments): 0 0 0 Expected output: 1 2 3 Why can't I access the updated variable in the for loop? 回答1: Expansion of variables with percents is done before a statement/block is executed. So in your case the complete block is expanded before the echo %ITER% is executed, to constant echo 0 . The variable ITER itself is updated in the loop properly. To avoid this, you could use the

R: Break for loop

核能气质少年 提交于 2020-01-09 04:45:08
问题 Can you confirm if the next break cancels the inner for loop? for (out in 1:n_old){ id_velho <- old_table_df$id[out] for (in in 1:n) { id_novo <- new_table_df$ID[in] if(id_velho==id_novo) { break }else if(in == n) { sold_df <- rbind(sold_df,old_table_df[out,]) } } } 回答1: Well your code is not reproducable so we never know for sure, but this is what help('break') says: break breaks out of a for, while or repeat loop; control is transferred to the first statement outside the inner-most loop. So

Remove items from array with splice in for loop

空扰寡人 提交于 2020-01-08 11:25:30
问题 I want to implement a kind of jQuery live search. But before sending the input to the server I'd like to remove all items in my array which have 3 or less characters (because in the german language, those words usually can be ignored in terms of searching) So ["this", "is", "a", "test"] becomes ["this", "test"] $(document).ready(function() { var timer, searchInput; $('#searchFAQ').keyup(function() { clearTimeout(timer); timer = setTimeout(function() { searchInput = $('#searchFAQ').val().match

Why for loop only shows result of last loop

跟風遠走 提交于 2020-01-07 14:19:32
问题 I have this sample matrix: X1 X2 X3 X4 1 F F F F 2 C C C C 3 D D D D 4 A# A# A# A And I'm trying to use a for loop to get the number of unique pitches in each column. Here's how I'm trying to do it: y <- read.csv(file) frame <- data.frame(y) for(i in 1:4){ specframe <- frame[, i] x <- unique(specframe) } length(x) But the result of length is just 4. The output I'm looking for is a vector of 4 elements in which each element details the number of unique elements in their respective columns. It

How to count elements of array in c++

大憨熊 提交于 2020-01-07 09:58:41
问题 I have an array with a particular size (100 for example), which is filled with user input. However, the user might not necessarily enter enough data to fill the entire array. I want to count the elements of the array which the user entered. How can I do this? I tried by this for loop: int COUNT=0; for( int i=0; i<size; i++) if (Student[i]=1) //which means this element is true, not empty element. COUNT++; cout<< COUNT+1 << "\n"; But this code gives an error on this line: if (Students[i]==1)

how to start the forloop.counter from a different index

笑着哭i 提交于 2020-01-07 09:49:21
问题 I have 2 seperate forloops and i am using forloop.counter in bothloops. I want to start the second forloop counter from the ending of first forloop {% for i in something1 %} <tr> <td>{{ forloop.counter }}</td> <td>i.username</td> </tr> {% endfor %} {% for j in something2 %} <tr> <td>{{ forloop.counter }}</td> <td>j.username</td> </tr> {% endfor %} if the first forloop ends at 10 then i want to start the next for loop from 11.plz help 回答1: I'm not comfortable with Django, so I show a couple of

Java to print number triangle with nested loop

有些话、适合烂在心里 提交于 2020-01-07 09:40:20
问题 I am trying to print the following using a nested loop in Java: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 but it's coming out like the following: 1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 Here is my code: for (int i = 1; i <= 6; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int j = i; j <= 6; j++) { System.out.print(j + " "); } System.out.println(); } Any help would be appreciated. Thanks 回答1: int n = 7; for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { System