Friday, 22 November 2013

Some thoughts

The recent test went pretty well I think! It was simply and straight forward, which is exactly how tests should be. If you went to the labs or listened to the lectures and actively finished the exercises, it shouldn't have been that bad.

Some thoughts on the course so far:

1. I really wish we'd gone more into depth with big O notation and runtime complexity. It was a good refresher to be taught about the different sorting algorithms but it still felt a little rushed.

2. The recent lecture was really useful! I hadn't heard of the property method before, so now that I know of it this will definitely help with making attributes more private in the future. These are exactly the kind of tips I was expecting from this course.

Until next time!

Tuesday, 12 November 2013

Hectic

So this week has been crazy hectic in terms of assignments, essays, and presentations in general. That assignment #2 wasn't as bad the first one, in my opinion. But I'm sure there are different and more efficient ways of going about solving it. There is one thing that I learned when working on the second assignment, and was very disappointed by. Python doesn't have switch cases. WHY? You can read more about that here. But there are still numerous ways to go about comparing a variable to integral values.

1. Using if and elif blocks.

if variable == 1:
    print ("One is a prime.")
elif variable == 2:
    print ("Two is a prime.")
elif variable > 2:
    print ("Prime numbers are only odd now!")
else:  # similar to default
    print ("Numbers!")

This is probably a common method and an easy one. Though it can get messy if you write really long if statements.

2. Using dictionaries (taking advantage of the fact that they are similar to associative arrays)

def check(var):
    return {
        1: 'one',
        2: 'two',
        3: 'three',
        }.get(var, 'not important') # use get as default if x not found

or:

def zero():
    return "Zero."

def one():
    return "One."

def two():
    return "Two."

def prime():
    return "Prime numbers are odd now."
   
def check(var):
    return {0 : zero(),
            1 : one(),
            2 : two()}.get(var, prime()) # use get as default if x not found

A third method would be to create your own class that acts similar to switch/case. That'd be good practice!