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)) {
                break;
            }
            // 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) {
    e.printStackTrace();
} finally {
    if (clientSocket != null) {
       clientSocket.close();
    }
}

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:
            break
        # 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()
    return

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.

 

Custom printing for HTML with JSoup

Lately I’ve written about HTML parsing with JSoup. It just happened that I came across a problem for pretty printing or it is better to call it prettier printing of HTML documents with JSoup in another project and I thought this would be another good thing to share it with you through an article.

Continue reading

Test post with e-mail

So, another offline editing tool I try: email.
And to be honest: Quamana is not as good as it seemed: I have a couple of draft posts available however it loads only until the “more” tag — so it is unusable.

As I go along in offline mode I struggle. With Mac you only have commercial applications to use. OK, you can say: “If you have a Mac you have another 35 dollars to spend for MarsEdit.” Well, this is OK but I’m a fan of free software…
As you can guess for now I try MarsEdit beside this email thing. It seems more OK than Quamana or Ecto because it accesses all your draft posts — with everything you’ve written along the way. And if I have too much work to do I’ll stay with that tool. But I will summarize my experiences in a separate MarsEdit post.

For the emails now: it is quite convenient to use emails for posting. However here I do not see any possibilities to have source code included in the post. For formatting it has less option. I would say that using emails is a way to have quick posts created and send after you get to some internet connection. Formatting can be done later. Images can be included, heading seem currently impossible in this client. Alternatively I could format headings to my custom style with available tools but it would look like a piece of pain among my other, better formatted articles.

So for now this was the test post with an email. Next time I’ll write my conclusions about MarsEdit. Stay tuned.

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.

Continue reading

Website scraping with JSoup and XMLBeam — Part 3

In the last two articles I introduced website scraping with XMLBeam and JSoup respectively.

In this article I’ll do a final comparison of the two libraries — however do not expect anything professional. It will be just a simple run-and-measure-time analysis of the two libraries on my dataset — and on two machines.

Continue reading