Port of schuchert.wikispaces.com


Katas.RecursiveDecentIterator

Katas.RecursiveDecentIterator

Problem

You need to get a list of all of the files in a directory system (or walk a n-ary tree really). You want to provide one file at a time to the caller. For example, given the following directory structure:

wikispaces/
├── assets
│   └── css
│       └── style.scss
├── bin
│   ├── check_site
│   └── serve
├── _config.yml
├── _includes
│   ├── aside
│   │   ├── collapsed
│   │   ├── end
│   │   └── start
│   ├── include_md_file
│   ├── links_for
│   ├── site_url
│   └── toc
├── index.md
├── pages
│   ├── 3Questions-Smalltalk.md
│   └── aop
│       └── AOP_Field_Stones.md
── search.html

And given the following code:

DirectoryIterator iter = new DirectoryIterator("/wikispaces");
while(iter.hasNext())
   System.out.println(iter.next());

The output should be similar to the following. The order is not important. The iterator returns all files. You might only return the names or the names with their directorie. You could introduce filtering.

Why write it this way? This forces you to write what is traditionally a recursive algorithm (DAG traversal) without using recursion.

There are other ways to address this problem:

While you can certainly do these, that misses the point of practicing writing a traditional algorithm in a way that is possibly different from how you’ve done it in the past. This exercise will give you some general skills you’ll be able to apply to other graph traversal algorithms.


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.