Searching for file in directories recursively

You could use this overload of Directory.GetFiles which searches subdirectories for you, for example: string[] files = Directory.GetFiles(sDir, “*.xml”, SearchOption.AllDirectories); Only one extension can be searched for like that, but you could use something like: var extensions = new List<string> { “.txt”, “.xml” }; string[] files = Directory.GetFiles(sDir, “*.*”, SearchOption.AllDirectories) .Where(f => extensions.IndexOf(Path.GetExtension(f)) >= 0).ToArray(); … Read more

Inheritance and recursion

This is expected. This is what happens for an instance of B. class A { void recursive(int i) { // <– 3. this gets called System.out.println(“A.recursive(” + i + “)”); if (i > 0) { recursive(i – 1); // <– 4. this calls the overriden “recursive” method in class B, going back to 1. } … Read more

Recursion schemes for dummies?

Extremely loosely speaking, a catamorphism is just a slight generalization of fold, and an anamorphism is a slight generalization of unfold. (And a hylomorphism is just an unfold followed by a fold.). They’re presented in a more rigorous form usually, to make the connection to category theory clearer. The denser form lets us distinguish data … Read more

Using self.xxxx as a default parameter – Python

larsmans answered your first question For your second question, can you simply look before you leap to avoid recursion? def makeList(self, aNode=None): if aNode is None: aNode = self.root treeaslist = [aNode.data] if aNode.lChild: treeaslist.extend(self.makeList(aNode.lChild)) if aNode.rChild: treeaslist.extend(self.makeList(aNode.rChild)) return treeaslist

How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

If you want to extract the files to the respective folder you can try this find . -name “*.zip” | while read filename; do unzip -o -d “`dirname “$filename”`” “$filename”; done; A multi-processed version for systems that can handle high I/O: find . -name “*.zip” | xargs -P 5 -I fileName sh -c ‘unzip -o … Read more

Recursive function to generate multidimensional array from database result

Some very simple, generic tree building: function buildTree(array $elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element[‘parent_id’] == $parentId) { $children = buildTree($elements, $element[‘id’]); if ($children) { $element[‘children’] = $children; } $branch[] = $element; } } return $branch; } $tree = buildTree($rows); The algorithm is pretty simple: Take the … Read more