Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

Preparing for the Catalysts Coding Contest

As I already mentioned at Twitter, I’ll be attending the Catalysts Coding Contest (CCC) at Vienna on the 24th October. For this I’m going to prepare myself with the common tasks I encountered the last time and with the practice games from previous CCCs.

I’m thinking about coding the challenge in Python but if I get nervous I’m going to use Java — and for this I need an archetype to have a quick project setup with all the tools I can need.

 Python setup

Because I’m thinking about going the contest with Python I’ll set up a small set of libraries I’m going to use for the contest.

There won’t be any big things such as NumPy because I do not know that library — and what I do not know I do not want to use. Or at least not in a timed contest.

I’ll take my notebook with me to use however it is a good idea to have the required libraries grouped at a place together for further use. Besides I won’t have as much required libraries.

What I want to use is:

  • Python 2.7
  • IPython Notebook
  • a UnitTesting framework (eventually)

I do not know currently what type of unit testing to use. This is because in most of my time I do not write unit tests to my python applications — and sometimes this causes me a bit of development overhead.

Why bother with unit testing?

This is again a good question. As I mentioned before this can cause some development errors and overhead which is not the best when you developing on a contest. And besides this the contest works as follows:

  1. You get a problem description
  2. You get a sample input for the application
  3. You get the expected result of the input from 2
  4. You get some more challenging input
  5. You provide the result of the inputs to the system
  6. If all of your results is OK then
    • if this is the last level you are done, congrats
    • else you continue at “1. You get a problem description”

This is not the whole workflow, I’ve just taken a bit of it, to represent why do I want some unit testing. Naturally this could be done without much trouble of a unit testing framework. Let’s see how.

For example your problem is to calculate the sum of all digits in a number. This is one easy task so I’ve chosen this as a good one. For this I’d create a function which takes the number as a parameter, converts it to a string and iterates over the digits and calculates the sum of those digits. And the return value is the sum.

In the main block of the script I’d call the method first with the sample input and compare the sample result with mine. If it is OK, then go otherwise fail with a big, nasty exception. And after this I’ll call the method with the challenging inputs and print their result to the standard out. Eventually I should write them to a file because it could grow big (ok, not for the counting but other problems).

So, with this rubber-ducking I made up my mind to ignore the unit testing. However do not let fool ourselves: to hold the results ongoing OK I have to ensure that if I change the method it will yield the same result. And for this is unit testing essential: write test to ensure the sample result — and if the level is complete (so the challenging input resulted as expected) we can take the challenge input to the unit tests.

I thing I’ll take a look at the embedded unit testing in Python (unittest module) because it is sufficient and I do not need fancy tests — just little assertions. Alternative is to create a main script where I import my contest-script and when launching the main script it starts the defined tests.

For IPython Notebook goes almost the same: it is an easy tool to create and run Python code. However I could not manage to import other notebooks into the main one although there are some posts on the internet how to do it.

I do not mind it, so I ended up with simple Python scripts and probably I’ll use PyCharm at the contest because a good IDE is better than vim for me.

Java setup

You can ask why do I bother with a Maven project? Some IDEs provide unit testing capabilities and such. However sometimes I use 3rd party libraries with common functionalities. And to include those in every single project could result as a pain in the neck. So for this I’ll take the overhead of a maven archetype and a maven project.

This is the easiest way to have those files generated which I need — such as in the Python case.

Besides this I always wanted to know what’s behind archetypes and how complex is to create one. After creating this one for the Contest I have to tell you: it is easy.

There are two ways: create an archetype from scratch oder create a project and let the archetype generated for you by Maven. I went the second way because I needed runnable Java sources in my archetype.

After you are done with your project, go into the folder, remove every folder you do not need (mvn clean and remove all IDE specific folders and files if you do not want them to be delivered into your archetype),

After this is set, execute

mvn archetype:create-from-project

The resulting archetype’s sources are in under target/generated-sources/archetype if you call here mvn clean install it will be added to your local maven repository.

Now you can create a project based on your archetype:

mvn archetype:generate -DarchetypeCatalog=local

With the local modifier you tell maven to only use those archetypes which are locally installed.

So now you have an archetype and can create a project from it just like from any other archetype. Friday evening I released version 1.0 from the configuration where I included a JAR for the archetype. If you want to install it locally, you can download the JAR and install it with

mvn install:install-file -Dfile=contestconfig-archetype-1.0.jar -DgroupId=hu.japy.dev -DartifactId=contestconfig -Dversion=1.0 -Dpackaging=jar

If you take a look at the sources, you’ll see that while developing I stuck to Java 8 because I use some lambda expressions to have less code cover the main functionality. So, if you want to use this archetype and the generated project, get a JDK8.

Conclusion and Sources

As in most projects I am not done yet. This is just a basic configuration I created for myself. After the contest I will find some improvements to accomplish to be more versed for next year’s challenges.

I encourage you to attempt the contest — you do not need to come to Vienna, you can do it online too. In this case you have your own room with your own pace. But it is a good try for yourself.

And the sources (modules.txt and archetype) are provided at my GitHub repository. Feel free to use it.

Share the knowledge!

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below

%d bloggers like this: