Lesson in python and keeping my mouth shut

I was asked to write a factorial function—on the spot in a pressurized situation—as a challenge and guess what, I couldn’t do it. This is a simple thing to do and it unfortunately took me a bit of time away from the pressure to accomplish it. I’ve never been asked to replicate a function that someone already wrote a library for. As such, I was caught way off guard and unfortunately got crazy defensive saying things like “I would never do something like that, it already exists”. Double wrong thing to say. This is undoubtably a side effect of me being a novice. I will make no claims to being a proficient programmer…yet. You better believe that is going to change. Unfortunately I failed miserably and in such a spectacular manner that it was comical.

At the time, I didn’t know about some simple tools that probably everyone on the planet that knows how to (or took classes to learn how to) program already knows about. I knew about the += command that adds the item from a loop in python but, I didn’t know that there was an analog *=. Turns out, there is and I made a complete ass of myself for not knowing about it. Lesson learned—I need to start doing programming homework problems I can find online. Nonetheless, here is my first attempt at the function. I’m writing this mainly because it is cathartic to write and I need to own my mistake.


def fact(n):
result = 1
for item in n:
result *= item
return result

Of course, there are other things to add to the definition above such as checks to ensure that the list n is what you are expecting it to be. If it is a single number, then that needs to be handled.


def fact(n):
 result = 1
 if type(n) != list:
 result = n
 elif type(n) == list:
 for item in n:
 result *= item
 return result

If the input is 0, then that has to be handled as well.


def fact(n):
 result = 1
 if n == 0 or 0.:
 result = 1
 elif type(n) != list:
 result = n
 elif type(n) == list:
 for item in n:
 result *= item
 return result

And finally if a zero occurs in the list, one has to handle that. I did it by filtering them out.


def fact(n):
 result = 1
 if n == 0 or 0.:
 result = 1
 elif type(n) != list:
 result = n
 elif type(n) == list:
 temp = [ item for item in n if item > 0 ]
 for item in temp:
 result *= item
 return result

While this works, it is too little too late for my pressurized situation. I need to just keep practicing. I’m sure there are more elegant and easier ways to do what I did above but, it is the first thing I got to work properly.

hahaha

I’m still an idiot as the above code is completely wrong. Not for what it does, but because it is not a factorial. Oh well, still lessons learning. I’m happy to wear this mistake like a badge of honor. At any rate, this is correct.


def fact(n):
 result = 1
 if type(n) != int:
 print 'Error, fact takes an integer.'
 nList = [ x for x in range(1,n+1) ]
 if n == 0:
 result = 1
 for item in nList:
 result *= item
 return result

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: