web analytics
Skip to main content
elephant_jokes

Learning recursion through elephant jokes

(Note: This post could also be titled “Telling elephant jokes with python”)

When teaching my students how to understand recursion, I always refer to the classic elephant joke: “how do you kill a blue elephant? With a blue elephant gun”.

They look amazed at first, until they notice that the joke is indeed a recursion, since, in order to kill an elephant with a certain colour, you should make it change the colour, then try to kill it again, until it turns blue, and the recursion ends.

So, I just implemented a little python program, with an “Elephant” class and a “kill” recursive method, in order to better demonstrate how recursion works. Have fun:

class Elephant():
	def __init__(self, colour):
		self.colour = colour
		
	def change_colour(self, new_colour):
		self.colour = new_colour
		print "Elephant's colour turns to " + self.colour
		
	def hold_nose(self):
		print "Hold elephant's nose..."
		self.change_colour('blue')
		
	def tickle(self):
		print "Tickle the elephant..."
		self.change_colour("pink")

	def strangle(self):
		print "Strangle the elephant..."
		self.change_colour("red")
	
	def kill(self):
		if self.colour == 'blue':
			print "Kill the elephant with a blue elephant gun!"
		elif self.colour == 'pink':
			self.hold_nose()
			self.kill()
		elif self.colour == 'red':
			self.tickle()
			self.kill()
		elif self.colour == 'white':
			self.strangle()
			self.kill()
		else:
			print "There aren't " + self.colour + " elephants!"

e = Elephant('blue')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('pink')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('red')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('white')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('yellow')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

Here you have the program results:

How to kill a blue elephant?
Kill the elephant with a blue elephant gun!

How to kill a pink elephant?
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a red elephant?
Tickle the elephant…
Elephant’s colour turns to pink
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a white elephant?
Strangle the elephant…
Elephant’s colour turns to red
Tickle the elephant…
Elephant’s colour turns to pink
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a yellow elephant?
There aren’t yellow elephants!

internationalization-translation-flags-globe

How to compile PO files

Everybody knows gettext. Well, almost everybody… It is an internationalization and localization system popularly used by many applications and systems everywhere.

It is based in source files that hold the original texts of your application (usually written in English), and the corresponding translations for all the languages you need.

(more…)

Google Plus

What happened to Google+?

This interesting piece in Business Insider talks about the changes recently announced by Google in its strategy with Google+. According to the article, although millions of people use the social network daily, most of them only do it in order to connect to one of the many Google services that are kind of linked together by Google+.

At the end of the day, according to data gathered by researcher Edward Morbis, only about 6 million out of 2.2 billion profiles are actively using Google+.

(more…)

wordpress-facebook

Relative url in og:image using WordPress Facebook plugin

I was struggling one of these days trying to figure out why my WordPress blog posts didn’t show the proper image when shared in Facebook.

I had already tried some different Facebook plugins, including the official Facebook plugin for WordPress, but I couldn’t find the reason. When I used the Facebook debugger, I always got an error message like this one:

Object at URL ‘http://mysite.com/mypost-path’ of type ‘website’ is invalid because the given value ‘/images/my_image.jpg’ for property ‘og:image:url’ could not be parsed as type ‘url’.

Strange, because my wordpress blog configuration looked fine, my theme (Atahualpa) supported featured thumbnails, and my posts had a featured thumbnail set.

(more…)

Django Python

How to generate a Django password

OK, this is for anyone who needs to manually generate a Django password. I was faced with this problem because I needed to manually insert a new Django user in the database. So, how to take the desired plain text password and to convert it properly so that you can insert it in the “password” field of the auth_user table in the Django database?

To answer this question, I had to take a look at the way Django handles user passwords. Basically, I needed to look at the following Python files from Django repository: utils.py, utils/encoding.py, utils/functional.py.

I’ve grabbed just the functions that I needed and put them together in the following piece of code that you can use.

(more…)