recursion

Ruby get permutations of all lengths from a string in order

风流意气都作罢 提交于 2019-12-31 05:50:27
问题 Here's my code- $arr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-" def scan(f) begin if f.size == 6 exit end $arr.each_char do |y| t = f + y puts t scan(t) end end end I wish to print all permutations and lengths upto 6, I've tried this recursive approach. The output I get is - A AA AAA AAAA AAAAA However I seek something like this- A AA AB AC . . AZ AAA AAB . . AAZ . . upto 6 chars On commenting the recursive call I see that it prints AA AB AC . . Which is fine without

Clearing a counter after each function call: JavaScript Recursive function

核能气质少年 提交于 2019-12-31 05:43:06
问题 I have the folllowing solution to a problem relating to multiplicative persistence. However, I need to wipe the counter after each function call. I have tried different return statements, counters and arrays. I don't seem to be able to clear the counter after each function call AND get the correct answer. It is adding all the answers from multiple function calls. function persistence(num, counter = 0) { if (num.toString().length != 1) { num = num.toString().split("").filter(Number).reduce((a,

Tail recursive maximum depth method of binary tree in Scala

情到浓时终转凉″ 提交于 2019-12-31 05:42:37
问题 I wrote a method to calculate the maximum depth of a binary tree. I would like to write a tail recursive method. I thought of using lists, but I didn't find solutions This is my method that is not tail recursive: def depth: Int = { def iter(f: FormulaWff): Int = f match { case Var(_) => 0 case Not(e1) => 1 + iter(e1) case And(e1, e2) => 1 + Math.max(iter(e1), iter(e2)) case Or(e1, e2) => 1 + Math.max(iter(e1), iter(e2)) case Implies(e1, e2) => 1 + Math.max(iter(e1), iter(e2)) } iter(this) }

Recursion in C, understand recursion example

走远了吗. 提交于 2019-12-31 05:35:05
问题 i am having trouble understanding this example. I cant figure out what actually happens after a certain point. Here is the code, the result is supposed to be 4. I can see that it calls itself several times but how it actually comes to the result of 4 eludes me entirely. Any help would be greatly appreciated. #include <stdio.h> int recursion(int i) { return (i>1 ? i - recursion(i/2) : 3); } int main() { int number = 9; printf("The result is %d\n", recursion(number)); return 0; } edit: Thanks

Confusion in eloquent javascript power recursion sample

余生长醉 提交于 2019-12-31 05:34:28
问题 this recursion code is from the book of eloquent javascript function power(base, exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } console.log(power(2, 3)); obviously the exponent is decreased until it reached 0, if it is not zero, it adds power call on the stack, if it is zero, I start to see the return value of 1 then 2 then 4 then 8. But how did base got multiplied by exponent, how did base see the exponent value? it's on else and on power

Function returns None after recursion

会有一股神秘感。 提交于 2019-12-31 05:34:10
问题 I have written a function to determine the height of a display, given a width and a format. The function operates recursively, if it cannot find a match for the given width and format while trying out a row of height values. The function works, if it finds a match before going into the recursion, but after that it always returns none and not the matching value pair. I am very confused as to why that is. Am I missing some principle here? def getDisplayDimensions(width,FormatX,FormatY): Format

PHP - BBCode parser - recursive [quote] with regex and preg_replace

微笑、不失礼 提交于 2019-12-31 05:31:08
问题 i'm making my own bbcode parser, and i've a problem when i try to do the recursive quote. this is my code : function forumBBCode($str){ $format_search=array( '#\[quote=(.*?)\](.*?)\[/quote\]#is' ); $format_replace=array( '<blockquote class="quotearea"><i><a class="lblackbu" href="./index.php?status=userview&userv=$1">$1</a> wrote :</i><br />$2</blockquote>' ); $str=preg_replace($format_search, $format_replace, $str); $str=nl2br($str); return $str; } what i must add/edit to do a recursive

recursion in mergesort: two recursive calls

别来无恙 提交于 2019-12-31 05:28:08
问题 private void mergesort(int low, int high) { //line 1 if (low < high) { //line 2 int middle = (low + high)/2 ; //line 3 mergesort(low, middle); //line 4 mergesort(middle+1, high); //line 5 merge(low, middle, high); //line 6 }} //line 7 I understand the fact that when the if statement is false you exit the method/function. For example, if i call mergesort(0, 5) in the main method the first mergesort(low, middle) will run 3 times and terminate, then the program will go to line 7. What i am

C Programming - Two for loops to recursion

喜你入骨 提交于 2019-12-31 04:55:09
问题 I was trying to make recursive function that would simulate two for loops. So, function would have to do this: int recursion(int n, int i, int j) { for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { printf("%d %d\n", i, j); } } } but, i want it to be recursive. I tried something like: int recursion(int n, int i, int j) { if(i<n) { if(j<n) { printf("%d %d\n", i, j); recursion(n, i+1, j+1); } recursion(n, i+1, i+1+1); } } I would call recursive one in main like recursion(10, 0, 1); but output is not

Google Maps Api v3 - map.panTo causing “too much recursion”

倾然丶 夕夏残阳落幕 提交于 2019-12-31 04:54:10
问题 I'm having a problem finishing off my script, it works using map.setCenter to reposition on each marker, but I would like to pan to each new marker. However, when I use the panTo function the markers don't appear, although the map does show and pans around to where each new marker would be. Firebug gives me a "too much recursion" error too. Can anyone help? <script type="text/javascript"> var map = {}; var mapOptions; var infowindow; var marker; var lat, lng; var latlng; var gender; var