Don't code everything at once.
Pick an example
Work through the example on paper
Test first approach: type in a test before you code.
Type in the code
Repeat until your code is correct on all examples