recursion

Recursion and Iteration

What is the difference? Are these the same? If not, can someone please give me an example? MW: Iteration - 1 : the action or a process of iterating or repeating: as a : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result b : the repetition of a sequence of computer instruct...

Java: how to get all subdirs recursively?

Before debugging the late-hour-out-of-bound-recursive-function: is there a command to get subdirs? giveMeSubDirs(downToPath)? // WARNING: RECURSION out of bound or too much data public HashSet<FileObject> getAllDirs(String path) { HashSet<FileObject> checkedDirs = new HashSet<FileObject>(); HashSet<FileObject> allDirs = new HashSet<...

This recursive function puzzles me, what is going on?

I was playing around with recursion and did this simple function. I was assuming that it would print out 9-0 to stdout, but, it prints 0-9. I can't see how that happens at all. int main() { rec(10); return 0; } int rec(int n){ if(n > 0) printf("%d\n", rec(n -1)); return n; } ...

What is the possible benefit (if any) of allowing recursive contructors?

In Java, constructors cannot be recursive. Compile time error: "recursive constructor invocation". Let's assume that we did not have this restriction. Things to keep in mind: The return type of a constructor is void. Since it is a void method you can't harness the complete power of recursion. A constructor can invoke itself (or any ot...

Strange PHP reference bug

Hello, I have a really strange bug with my PHP code. I have a recursive code which build up a menu tree with object and one of my customers server can't make it work. Contructor: function Menu(&$menus, &$keys , &$parent, &$menu){ ... if($keys === NULL){ $keys = array_keys($menus); } ... ...

Python recursion with list returns None

def foo(a): a.append(1) if len(a) > 10: print a return a else: foo(a) Why this recursive function returns None (see transcript below)? I can't quite understand what I am doing wrong. In [263]: x = [] In [264]: y = foo(x) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] In [265]: print y None ...

Sending data recursively using jquery $.ajax gives stack overflow error..

Why am I getting "too much recursion" error when I do the following? function sendTheNames() { alert("start submitting names.."); return function (array) { var name = $(array.shift()).text(); $.ajax({ url: "test.jsp?name=" + name, complete: function () { ...

PHP Array to String equivalent

Hey all, I'm wondering if anyone has a recursive solution to converting an array to a string. Here's what I mean: An array $args that has the following contents: Array ( [0] => $hello [1] => 411px [Jeeves] => Array ( [compiling] => 1 ) ) Result after calling arr_to_string($args): array($h...

Whats wrong with this method?

Here's the method: public static String CPUcolor () { System.out.println ("What color am I?") ; String s = getIns() ; System.out.println ("are you sure I'm "+s+"? (Y/N)") ; String a = getIns() ; while (!((a.equals ("y")) || (a.equals ("Y")) || (a.equals ("n")) || (a.equals ("N")))) { Syste...

Loop through multi-dimensional array and remove certain keys

Hi! I've got a nested tree structure which is based on the array below: Array ( [1] => Array ( [id] => 1 [parent] => 0 [name] => Startpage [uri] => 125 [basename] => index.php [child] => ) [23] => Array ( [id] => 23 ...

Is there a better way to serially submit multiple AJAX requests?

I have a page with multiple forms that I submit via Ajax POSTs serially. At first, I tried using synchronous XHR requests, but this causes the browser to lock up for the duration of the request, and breaks my DOM-manipulation effects, which is unacceptable. So the pattern I ended up using is basically this: var fcount = 0; // increme...

How to avoid recursion in Java SecurityManager checkConnect?

I'm trying to take control of a Java code base that does lots of un-documented things. I'm using a custom SecurityManager to check permission requests. Specifically, my code is checking SocketPermission checks -- checkConnect. checkConnect is called when the application tries to resolve a host name to IP address and to connect to a spec...

Segmentation fault in a function to reverse a singly linked list recursivley.

I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef struct _node { int data; struct _node *next; } Node, *NodeP; NodeP recursiveReverseList(NodeP first){ if(first == NULL) return NULL; if(first->next == NULL) return first; NodeP rest = recursiveReverseList(first->next); r...

Recursive Functions for Beginners?

I'm looking for a few recursive function examples, preferably ones that show increase in complexity. I understand basic recursive functions, but I'm having trouble implementing them in my code. I've never used them in my code before, and I know it doesn't always call for it, but I'd like to try. Is there a good resource with examples, ...

How to recursively serialize an object using reflection ?

I want to navigate to the N-th level of an object, and serialize it's properties in String format. For Example: class Animal { public String name; public int weight; public Animal friend; public Set<Animal> children = new HashSet<Animal>() ; } should be serialized like this: {name:"Monkey", weight:200, friend:{name:"Mon...

Java: dangerous self-returning-recursive function by IOException?

I had very errorsome Exception handling with if-clauses. An exception occurs if not found path. An exception returns the function again. The function is recursive. Safe? $ javac SubDirs.java $ java SubDirs Give me an path. . Dir1 Dir2 Give me an path. IT WON'T FIND ME, SO IT RETURNS ITSELF due to Exception caught Give me an path. Th...

Drawing the call stack in a recursive method

Hey, I want to draw the call stack for any recursive method, so I've created a schema like this, recursiveMethod(){ //Break recursion condition if(){ // Add value here to the return values' list- No drawing return } else{ //Draw stack with the value which will be pushed to the stack here variable <- recursive...

JavaScript recursion does not work properly

Hi, Could anyone say why the following recursive function does not work for me ? It should collect recursively all radio buttons in a given element. But, it does not found any for some reason !? Thanks !! <?xml version="1.0" encoding="Windows-1255"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org...

How to write a function to output unconstant loop

Here is the function description test($argv) $argv is an array, for example $argv=array($from1,$to1,$from2,$to2.....); array items must be even. $argv=array(1,3,4,5) : this will output values like below: 1_4 1_5 2_4 2_5 3_4 3_5 pseudocode is like while($from1<=$to1){ while($from2<=$to2){ echo $from1."_".$from2."<br/>"...

Does C++ limit recursion depth?

In Python there is a maximum recursion depth. Seems it is because Python is an interpreter, not a compiler. Does C++ have the same concept? Or it only connected with RAM limit? ...