Sunday, May 23, 2010

Code Jam Round 1

Code Jam’s Round 1 is over now and I didn’t make it to the next round as last year :-(. I am pretty sad and writing this entry with heavy heart but on the other hand quite satisfied by my performance as I was eliminated with very close competition. Let me describe you my sad story.

Round 1 consisted of 3 sub rounds of 2hr 30min duration each and top 1000 finishers from each round were to qualify to Round 2.

Round 1A was at 6:00 AM, Saturday, May 22, 2010. I was in my friend’s home on that day. I woke up early with great enthusiasm after a 4 hours nap and started the computer. The development environment is not set on that system. “Its ok, no problem, lets setup the development environment”. It penalized me about 15 minutes. Started solving the first problem, applied brute force and it was accepted but I took 1hr 15min to solve it. Now I was just looking for one more solution for small input for a finish under top 1000. Started the second problem, solved it, submitted it and “Aah! INCORRECT RESULT”.  I made another attempt and same result again. Round over and I ranked about 1360 with 23 points. After system tests, I managed to finish at 1269. I was pretty happy with my performance as I was very close for getting through. The person at 1000th place was also with the same score but with less time penalty i.e. 51min 40sec.

Round 1B was at 9:00 PM on the same day. O boy, power cutoff from 8:00 PM – 10:00 PM, but I will participate from 10:00 PM onwards. At 10:00 PM, I entered in to the contest. 1 hour is already spent in load shedding so I have to be fast and accurate. I started Problem A, solved it in almost 43 minutes and it got accepted. Problem B was also easy, just a bubble sort algorithm. I got it accepted in almost 35 minutes with one wrong try. I finished at 56 points with total time 2hr 26min 06sec (1hr 26min 06sec of contest + 1 hr of load shedding). Ranked 1410 and after System Tests 1352. The interesting part is that the person ranked 1000 spent 1hr 26min 15sec so I am qualified if I subtract 1 hour of load shedding. :-)

Round 1C was at 2:00 PM, Sunday, May 23, 2010. Load shedding from 2:00 - 4:00. No chance. So this is how I eliminated :-(.

Lessons Learned:
1. Most importantly, I need more practice as in Round 1A, I got complete time but was just making a stupid mistake in 2nd problem.
2. Make sure the development environment is setup and working before contest.
3. Take at least 8 hours sleep before any programming contest.
4. If above rules are being obeyed, no one can stop you from being qualified to Round 2 even a power cut off.

Country Statistics:
Click here to see the results of all countries.

Tuesday, May 4, 2010

Clean Code by Rober C. Martin (Uncle Bob)

Thanks to JongMan Koo for sharing this beautiful book. Frankly, I don’t like reading e-books but this book is really appealing and I am enjoying reading it. Most of the content of the book forced me to practice rather than just reading. Robert C. Martin has some great books related to principles of software design and patterns in his account. In this book he mainly forces on practicing the techniques of writing good code. He wrote:

“I can teach you the physics of riding a bicycle. Indeed, the classical mathematics is relatively straightforward. Gravity, friction, angular momentum, center of mass, and so forth, can be demonstrated with less than a page full of equations. Given those formulae I could prove to you that bicycle riding is practical and give you all the knowledge you needed to make it work. And you’d still fall down the first time you climbed on that bike.”

The tone of the writer is strident most of the time in this book. The style is a bit strict that attracts you a lot and you feel an emotional attachment with every word you read. He also explains how the bigger products are buried just because of nothing but bad code. He forces the reader not to write too much cluttering comments as it is difficult to maintain them with frequent changes, instead make your code self explainable. The naming conventions are beautifully explained. One sentence is constantly ticking me in this regard:

“You should name a variable using the same care with which you name a first-born child.”

“Uncle Bob” mainly explains what are the main components of a clean code in the light of various quotations made by some great scholars and practitioners in the history of computers. I haven’t read the complete book yet so I will not be able to summarize all the aspects the author covered but I assure you that some initial reading will definitely made you think about common mistakes we normally make. It is not just a theoretical book, it is for practitioners so a lot of real time examples are given in the final chapters to improve readers' capability of understanding bad practices and adapting better ones.

If you are a coder with around 1 year of experience and you have seen a couple of ups and downs in your project, this book is highly recommended.

Thursday, April 15, 2010

Implementation of Full Text Search using MySQL database


“Search? Aha…lets use wildcard for now”.

This is a very common phrase that appears when any site based on MySQL database requires “search” feature. Today, I read about another method of full text search that is not only faster but just took 10 minutes to be implemented.

Let say you are using something like:

SELECT * FROM blog WHERE body like ‘%full text query%’ OR title like ‘%full text query%’

Above query may take a few milliseconds to execute but if you are required to join this table with other tables. Huh! Hell tough. And you are just fed up by adding too many indices on the columns but  in vain. What to do? Here are some simple steps as alternative.

Step 1. Create a FULLTEXT index :
ALTER TABLE blog ADD FULLTEXT(title, body)

Step 2. Replace the above query with this one
SELECT * FROM blog WHERE MATCH(title, body) AGAINST (' full text query')

Just make sure that whatever you are passing to MATCH() is passed to FULLTEXT() in Step 1.

Step 3. There is not step 3 :P, you are done.

Advantages:
1.       Almost 10 times faster
2.       Results are sorted by relevance
3.       Each word of query string is matched.


References: