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 ;)

Catalysts Coding Contest on 24th October 2014

As I mentioned earlier, I’ve attended the Catalysts Coding Contest.

Although I prepares a project in Java and in Python, Catalysts changed the contest’s type: this time it was a simulator-based contest, so you had to provide controlling input parameters for a simulator application — provided by Catalysts.

The objectives were validated by this simulator, and if you managed to accomplish the level, you’ve got a result file (generated by the simulator) which you had to upload.

General impressions of the contest

Attending the contest on-site is far more other than on-line. First of all there is the noise and other contestants you have to… endure, I say it so. Naturally you can omit noises while listening to music.

The biggest difficulty is the LAN: Catalysts provides on-site LAN to access the contest site, however when 200 participants simultaneously access the same server and site: it can get laggy. Or even brake down — as it happened this time. After some minutes the server was not reachable. Most of the contestants finished level 1 and they (and among them I) could not upload the results — and get the data for level 2. For this some people from Catalysts walked through the room and shared the input for the next levels via a USB stick. Not the best solution — but it was better than waiting.

Fortunately they fixed the issue, however the WLAN was slooooow. After finishing some levels I had to wait 2-3 minutes to get to the level submission. This was not the best thing. I’m thinking about buying a LAN adapter for my Mac. I hope this would help next time to be a bit faster.

I finished at place 24 from 106 (this 106 includes single participants and teams from up to 3 people). I think it is not bad. However probably I could have been better: after finishing level 4 (I needed less than 2 hours) I was at rank 5. However I dropped down slowly to the 24th place. But I do not mind because silver ist good (yes, this ranking gave me a silver medal).

And participating on-site gives you the opportunity to take part of the tombola and win cool prizes. I’ve won a BeagleBone Black starter kit — and I am happy with it. I’m interested in IoT and this gives me some tools to extend (so buy some extensions) and write articles about this topic too. But that some time later in 2015.

Some code

As I mentioned above, this time the contest worked a bit other than I prepared myself. However I’d like to share some code with you — to be prepared if the next time it will be the same.

The simulator needed parameters. This you could provide in either of two ways:

  • console (stdin)
  • TCP

I used the TCP version in my application because it was a bit easier because I did not want to redirect my test-output to the error console. So this gives a good opportunity to show how to communicate through a TCP socket in Java and Python

Java socket communication

The Java version of the socket communication is simple. You only need a host and a port to connect. To read from a socket, you get an InpurStream object, and you can send messages through a DataOutputStream.

I took a simple approach and made the version to adapt to the provided CatCoder example: Autonomous Car Warmup.

try {
    String serverMessage;
    clientSocket = new Socket(host, Integer.valueOf(port));
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while (true) {
        while(true) {
            serverMessage = inFromServer.readLine();
            if("update".equals(serverMessage)) {
            // split and parse the server's message to use it later in the calculate method
        System.out.println("FROM SERVER: " + serverMessage);
        outToServer.writeBytes(calculate(serverMessage) + '\n');
} catch (IOException e) {
} finally {
    if (clientSocket != null) {

Python socket communication

There are two types of reading from a socket in Python. One is a generator function (which uses yield to wait for input). The other is a simple function put into an endless loop. I’ll show both here because it is good to have two versions of the same functionality.

Simple reader

The simple reader consists of a few lines with an endless-loop and some line reading from the socket.

def read_data():
    data = ""
    while True:
        tmp = sf.readline()
        if "update" == tmp:
        # split the data here
        data += tmp
    return data.strip()

This code reads the data until it is the “update”. Then returns all the lines read.

Generator reader

The second one is a generator function.

def readlines(sock, recv_buffer=4096, delim='\n'):
    buffer = ''
    data = True
    while data:
        data = sock.recv(recv_buffer)
        buffer += data

        while buffer.find(delim) != -1:
            line, buffer = buffer.split('\n', 1)
            yield line.strip()

This one reads each line after another. It could have been made better (like the simple one above). It is always a question of liking which one do you want to use. I tend to the simple one because it is a bit more handy. However you get both of the functions in the source codes — and I show how to call them too.

The code examples are at…

… the same repository as the other preparation configurations at GitHub. For the Java part I did not extend the archetype now — eventually I’ll do it later. This is because I’m lazy and I do the contest in Python mostly.


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: