It's the final week of class and we're ending the course with hash tables and traps - sticky bits of code that look, at first, unproblematic, but end up producing errors or wildly unexpected results. I was excited to learn about hash tables, mostly because I had gotten endless error messages from python this year saying such-and-such data type was not hashable and I never understood what it meant. Now it finally makes sense to me. Hash tables simply assign some piece of data a unique number so that when we want to find that data again we can do so immediately just by knowing its unique address. A hashable data type is just a python Type that can be assigned a unique number (hence the data type needs to be immutable). In discussing hash tables we learned about the birthday paradox - that, given 366 possible birth dates, after looking through about 23 of these dates we are about 50% likely to find a recurring date. And that is the major problem with hash tables - in assigning unique numbers to data we are always bound to assign the same number twice eventually. There is no formula we can come up with to avoid this. So, our code must take measures to avoid this, and usually this means increasing the size of the hash table so that it is always significantly larger than the amount of data that currently has been hashed (assigned a unique number).
We also learned about a few interesting traps in python. One has to do with "global" variables. If we create some variable x outside of the function and then call x inside the function before defining it locally inside the function, then you will get an error saying x was referenced before it was created. This is because inside every function variables act as if they are all local, so to get around this you either need to create a local variable inside your function or define the variable x as a global variable inside your function.
Finally, the most interesting trap I thought had to do with creating default values in a function. Apparently if you set a default value equal to a mutable data type, like a list, then when that function is called a second time, or any time after its first call, the default value of our list will now be equal to whatever state the list was last in. In other words, say we have a parameter A that we want to have the default value of an empty list, so A = []. If after we run the function the first time A now equals [3] then when we call the function next we will not get a default value of A = [] but A = [3]. This happens for all mutable data types! So the big lesson: NEVER use mutable data types as default values for function parameters.
Well, the semester is coming to an end, all that's left is to study for finals...
Wednesday, 6 April 2016
Wednesday, 30 March 2016
Week 11
I'm looking back on my "real" week 6 entry about recursion and am noticing I've learned a few things since then. In week 6 I figured the only way to implement recursion was to have if statements in your code that could eventually get to a point where the recursive calls end. The thing is, I'm not sure if this is the only way to do it, though I still think it is, but in week 6 I was envisioning these if statements and all of the information they received as being completely contained in the body of the function. But while writing code for this second assignment, mainly for the depth first search, I made use of variables in a different way then I had thought of before - global variables. I wrote code that updated these global variables and, most importantly, one of them was directly used to prevent a branch of code containing a recursive call from being accessed. And in one of the last few lectures we also saw that you could add an argument to the function that gets updated with each new recursive call, and this argument can take on a value that effects an if statement appropriately. So, while I still think if statements probably are the only way to do it (I suspect the more advanced and sneaky code that is recursive must still contain some idea of an if statement or a condition), I'm seeing that the ways in which these if statements work is not always dependent on code that seems strictly contained in the function body… global variables and "outside" things can impact it too.
Looking back on some older posts from week 3 or so I realize how much we've covered in this class so far. I forgot we began by thinking about inheritance and composition and I find it interesting how I can still recall what those terms mean and how they've become second-nature to me now. In week 4 I couldn't imagine how Linked Lists could be implemented without themselves just being composed of python lists. At that point I really didn't see how versatile class-building could be. I thought python had certain data types already and all we could do was slightly alter how they operate. While it's true at some level all we are doing is altering these basic python data types whenever we create a class we are also altering and combining and manipulating them in such incredible ways they really can be almost unrecognizable after it's all done. I couldn't see to get past the idea that anything we wanted to behave as a list must inherit a list or be composed of a list. This class has really awoken me to how different ADTs and classes can be developed and the power behind them.
Week 10
We wrote the second term test. It was much more code-focused than the first test, which evaluated us more on our communication skills - writing docstrings especially. Though the content of this second test was definitely harder than the first I felt the two were roughly about the same level of difficulty, though the second was slightly more challenging. Thankfully, unlike the first test, there was enough time to write this one without feeling extremely rushed to complete it. Though, to my downfall, when there were 10 minutes left and I was looking my paper over I jumped up at the chance to leave early rather than continue looking my work over, and, as always is the case, realizing as I was walking from the exam that I had made a mistake. Well, either way, I'm happy with how the test went all in all.
This week we concentrated mostly on algorithm running time and Big Oh. We're doing Big Oh in csc165 at the moment and really the concept isn't too difficult. I do find counting the steps and judging the running time a little challenging though. I have to work through it quite slowly at the moment. After this second assignment I really see how important running time is. It makes me kind of worried that running time will be a primary concern from this point onwards because it's a little hard to compute. But I guess I have to get practicing it now so that I can do it later when it matters. The puzzle-solving algorithms really varied drastically in performance between puzzle type and algorithm type (breadth first or depth first search) and sometimes small tweaks to the code could improve the running time substantially. Most of the time it was pretty logical why certain modifications would make a running time difference. Loops are always to blame! Without loops the function is always in O(1) but as soon as there are loops of some kind the code takes more and more time to run - loops that are dependent on the data in the function's arguments that is.
*Edit: my week 10 post had difficulty publishing and when I came back to it I found only a third of it saved as a draft when I thought it was published in full. So I've just added the text that somehow got deleted.
This week we concentrated mostly on algorithm running time and Big Oh. We're doing Big Oh in csc165 at the moment and really the concept isn't too difficult. I do find counting the steps and judging the running time a little challenging though. I have to work through it quite slowly at the moment. After this second assignment I really see how important running time is. It makes me kind of worried that running time will be a primary concern from this point onwards because it's a little hard to compute. But I guess I have to get practicing it now so that I can do it later when it matters. The puzzle-solving algorithms really varied drastically in performance between puzzle type and algorithm type (breadth first or depth first search) and sometimes small tweaks to the code could improve the running time substantially. Most of the time it was pretty logical why certain modifications would make a running time difference. Loops are always to blame! Without loops the function is always in O(1) but as soon as there are loops of some kind the code takes more and more time to run - loops that are dependent on the data in the function's arguments that is.
*Edit: my week 10 post had difficulty publishing and when I came back to it I found only a third of it saved as a draft when I thought it was published in full. So I've just added the text that somehow got deleted.
Wednesday, 16 March 2016
Week 9
This week in CSC148 is the week of our second term test, so I've been focusing my energy on LinkedLists, Trees and Binary Trees. I found LinkedLists somewhat confusing the first time we went through them because I didn't have a good grasp of how L.front, L.back and L.size were functioning and I also was getting confused about what kind of object L.next_ is. Once I realized L.next_ was a LinkedListNode (or None) and not just a value like L.value it started getting clearer to me how all of this was supposed to work. I think my first time playing with LinkedLists I was assigning regular values to L.next_ and so print(L) was returning what looked correct but really the LinkedList was ruined and I later realized all of this when I tried doing L.append( ), etc.
For term test 2 I decided to work through all of the example functions provided to us for LinkedLists and Trees - there were quite a bit - but rather than use a computer and an IDE I stuck to pencil and paper. After I was confident with a solution I typed it up and tried it out and determined what, if anything, had gone wrong. I found this method to be extremely effective. I wouldn't say I studied for a particularly long time for the test, but my studying really paid off. Without the convenience of being able to run your code instantaneously and then mindlessly alter a few things here and there until it works, pencil and paper actually forced me to sit and understand what I was doing and go through the logic of it. It definitely increased my understanding of how LinkedLists and Trees worked and I really found that the mistakes I made I made only once or twice before learning my lesson (probably because I wasn't thoughtlessly changing code until I got it right but legitimately working through it).
This is definitely going to be my go-to study method in the future.
For term test 2 I decided to work through all of the example functions provided to us for LinkedLists and Trees - there were quite a bit - but rather than use a computer and an IDE I stuck to pencil and paper. After I was confident with a solution I typed it up and tried it out and determined what, if anything, had gone wrong. I found this method to be extremely effective. I wouldn't say I studied for a particularly long time for the test, but my studying really paid off. Without the convenience of being able to run your code instantaneously and then mindlessly alter a few things here and there until it works, pencil and paper actually forced me to sit and understand what I was doing and go through the logic of it. It definitely increased my understanding of how LinkedLists and Trees worked and I really found that the mistakes I made I made only once or twice before learning my lesson (probably because I wasn't thoughtlessly changing code until I got it right but legitimately working through it).
This is definitely going to be my go-to study method in the future.
Tuesday, 15 March 2016
Week 8
In week 8 of CSC148 we continued on with recursion. This time we explored Trees in more detail and also worked on Binary Trees. We we given a pretty long list of functions to implement for Trees, which turned out to be good practice for recursion. The first couple solutions were all pretty straight forward and it became easy to move into the following functions somewhat mindlessly, assuming the same "simplest case" would continue for the other functions and that the recursive code would all be similar. It was a good wake up call and reminder that you can't really go on autopilot while doing this stuff. Recursion is difficult and really takes a lot of playing around with, I'm finding, to get comfortable with how it works.
Binary Trees, at least, seem much easier to work with than regular Trees. This is probably because their property of having arity less than or equal to 2 makes their structure more predictable. We are also shown some nice examples of Binary Search Trees near the end of the week - Binary Trees whose values are all comparable and whose values run from smallest to largest as you move from the left-most node to the right-most node of the Tree. You can search for values in this kind of Tree very effectively. The way we searched for values in the BST, by judging whether each node was less than or greater than or equal to the value we were searching for, really reminded me of using binary search on sorted lists.
The second term test is next week and I'll have to get studying soon. The two main topics on the test are LinkedLists and recursion, both of which are the hardest things we've learned thus far.
Binary Trees, at least, seem much easier to work with than regular Trees. This is probably because their property of having arity less than or equal to 2 makes their structure more predictable. We are also shown some nice examples of Binary Search Trees near the end of the week - Binary Trees whose values are all comparable and whose values run from smallest to largest as you move from the left-most node to the right-most node of the Tree. You can search for values in this kind of Tree very effectively. The way we searched for values in the BST, by judging whether each node was less than or greater than or equal to the value we were searching for, really reminded me of using binary search on sorted lists.
The second term test is next week and I'll have to get studying soon. The two main topics on the test are LinkedLists and recursion, both of which are the hardest things we've learned thus far.
Wednesday, 2 March 2016
Week 7
This week I'm going to talk about recursion once more, mainly because I'm finding it so fascinating, but this time as it is used to implement an ADT - particularly a Tree. A Tree is an Abstract Data Type that allows you to store information in nodes, each of which has as many as one "parent node" and zero or more "children". The concept is very similar to how a family tree works. Take some progenitor in your family history, say a great great grandmother. Since we are beginning with her she has no "parents", she is the first level on the Tree essentially, and we call this the root. From here the tree grows. Each child of your great great grandmother's are listed next, each one of them having her as their parent node, and these children constitute level two of the Tree. The Tree continues in this same way until it gets to the lowest (or highest, depending on your perspective) level; that is, until you reach a level of children that have no children.
Well, the only difference between this image and the ADT Tree we are working with is that the ADT does not have to store "people" (great great grandmothers, aunts and grandfathers, for example) in its nodes, but each node is just a placeholder for some kind of information that the user of the ADT has deemed relevant. We can do various things with Trees, such as tell how many levels it has (what its height is), how many children it has - we can even limit how many branches one parent is allowed to send off. That is, we can say that for any node on the Tree, that node cannot have more than 2 children.
The reason I like this ADT so much is mostly because I've been interested in puzzle solving lately and I see here potential for an easy, although perhaps ultimately inefficient, solution for an algorithm that can choose the shortest path from some point A to B in a maze full of lots of dead-ends or other, though longer, paths from A to B. It shouldn't be too hard to list all the possible paths one can take beginning from point A (letting A be your root) and to organize this information in a Tree that displays each unique path. From there you could look at which paths end at position B and of those you could see which has the fewest number of nodes and that would be the quickest path from A to B through the maze. I've already finished the lab for tomorrow and will be working on the extra problems during lab hours, and hopefully I'll have some time this weekend to create a few small mazes and write such an algorithm using Trees to find the shortest path from their start to end points.
Well, the only difference between this image and the ADT Tree we are working with is that the ADT does not have to store "people" (great great grandmothers, aunts and grandfathers, for example) in its nodes, but each node is just a placeholder for some kind of information that the user of the ADT has deemed relevant. We can do various things with Trees, such as tell how many levels it has (what its height is), how many children it has - we can even limit how many branches one parent is allowed to send off. That is, we can say that for any node on the Tree, that node cannot have more than 2 children.
The reason I like this ADT so much is mostly because I've been interested in puzzle solving lately and I see here potential for an easy, although perhaps ultimately inefficient, solution for an algorithm that can choose the shortest path from some point A to B in a maze full of lots of dead-ends or other, though longer, paths from A to B. It shouldn't be too hard to list all the possible paths one can take beginning from point A (letting A be your root) and to organize this information in a Tree that displays each unique path. From there you could look at which paths end at position B and of those you could see which has the fewest number of nodes and that would be the quickest path from A to B through the maze. I've already finished the lab for tomorrow and will be working on the extra problems during lab hours, and hopefully I'll have some time this weekend to create a few small mazes and write such an algorithm using Trees to find the shortest path from their start to end points.
Wednesday, 24 February 2016
The "real" week 6
I suppose this week is actually week 6, since last week was reading week, and I guess I'll stick to the way the university counts weeks. This week in class has been exciting, we're finally getting around to recursion, something I've heard lots about and so have been anticipating. It seems like an incredibly powerful tool. The concept isn't too hard to grasp though understanding how its implemented by the computer is a little tough. After you trace through a couple nested list examples, though, it's really not too bad. I think it's a really neat, and clever, idea.
I thought at first, like many others, that it didn't make sense for a function to be able to call itself, and I was wondering how it wouldn't just get stuck in an infinite loop. The key is that the function has conditional statements within it (if, elif, else). If one condition, one "if" makes use of recursion, as long as the recursion eventually yields results that, when put back into the function as an argument, no longer satisfy this condition (let's say they don't satisfy the "if" but now they satisfy the "else") then the function will have an endpoint, it won't be infinite. And really, I guess, that's the trick to recursion. It seems to me, with my limited programming knowledge that is, that without conditional statements, or something that mimics them, recursion would break down.
It seems that whenever the method (or algorithm) you use to solve the problem as a whole is applied in exactly the same way to any of its parts then recursion is a good idea. It should allow us to take very complicated problems and break them down into a series of much easier problems once we recognize that the method for solving the problem is the same at every level. I'm really interested to see what kinds of things we can apply this idea to.
I thought at first, like many others, that it didn't make sense for a function to be able to call itself, and I was wondering how it wouldn't just get stuck in an infinite loop. The key is that the function has conditional statements within it (if, elif, else). If one condition, one "if" makes use of recursion, as long as the recursion eventually yields results that, when put back into the function as an argument, no longer satisfy this condition (let's say they don't satisfy the "if" but now they satisfy the "else") then the function will have an endpoint, it won't be infinite. And really, I guess, that's the trick to recursion. It seems to me, with my limited programming knowledge that is, that without conditional statements, or something that mimics them, recursion would break down.
It seems that whenever the method (or algorithm) you use to solve the problem as a whole is applied in exactly the same way to any of its parts then recursion is a good idea. It should allow us to take very complicated problems and break them down into a series of much easier problems once we recognize that the method for solving the problem is the same at every level. I'm really interested to see what kinds of things we can apply this idea to.
Thursday, 18 February 2016
Week 6
This week is reading week so it's a nice little break from the responsibility to drag our sleep-deprived bodies across this chilly campus in the mornings. Nevertheless, there's still lots to do in this course. Our first assignment is due in one week today and since every one of my other classes decided to take advantage of our reading week "spare time" and make something due this coming week, I've had to make sure to get my assignment nearly complete before Monday. So far so good. I know professor Danny was warning us that we will accomplish less than we set out to this week, and I know that's true, but I had nowhere to be and I was quite confident I would get this assignment done or almost done before school returns.
The assignment was pretty frustrating at first. I made the mistake of thinking that reading the assignment handout would be enough knowledge to go into it with. That is, I didn't look through each of the python files and try to read the code and see what was going on and what was expected. That cost me a lot of time because I ended up writing a bunch of functions that didn't do what was expected of them by code that implements them later on, but rather what I thought they should do. I also realized I went into the assignment with a pretty fuzzy understanding of how the simulation actually worked. I understood what we were simulating and the basics about it but I didn't really see how the computer was going to implement all of this nor did I quite understand how all of the modules would work together. I got through about 4 and a half modules and learned my lesson. Things starting breaking down in a hurry. I had messed with too much and figured I would cut my losses, devote a few hours to understanding how the program is supposed to work, draw out some pictures, run through the simulation by hand on a piece of paper and ask myself 'Okay, now how would a computer do this?'. I did that and returned to the assignment, and started from scratch since I figured my newly improved understanding could write better code now and in a quicker time than it would have taken me to fix everything step by step. I think that decision paid off. I went through the first 5 modules pretty quickly and noticed a lot of places I went wrong, and in one instance found much more elegant code to implement a function I had done before.
The assignment was also getting on my nerves because it seemed so much less focused than in CSC108. In 108 we were told step by step what we had to do. This assignment kind of just gave us a rough idea and a batch of code and told us to get started. At first I thought this was a fault on behalf of the instructors until I realized this is exactly the kind of situation I'm going to face in a workplace. No one is going to have the time to write out a detailed step-by-step instruction manual for a program we are working on. Instead, I'm going to get half-written code, docstrings, comments and a rough description of what the program is supposed to do. Just like this assignment. And I'll have to take it from there. So actually I'm quite pleased with how this assignment went. I think it was a really valuable learning experience and definitely taught me that taking a few hours before coding to sit down and really understand what needs to be done is indispensable. If I had done that from the start I would have saved myself about 3-4 hours of work. Lesson learned.
The assignment was pretty frustrating at first. I made the mistake of thinking that reading the assignment handout would be enough knowledge to go into it with. That is, I didn't look through each of the python files and try to read the code and see what was going on and what was expected. That cost me a lot of time because I ended up writing a bunch of functions that didn't do what was expected of them by code that implements them later on, but rather what I thought they should do. I also realized I went into the assignment with a pretty fuzzy understanding of how the simulation actually worked. I understood what we were simulating and the basics about it but I didn't really see how the computer was going to implement all of this nor did I quite understand how all of the modules would work together. I got through about 4 and a half modules and learned my lesson. Things starting breaking down in a hurry. I had messed with too much and figured I would cut my losses, devote a few hours to understanding how the program is supposed to work, draw out some pictures, run through the simulation by hand on a piece of paper and ask myself 'Okay, now how would a computer do this?'. I did that and returned to the assignment, and started from scratch since I figured my newly improved understanding could write better code now and in a quicker time than it would have taken me to fix everything step by step. I think that decision paid off. I went through the first 5 modules pretty quickly and noticed a lot of places I went wrong, and in one instance found much more elegant code to implement a function I had done before.
The assignment was also getting on my nerves because it seemed so much less focused than in CSC108. In 108 we were told step by step what we had to do. This assignment kind of just gave us a rough idea and a batch of code and told us to get started. At first I thought this was a fault on behalf of the instructors until I realized this is exactly the kind of situation I'm going to face in a workplace. No one is going to have the time to write out a detailed step-by-step instruction manual for a program we are working on. Instead, I'm going to get half-written code, docstrings, comments and a rough description of what the program is supposed to do. Just like this assignment. And I'll have to take it from there. So actually I'm quite pleased with how this assignment went. I think it was a really valuable learning experience and definitely taught me that taking a few hours before coding to sit down and really understand what needs to be done is indispensable. If I had done that from the start I would have saved myself about 3-4 hours of work. Lesson learned.
Wednesday, 10 February 2016
Week 5
This week pretty much came down to two things for me: try to understand what linked lists are and how they work, and prepare for the test. Well, the test is over now, and while there was nothing particularly challenging about the questions themselves, what was challenging was being able to answer them all in the amount of time given. I felt there was simply too much pressure from the short writing time to really get results that indicated how well we understood the material, but oh well, I know what to expect for next time now.
As for linked lists, this was an ADT I really enjoyed. I spent a good deal of time the other night fiddling with code and doing lab 4 in advance to get a better understanding of these things. At first I was really confused about how the nodes worked and I was under the impression that the next_ attribute of one node simply called the value of the next node, which was a bit silly. I honestly didn't think that drawing the nodes out on paper would help, I figured the image of these little boxes sitting near each other was easy enough to comprehend but I have to admit after I drew the pictures I was writing code a lot more efficiently. It's really useful to have something on a sheet of paper next to you for your eye to follow as you think about how to implement the code and where the code might run into problems.
The material is definitely starting to get tougher and more interesting!
As for linked lists, this was an ADT I really enjoyed. I spent a good deal of time the other night fiddling with code and doing lab 4 in advance to get a better understanding of these things. At first I was really confused about how the nodes worked and I was under the impression that the next_ attribute of one node simply called the value of the next node, which was a bit silly. I honestly didn't think that drawing the nodes out on paper would help, I figured the image of these little boxes sitting near each other was easy enough to comprehend but I have to admit after I drew the pictures I was writing code a lot more efficiently. It's really useful to have something on a sheet of paper next to you for your eye to follow as you think about how to implement the code and where the code might run into problems.
The material is definitely starting to get tougher and more interesting!
Wednesday, 3 February 2016
Week 4
Well, week 4 is starting to pick up. This week we've focused mainly on stacks and lists. I thought stacks were a pretty neat idea, though I admit at first I found them superfluous. I didn't really understand how they differed from or improved upon what lists could already do in Python, but professor Heap's example about balancing parentheses made a lot of sense. Before he explained it I imagined I would take care of the problem using lists. I figured I would add each parenthesis, bracket or brace to a list one after another, and then when I encountered a closing symbol I would just compare it with the most recent addition to the list, and then the next one with the second most recent addition, etc.. But I can see how that solution could get quite complicated, especially when parentheses are being opened and closed inside of another set of parentheses. The stack solution really simplified the problem a lot. I now see the immense value of stacks, though I am having a little trouble imagining what else they could be used for. Oh and we also learned about creating a list whose components are scattered throughout the memory, which I thought was a really cool idea, though I am not sure how Python would implement such a thing. Wouldn't these just be tiny lists? Wouldn't Python still try to get hold of a bunch of adjacent memory slots? I guess I will find out.
Wednesday, 27 January 2016
Week 3 in the life
In the first two weeks of CSC148 we've focused mainly on building simple classes and learning how to implement them efficiently. We're getting used to coming up with appropriately descriptive class names, how to identify class attributes and methods before we begin designing, and finally how to write a detailed class API before we begin coding. Writing good API's is an incredibly important skill for a programmer to have, even if he is not working on a large project with other programmers, having to share his own code with them. API's are useful to just a single person working on a project because they not only help organize his code and make it more readable but they serve as helpful reminders if he or she has to take a break and come back to the project in the future.
We've explored things like inheritance and composition in terms of building efficient classes; that is, in terms of writing code so that superfluous details are omitted and the program can run with as few steps as possible to accomplish its goal. One thing I really liked learning about these past two weeks was list comprehensions. Last year I continually felt that iterating through a for-loop, assigning new values to a new list from an old list, etc. in the format we originally learned was too bulky and unreadable. List comprehensions are easy to understand and much more user-friendly and really make the code feel lighter and more manageable.
I'm looking forward to what's to come!
We've explored things like inheritance and composition in terms of building efficient classes; that is, in terms of writing code so that superfluous details are omitted and the program can run with as few steps as possible to accomplish its goal. One thing I really liked learning about these past two weeks was list comprehensions. Last year I continually felt that iterating through a for-loop, assigning new values to a new list from an old list, etc. in the format we originally learned was too bulky and unreadable. List comprehensions are easy to understand and much more user-friendly and really make the code feel lighter and more manageable.
I'm looking forward to what's to come!
Subscribe to:
Posts (Atom)