Facebook Hacker Cup : Peg Game Problem Solution

Continuing with my earlier solution of the First Problem “Double Square”, here is solution and analysis for Second Problem “Peg Game”

Peg Game

At the arcade, you can play a simple game where a ball is dropped into the top of the game, from a position of your choosing. There are a number of pegs that the ball will bounce off of as it drops through the game. Whenever the ball hits a peg, it will bounce to the left with probability 0.5 and to the right with probability 0.5.

Read More …

Facebook Hacker Cup : Double Squares Problem Solution

A lot of people have participated and qualified in Qualification Round of Facebook Hacker Cup. Luckily I am also qualified for First Round. Facebook Hacker Cup Qualification Round consisted 3 problems. All of these problems were programmatically easy but tricky. Lots of people made very simple mistakes while solving these problem(including me).

First Problem of the round was:

Double Squares

A double-square number is an integer X which can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 32+ 12.

Read More …

RegEx to Convert String in TitleCase (Python / Django)

During web development I very often come across a problem to convert any string into TitleCase. Those who don’t know title case is the style of writing having first letter as a majuscule (upper-case letter) and the remaining letters in minuscules (lower-case letters).

So here is the python function to do that:

def title(value):
    """Converts a string into titlecase."""
    t = re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
    return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t)

Happy Coding

Read More …

Django ORM : What to do when the memory requirement goes high?

As the title suggest this post is regarding “Memory Requirement of django ORM”.
Actually some days back I myself faced this problem and wrote “Why Django ORM Sucks : It takes a hell lot of memory in processing“. From various comments and a thread which started on google groups, I came up with few points one should check while operating on large amount of data.

For showing points i’ll use following example:

for r in UserRating.objects.all():
	ratingDict = cache.get(r.movie.id)
	if ratingDict is None:
		cache.set(r.movie.id, [(r.user.id, r.rating)], 86400)
		ratingDict.append((r.user.id, r.rating))
		cache.set(r.movie.id, ratingDict, 86400)

So here are the mistakes in previous code.

Read More …

Installing Memcached for Django Application on Windows XP

For a project I needed Memcached. But the problem was that I have my development system on WindowsXP.

When I Google I got various links to install Memcached on windows, but there wasn’t any link that tell all about Memcached with Django on Windows

So Here we go:

  • Download memcached 1.2.4 Win32.
  • Unzip the binaries in your desired directory (eg. c:\memcached)
  • Install the service using the command: ‘c:\memcached\memcached.exe -d install’ from either the command line
  • Start the server from the Microsoft Management Console or by running the following command: ‘c:\memcached\memcached.exe -d start’
  • Use the server, by default listening to port 11211
  • Now we have to get something so that python can talk with our memcached server.
  • Read More …