recursion

recurse over a list

I'm trying to recurse over a list (eg. [True, [[True, False], [False, [False, True]]]]) using Python. I know that the list length will always be 2 and both values will be boolean. I'd like to take those values and substitute them back into the list until there are only 2 values left (or 1 boolean value). Any help would be much appreci...

Can continuations be used as a replacement for recursion?

The following function generates a 'stack level too deep (SystemStackError)' for n = 5,000 def factorial(n) n == 0 ? 1 : factorial(n -1) * n end Is there a way to avoid this error using continuations/callcc? Note: I know this can be implemented without recursion. e.g. def factorial2(n) (1..n).inject(1) {|result, n| result * n ...

FIlling a Java Bean tree structure from a csv flat file

Hi, I'm currently trying to construct a list of bean classes in Java from a flat description file formatted in csv. Concretely : Here is the structure of the csv file : MES_ID;GRP_PARENT_ID;GRP_ID;ATTR_ID M1 ; ;G1 ;A1 M1 ; ;G1 ;A2 M1 ;G1 ;G2 ;A3 M1 ;G1 ;G2 ;A4 M1 ;...

Recursion in Java enums?

I've been trying for 3 hours and I just can't understand what is happening here. I have an enum 'Maze'. For some reason, when the method 'search' is called on this enum it's EXTREMELY slow (3 minutes to run). However, if I copy the same method to another class as a static method, and I call it from the enum 'Maze' it runs in one second!...

CakePHP - Set recursive to -1 in AppModel then use Containable behaviour as appropriate

When using CakePhp would it be advisable to set recursive = -1 in the AppModel class and then use the Containable Behaviour whenever you need a deeper data relationship? I believe this would give my applications the best chance of avoiding database related sluggish performance but is the methodology sound? Thanks Leo ...

How can I recursively read out directories in Perl?

Hi, I want to read out a directory recursively to print the data-structure in an HTML-Page with Template::Toolkit. But I'm hanging in how to save the Paths and Files in a form that can be read our easy. My idea started like this sub list_dirs{ my ($rootPath) = @_; my (@paths); $rootPath .= '/' if($rootPath !~ /\/$/); ...

What is the most efficient way to list all of the files in a directory (including sub-directories)?

I am writing a servlet which will examine a directory on the server (external to the web container), and recursively search for certain files (by certain files, I mean files that are of a certain extension as well as a certain naming convention). Once these files are found, the servlet responds with a long list of all of the found files ...

Anonymous recursive PHP functions.

Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn't pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n == 1 ) return 1; return $factorial( $n - 1 ) * $n; }; print $factorial( 5 ); I'm also aware that this is a bad way t...

HQL recursion, how do I do this?

Hi guys, I have a tree structure where each Node has a parent and a Set<Node> children. Each Node has a String title, and I want to make a query where I select Set<String> titles, being the title of this node and of all parent nodes. How do I write this query? The query for a single title is this, but like I said, I'd like it expanded ...

Array: mathematical sequence

An array of integers A[i] (i > 1) is defined in the following way: an element A[k] ( k > 1) is the smallest number greater than A[k-1] such that the sum of its digits is equal to the sum of the digits of the number 4* A[k-1] . You need to write a program that calculates the N th number in this array based on the given first eleme...

Recursive stack size

How i can know the current method stack frame while a recursive call in ruby? ...

Why so much stack space used for each recursion?

I have a simple recursive function RCompare() that calls a more complex function Compare() which returns before the recursive call. Each recursion level uses 248 bytes of stack space which seems like way more than it should. Here is the recursive function: void CMList::RCompare(MP n1) // RECURSIVE and Looping compare function { auto M...

What are basic programs like, recursion, Fibonacci, small trick programs?

This question may seem daft (I'm a new to 'programming' and should probably stop if this is the type of question I'm required to ask)... What are: "basic programs like, recursion, fibonacci, factorial, string manipulation, small trick programs"? I've recently read Coding Horror - the non programmer and followed the links to Kege...

Why doesn't this loop terminate?

Here's the sample code: public static void col (int n) { if (n % 2 == 0) n = n/2 ; if (n % 2 != 0) n = ((n*3)+1) ; System.out.println (n) ; if (n != 1) col (n) ; } this works just fine until it gets down to 2. then it outputs 2 4 2 4 2 4 2 4 2 4 infinitely. it seems to me that if 2 is entered...

Using Mergesort to calculate number of inversions in C++

void MergeSort(int A[], int n, int B[], int C[]) { if(n > 1) { Copy(A,0,floor(n/2),B,0,floor(n/2)); Copy(A,floor(n/2),n-1,C,0,floor(n/2)-1); MergeSort(B,floor(n/2),B,C); MergeSort(C,floor(n/2),B,C); Merge(A,B,0,floor(n/2),C,0,floor(n/2)-1); } }; void Copy(int A[], int startIndexA, int endI...

PHP 2D Array output all combinations

Hi there, I've had this problem bending my mind for a while now (head cold doesn't help either!), basically I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1] = 'pears'; $array[0][2] = 'oranges'; $array[1][0] = 'steve'; $array[1][1] = 'bob'; And I would like to be able to produce from this a tabl...

scoping problem in recursive closure

why does this work: def function1(): a = 10 def function2(): print a function2() ...

dynamic ul with sub-levels

i have this recursive code <ul> <% sql="SELECT * FROM cats ORDER BY orderid " rs.Open sql,Conn dim recArray If Not rs.EOF Then recArray = rs.getRows() dim i for i=0 to uBound(recArray,2) if recArray(1,i)=0 then call showMessage(i) end if next End If function showMessage(index) %><li><...

how do you write a function like print_r() that recursively prints objects?

i want to write a function that prints multi-dimensional objects which are text (or integers, etc.) into <span></span> tags and arrays into unordered lists. how do you make the function work recursively so that it prints everything regardless of what level it's at in the object? thanks! ...

How can I query all Childcontrols of a Winform recursively?

Iam using windows forms. How can I query all child controls of a Form recursively which have a certain type? In SQL you would use a selfjoin to perform this. var result = from this join this ???? where ctrl is TextBox || ctrl is Checkbox select ctrl; Can I also do this in LINQ? EDIT: LINQ supports joins. Why can't I use...