recursion

How does your favorite language handle deep recursion?

I recently started learning Python and I was rather surprised to find a 1000 deep recursion limit (by default). If you set it high enough, about 30000, it crashes with a segmentation fault just like C. Although, C seems to go quite a lot higher. (The Python folks are quick to point out that you can always convert recursive functions t...

Should path recursion occur in a class or presentation layer?

I have a WinForms app with an input textbox, button, and a multiline output textbox. A root path is entered in the textbox. Button click calls a function to recursively check all subdirectories for some proper directory naming validation check. The results are output into the multiline textbox. If the recursive work is done in a separ...

Removing Items From IDictionary With Recursion

Anybody have a slicker way to do this? Seems like it should be easier than this, but I'm having a mental block. Basically I need to remove items from an dictionary and recurse into the values of the items that are also dictionaries. private void RemoveNotPermittedItems(ActionDictionary menu) { var keysToRemove = new List<string>()...

iterative version of recursive algorithm to make a binary tree

Given this algorithm, I would like to know if there exists an iterative version. Also, I want to know if the iterative version can be faster. This some kind of pseudo-python... the algorithm returns a reference to root of the tree make_tree(array a) if len(a) == 0 return None; node = pick a random point from the array c...

How do I create a recursive query in MSSQL 2005?

Let's say I have the following table: CustomerID ParentID Name ========== ======== ==== 1 null John 2 1 James 3 2 Jenna 4 3 Jennifer 5 3 Peter 6 5 Alice 7 5 Steve 8 1 Larry I want to retrieve in one query all th...

I want a program that writes every possible combination to a different line of a text file.

OK, so basically what I want is a program that would print every combination of a set of variables to a different line of a text file, creating a word list. for example, print all binary number combinations possible up to for 1, 2, and 3 digits: 0 1 00 01 10 11 000 001 010 011 100 101 110 111 But putting each one of those answers on...

How to increase stack size for a ruby app. Recursive app getting: Stack level too deep (SystemStackError)

Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive Ruby code and I get the: "Stack level too deep (SystemStackError)" (I'm quite sure the code works, that I'm not in an infinite recursive death spiral, but that is not the point anyway) Is there anyway to change the allowed stack depth/si...

Refactor this recursive method?

I'm pretty new to the idea of recursion and this is actually my first attempt at writing a recursive method. I tried to implement a recursive function Max that passes an array, along with a variable that holds the array's size in order to print the largest element. It works, but it just doesn't feel right! I have also noticed that I...

List files recursively in linux with path relative to the current directory

This is similar to this question, but I want to include the path relative to the current directory in unix. If can do the following: ls -LR | grep .txt But it doesn't include the full paths. For example, I have the follow dir structure: test1/file.txt test2/file1.txt test2/file2.txt The code above will return: file.txt file1.txt f...

Python: using a recursive algorithm as a generator

Recently I wrote a function to generate certain sequences with nontrivial constraints. The problem came with a natural recursive solution. Now it happens that, even for relatively small input, the sequences are several thousands, thus I would prefer to use my algorithm as a generator instead of using it to fill a list with all the sequen...

How does PHP do recursive function calls?

PHP (among others) will execute the deepest function first, working its way out. For example, $text = strtoupper(str_replace('_', ' ', file_get_contents('file.txt'))); I'm doing something very similar to the above example for a template parser. It looks for the tags {@tag_name} and replaces it with a variable of the name $tag_name....

Is it best practice to achieve recursion via a partial?

I have the need to display a nested set structure in HTML. I am doing it with the following partial: <ul<%= ' id="tree"' if depth == 0 %>> <% items.each do |item| %> <li id="node_<%= item.id %>"><a><%= item.name %></a> <% if item.has_children? %> <%= render :partial => 'tree_level', :locals => {:items => item.children, :depth =>...

Can I use recursion in a Sql Server 2005 View?

I tried to use OPTION (MAXRECURSION 0) in a view to generate a list of dates. This seems to be unsupported. Is there a workaround for this issue? EDIT to Explain what I actually want to do: I have 2 tables. table1: int weekday, bool available table2: datetime date, bool available I want the result: view1: date (here all days in this...

JSP Tag Recursion

I am implementing a tree tag for one of my practice projects, where I would display the contents of a directory in the form of a tree (recursively). I had implemented a similar requirement as a Custom Tag in Java during the pre-JSP2.0 days. Handling a directory needs recursion (to handle the subdirectories)! Is it possible to code this a...

Javascript callback functions and recursion

This is kind of a brainteaser question, since the code works perfectly fine as-is, it just irritates my aesthetic sense ever so slightly. I'm turning to Stack Overflow because my own brain is failing me right now. Here's a snippet of code that looks up an address using the Google Maps JS API and places a marker on a map. However, someti...

Does the List Clear() method destroy children [C#.NET]?

If I create a recursive list of of lists: class myList { List<myList> childLists; List<string> things; //... } List<myList> tempList = new List<myList>(); And then later call tempList.Clear(), will it destroy all the childLists in memory, or should I create a recursive method to clear all the childLists first? ...

What Is Tail Call Optimization?

Very simply, what is tail-call optimization? More specifically, Can anyone show some small code snippets where it could be applied, and where not, with an explanation of why? ...

ANSI 92 Recursive SQL Statement required

I am translating SQL Server SQL Statements into their ANSI generic equivalent at present, and am stuck with a recursive statement using a WITH statement. For the sake of concentrating on the issue, I'll simplify the issue as follows If I have two tables ReportingUnit col1: Key col2: ParentReportingUnitKey Facility col1: Key col2...

What's a good way to rewrite this non-tail-recursive function?

For some reason, I am having trouble thinking of a good way to rewrite this function so it uses constant stack space. Most online discussions of tree recursion cheat by using the Fibonacci function and exploiting the properties of that particular problem. Does anyone have any ideas for this "real-world" (well, more real-world than the Fi...

Recursive Make - friend or foe?

I'm using (GNU) Make in my project. I'm currently putting one makefile per directory and specify the subdirectories using SUBDIRS. It's been suggested to me that this is not the ideal way of using make, that using a one toplevel make file (or several, split up using include). I've tried migrating/using this layout in the past, but it app...