Don't code everything at once.
Pick an example
Work through the example on paper
Type in a test
Type in the code
Repeat until your code is correct on all examples