Post moved to its own thread.

What have you tired?

Post your code and the questions about the problems you are having with it.

You need to explain the algorithm for creating the output...