Tuple unpacking example, and to!

bearophile bearophileHUGS at lycos.com
Sun May 9 05:54:04 PDT 2010


Doing the Google Code Jam with both Python and D shows me that both languages are not optimal for this purpose (but both can be used).

Like other online programming contests this asks the programmer to invent correctly working algorithms in a very short time. Some of the algorithms can require multi-precision integers too, and generally you need a good amount of brain power and focus to find the algorithm solutions. Often you are not smart enough to find the solutions, so you are always on just one leg.

Compared to other online contests Google Code Jam is also data-driven: all tests are divided in two parts, one with a small input set and one with a large input set. A slow algorithm/language can be enough for the small data set, but your algorithm and language must be fast if you want to find the answer in the max time allowed (8 minutes).

Python is excellent to invent the algorithm that solves the problem, there is no compilation stage (on the other hand the programs are usually small enough, so with DMD the compilation time is quite small. So I think it's mostly a psychological thing), there are no types that you have to put in the code that can distract what you are doing, you don't have to import libs to do most things, you can write very little code and build your solution progressively, sometimes even starting from the interactive command line shell, and you have much less things to worry about, like integer overflows, slicing arrays, etc. The very clean syntax allows you to focus on just the semantics of the algorithm.

D, especially D2 with Phobos2 is not bad, and it's fast enough, but when you invent a new algorithm you want to give 101% of your brain to invention of the algorithm that solves the problem, and not to problems like integer overflows, unsafe slicing (slicing more than the length of the array, this is not a problem in Python, but requires stupid extra tests in D), leading/trailing spaces not ignored by the to!long, missing tuple unpacking, and so on and on.

So Python is good to invent the algorithm and write a working program, but often even using Psyco it's often too much slow to give a fast enough solution for the large data set. D is fast enough for the big data set, and it's not bad for programming in general, but you need to put more brain in writing the D code and you have to write more code and you quite often can put more bugs in the code (for example caused by conversion from signed to unsigned numbers because of a silly unsigned array length). And generally you don't want to waste time to write a first solution in Python and to translate it to D2 to solve the large data set.

I have seen people solve all problems in C++ in a short enough time, so maybe all those problems are just mine, and I am just less intelligent/expert than them. Yet, good tools can increase people "intelligence".

D and Python are not the only languages I know, but the others I know are even less fit for purpose.

So to perform a contest like Google Code Jam I am unhappy with all languages I currently know :-( Maybe the Boo language (for dotnet, but hopefully in future for JavaVM too) can be fitter for this kind of contest.

Bye,
bearophile


More information about the Digitalmars-d mailing list