

recursion help
i have a program i want to rewrite using recursion if possible. i have an iterative version of the program that works (i've tested it), but it's very cumbersome and if there's a recursive solution i'd like to use it.
the problem is like this: start with a list of objects that can be classified using simple rules. i want to form all subsets of a certain size that follow the rules. for example, the resulting set can only have 2 objects of type A, 3 of type B, etc. this is obviously easy to do iteratively using nested loops. the problem is that i want subsets of size 16, hence nested loops. here's my idea for a recursive version: Code:
function chooseNext(Set, Subset) if length(Set) == 1, return Set(1) else for each s in Set Subset = s return chooseNext(Set  s, Subset) not homework, if i was still in school i'd be smart enough to figure this out. 
03302012, 02:56 PM 
#1

Advertisement  [Remove Advertisement] 


If you have a working iterative solution I would stick with it, it is likely faster.

03302012, 06:13 PM 
#2

My cooter sweats, and reeks like rotting sea vermon.

Quote:
This. Does the solution have to be recursive? If not, don't bother. Recursion is appropriate in very special cases; otherwise it's just an attempt to be clever... which never ends well. In other words, have fun trying to understand what the code does three months from now, especially if the solution must be extended or modified. If the solution involving recursion is not immediately obvious (which it clearly is not) then file it under "being clever".
__________________
Use Linux and BSD 

04012012, 12:23 AM 
#3

Thread Tools  

