jump to navigation

There’s always an easier way March 8, 2007

Posted by sdpurtill in Python.

I am no beginner in Python, but I’m definitely no guru. So I still have problems expressing some things. It’s like learning Spanish — you know enough of the language for others to understand you, but there is always an easier way to convey what you want to say. I know enough Python for the computer to understand me, but here is a situation that just came up that shows there’s always an easier, shorter way to do things.

def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  str = ''
  i = 1
  total_cli = self.custodian_total_clients
  for cli in self.clients.all():
    str += cli.name
    i += 1
    if i <= total_cli:
      str += ' | '
  return str

I knew there had to be an easier way to do it, but I just couldn’t figure it out. So I asked people that knew more than me, i.e., the guys on the #django IRC Channel. Two minutes later, and I have a nice, elegant, even readable solution.

def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  return ' | '.join([client.name for x in self.clients.all()])
custodian_clients = property(_get_custodian_clients)

Sometimes I am amazed by my stupidity.


FizzBuzz :D February 27, 2007

Posted by sdpurtill in Python.
add a comment

In a recent blog post on Coding Horror entitled Why Can’t Programmers… Program?, they talked about the fact that the majority of so-called programmers are useless when it comes to solving problems. Apparently they give people 10-15 minutes in an interview to solve this simple problem.

After a fair bit of trial and error I’ve discovered that people who struggle to code don’t just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

So I set out to develop questions that can identify this kind of developer and came up with a class of questions I call “FizzBuzz Questions” named after a game children often play (or are made to play) in schools in the UK. An example of a Fizz-Buzz question is the following:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I suppose I could have written it in Ruby, ActionScript, PHP, ColdFusion, and JavaScript. But I just learned Python, so I figured I might as well do it in that.

for i in range(1,101):
  if i%3 == 0:
    if i%5 == 0: print 'FizzBuzz'
    else: print 'Fizz'
  elif i%5 == 0: print 'Buzz'
  else: print str(i)

Count it. 6 lines. Took 5 minutes. Gotta love Python. I don’t know why I’m bragging, that was a joke.