How to implement the following:
I was intrigued on this problem and wrote a solution to it. See my github project.