Recently, I’ve been working on the problems on Project Euler, and I’ve done the first 16 problems (in Java), although I will freely admit that I had help on two of the most difficult ones. I do intend on continuing to do the problems, and I am currently working on problem 17, however I paused to write the problem runner framework I’m going to talk about in this post.
What I had started to do, was to write each solution in it’s own Class, and have the main(String args) method output the answer. This was fine for the first few problems, and I could have continued to do it like that for all of the problems - however I wanted to be able to run all the problems at once, or a specific problem, and get the answer(s), or not show the answers but still get the timings.
After chatting with one of the Senior Dev’s at my job, he pointed out that what I wanted to do was basically the Command pattern. He sent me some example code, although once he’d said “Command pattern” I knew exactly what it was that I needed to do.
Thus, my problem runner framework was born, and whilst fairly simple, it does employ some techniques that the beginning Java developer might not be aware of. So, what I am going to (try) to do over the next few weeks is write a series of posts that show how I wrote it, partly just to have some content on my blog (which I am really, really lazy at updating), secondly to see how good I am at explaining something like this, and thirdly - it might actually be useful to someone.
The output of the problem runner framework looks like this:
As you can see, I have output a list of the problems, and the time taken to solve the problem, but I haven’t shown the answer.
Well, you didn’t think I was going to tell you the answers… Did you?
This also shows that I need to work on problem 5, 9 and 14 to try and optimise the solution to speed up performance, Project Euler says that problems “should” take under a minute to solve, however I’d still like to improve the code.