Thursday, July 25, 2013

My TopCoder coding setup (update 4)

Update (Aug 6-th, 2013): New KawigiEdit-pf version. Now 2.3.0. Synced KawigiEdit-pfx with it. Some tweaks.

Update: Updated KawigiEdit-pfx to version (Incorporates changes from normal KawigiEdit. Also it renames the Outside mode window so that it begins with the word "Outside ", updated accordingly.

Around a year ago, I had a big , big, enormous problem. I was supposed to be writing blogs for the TopCoder Open, and I had absolutely no idea what to talk about (Speaking of which, whatever happened to this year's TCO blogs?) . So I improvised. I decided to rush a post about arena setups. It is not easy to bring everything to work in the optimal way for you. Back then someone asked if I would release my modified version of KawigiEdit. Although I said I was going to release it, it took me this long to do it.

Why I modified KawigiEdit

KawigiEdit is a great Arena plugin, and everyone just starting in Topcoder should begin with this one. That's the reason I chose it. However, there were some issues with it. Back when I was starting to use TopCoder plugins, the version of KawigiEdit was not very updated and there was an issue with problem method signatures that returned double, TopCoder rules admit an absolute or relative error of up to 1e-9. The old KawigiEdit, however, compared doubles without taking care of this. So whenever you tried to solve one of those problems in KawigiEdit, it was likely that you would have KE tell you the results are wrong, even though they are not - they are really within the accepted range. With Kawigi not around to update the plugin, I had no choice but to add the comparison stuff myself.

Eventually, this issue got fixed by pivanof, but I started slipping downwards the slippery slope of editing KawigiEdit. Overtime things got crazy. It all started when I wanted to add separator lines between the output of example cases. I use print statements to debug issues with my problems and when a test case gets full of output, it is hard to find the test case in the normal KawigiEdit output format.

I added these big lines =========================== to the output. But I had a new problem. The lines were good at separating example cases because they were very visible. Perhaps too visible. They were starting to distract me. I needed these lines to of a color that didn't contrast as much as the background. But adding color to the KawigiEdit output was looking very problematic.

It occurred me that I could use ANS color codes in the kawigi edit output . But then I would have to make the output appear in a terminal window instead of KawigiEdit's output tab... I had mixed feelings, but then I noticed "If I make output appear in terminal, I will also have the benefit of being able to read output and my code at the same time!" unlike the usual KawigiEdit experience... Then I noticed another good benefit. Closing the terminal window would forcefully kill the tester program. Killing programs was something that KawigiEdit was having issues with until a recent update.

The problem was how to move the output to a terminal window...

The gnometerm script

It was not easy to do it. But with the help of a lot of tweaking and a program called wmctrl. I was able to make a program that would run any command inside a terminal window. There were many requirements. The terminal window will be always on top, so that you can type on a larger window without it going missing. After the command finishes execution, the terminal shouldn't close. The active window should still be the coding window. Also, we don't want terminal windows to pile up, it is best if the program closes the previous one. But only this kind of terminal windows... you wouldn't want other terminal windows to go away...

This program is adapted to work with gnome-terminal, the default terminal application in Ubuntu. I am not sure how possible or not it is to make it work in other terminal applications.


# Run command in terminal window
# (c) Victor Hugo Soliz Kuncar under the Zlib/libpng license

# Terminal geometry. Read gnome-terminal manual for more info
# Basically, this is what makes sure the terminal spawns at the left side of the
# screen
# Terminal profile, I have a profile called KE
# you can leave both options empty...

TITLE='** TopCoder **'
# The window title for the terminal window. It is *not* merely aesthetic.
# It should be something unique that you are sure won't be shared by a window
# you don't want to be closed automatically whenever this command is called

if [ "$1" = "_act" ]; then
wmctrl -r "$TITLE" -b add,above
# == Activate window command: ==

# The idea is to activate the code window after the terminal window is created
# so that you can still code on it even after the output terminal appears.

# If the editor is undocked, the code window is called Outside KawigiEdit...:
wmctrl -a 'Outside KawigiEdit'
# Else it will be the normal competition arena window:
if [ $? = 1 ]; then
wmctrl -a 'TopCoder Competition Arena - Coding Phase'

# why twice? Because once seems to fail 1/20 of the times...
sleep 0.1
wmctrl -r "$TITLE" -b add,above
exit 0

if [ "$1" = "_monitor" ]; then
# == Monitor command: ==
# In this mode,
# - Call the activate command as another process so that there are no
# delays before calling the compiler.
$0 _act &
# - Run the command (provided to ./gnometerm as arguments)
# ${@:2} means "all arguments except first one"
read aok
# read aox makes the bash program wait until someone presses enter.
exit 0

# With a tool called wmctrl, close the window with title 'Topcoder Test cases!' so that
# only one of those windows exists at once:
wmctrl -c "$TITLE"

# Run the terminal: The terminal will run this script in monitor mode...
gnome-terminal $GEOMETRY $PROFILE --title="$TITLE" -x $0 _monitor $@ 2> /dev/null & disown
# 2> /dev/null is just so output doesn't go to KawigiEdit's tab...
# & disown so that killing this script doesn't kill ther terminal.
exit 0;

Modded KawigiEdit

Anyway, by using this script and placing it in KawigiEdit's work folder (and setting it as executable). I am able to put things like ./ python $PROBLEM$.py instead of just python $PROBLEM$.py and the execution will appear in the terminal. There are various issues with vanilla KawigiEdit and this approach.

It is convenient to have both compile messages and output appear in the terminal window. A bit of a problem is that KawigiEdit will automatically switch to the compile and output tabs whenever you run tests.

I had to change that. In fact, I removed the output tab altogether. I would remove Compile, but some of the least used languages still use it. Instead, the compile tab is only switched to automatically when there were "compile errors". Errors in the compile command. For my Java, c++ and Python setup, I use a single command to compile and run (I do not use the compile option in KawigiEdit and instead place an "echo" in the field ). The commands do various things. Specially my c++ command is not content with just compiling the program and running it. It also runs it in valgrind... and recolors the compiler messages :)

Anyway, the important thing is that I have a KawigiEdit mod. It has various "features" / things done different to the KawigiEdit I released before:

  • The test case output is very different. Besides of the ======== separators I mentioned, green and red ANSI color codes are used. Also, it has less clutter as it doesn't show your answer to a test case unless it was wrong. It also uses simply +, x and T (all colored) to say if a test case is correct, wrong or failed the time out. Because of the ANSI color codes, you need to use a trick similar to the gnometerm script to run the commands if you want to use this KawigiEdit mod.
  • The languages tab in settings is split in tabs, instead of having all settings in a single tab.
  • When you click the run tests button, the "Compile" tab will not be automatically selected. Unless the compile command you assigned has an exit code different to 0.
  • There is no output tab. If the run command you use has any console output, it will be added to the compile tab.
  • Instead of inserting "Powered by ... !" in your code it inserts a message that just says the editor version.
  • It doesn't say "you are a stud!" anymore. It says "Seems Ok (... Is It?)". I was never a fan of the stud thing.
  • The version is KawigiEdit-pfx 2.1.9. p stands for pavinof, f for ffao and x because it is my mod :).
  • Obviously , it supports python, just like KawigiEdit-pf.
  • So I would guess it is Linux-specific? Even gnome-specific

So if you are still interested, here is the jar: It has source code too:

Run script examples

Like I mentioned, I compile and run in a single command (which is called by Here are some mild examples:

./ python $PROBLEM$.py. It is easy for python :)

The following script works for Java, and adds a memory limit (Pass in class name as argument, if the script is called call it like: ./ ./ $PROBLEM$):

echo "compiling..."
javac $
if [ $? = 0 ];
java -Xmx64m $1

No comments :