Saturday, April 4, 2015

Week 11. Revisiting previous post

    I look back at week 2, and realized how significantly my views changed since then. For example, now I see docstrings as something that is there to help put code together, and fix it if it doesn't work properly. When you have a human language representation in front of you, it is easier to write actual code based on it rather than keeping everything in your head. This becomes even more important in group work. You don't want to spend hours explaining your teammates what each helper function does. It can be easily solved by them simply reading docstring, and getting familiar with how function/method behaves, instead of trying to understand each line of code written by somebody else which can be tedious and sometimes absolutely unnecessary.
    Even a bigger change happened in how I view recursion now. When it gets easier to understand and implement, you can see its full glory and power. Especially after covering efficiency, you can see how sometimes linear approach takes unbelievably more time (aka steps or iterations.) For example, implementing dequeue using recursion, decreased run-time for dequeing 1000 entries from a 1000000 items long queue(which now was replaced by linked list) from 12 seconds, to hundredths of a second. Can't even imagine difference for bigger queues.

Sunday, March 29, 2015

Week 12, optimization, big O

    Last week we have seen how different algorithms which do the same thing in the end, can differ a lot by amount of steps it takes to run. We measure it in steps and not in seconds, or something else, since time really varies depending on hardware, background applications consuming resources, etc. When speaking about optimization, we mainly care about the leading factor of the equation(for example: if it takes n^2 +7n+15 iterations to run the algorithm, where n is, say, length of a list, then we say it is of the order n^2). We care only about the highest power, because as n gets bigger, it is mostly affected by the biggest power. In the previous example, if n = 100, n^2+7n+15 = 10000+700+15. Already we can see that 7n and especially a constant 15, are not as important as n^2. And a list of length 100 is a rather short one. Imagine how big the difference between n^2 and 7n is going to be when we get to millions of entries? That is the main reason why we neglect other parts of the equation but the highest degree term. In addition to that, if the highest degree is for example: 6n^3, we still are going to ignore 6 in front of n, for the same reasons.
   Here is a list of the shortest time-run to the longest one in ascending order:
1) constants
2)log
3)square roots
4)n
5)n*logn
6)n^2
7)n^3
8)2^n
9)n^n
    Obviously when choosing an algorithm from two which do the job, you are going to pick the "faster" one.
    Big O notation is just a way to write the order of the run-time. So order of the first example is
 n^2 which can be written as: O(n^2)(that is not the exact symbol for big O, there is a little tiny disconnection in the correct one, but you get the idea.)

Tuesday, March 24, 2015

Week 9, Linked lists

    In my eyes linked lists are a special case of recursion which uses embedded reference. Mainly because each node in the list references to another object (which explains why it uses embedded reference), and since almost any node (except for the last one, called "back" of a linked list, which refers to NoneType) refers to another linked list. If this is not recursive, then I don't know what is. Obviously having empty nodes which just refer to another linked list again consisting of empty nodes is not very useful, so nodes contain data, called "cargo", which you can access when you "walk the linked list".
    But before that I want to define what are "head" and "tail". Head is the node in the linked list, and the tail is linked list which the head refers to. 
    Back to "walking". It means to access the "head" of each linked list, do something with it's cargo (or don't do anything until you find a specific data value you are looking for) and maybe with the node itself (like change it's reference), and AFTER MAKING SURE YOU ARE DONE WITH THE NODE go to the head of the linked list, previous node refers to. It is important, because one does not simply access a node after starting working with the next one. Getting back to it would require you to start walking from the beginning of the list, and would be super inefficient.
    LLists are useful for insertions and deletions, since they don't require changing all the values after the inserted one. For example if we had a list of 10 items, and wanted to insert one more item called Z between items 1 and 2, we had to move all of 2, 3, 4, 5, 6, 7, 8, 9, 10 by one, because of memory addresses they are at. However with linked list, we can just refer 1 to Z, and Z to 2, and everything else can stay unchanged.
    On the other side there are 2 drawbacks of using them:
        1) is time and amount of work it would take to access a value at node 10, because we would have to walk along all the values from 1 to 9 inclusive.
        2) It's easy to make mistakes, like the one described in caps-lock in paragraph 3.

Saturday, March 7, 2015

Week 8, impressions

    Probably one of the biggest impressions of last week is assignment 2. Especially those cases when you were starring at your code and editing it along with Googling for hours trying to figure out why it doesn't work, and then making the method work by adding two lines to another method, which you would never think affects work of the not working method. For example method 1 didn't work properly because it called method 2, and method 2 called method 3, and even though method 3 works on its own, and method 2 works absolutely fine without being called by method 1, they don't work together.
    And then comes minimax. Works with subtract square, raises an error with tippy. What do you find out after starring at tippy for long enough? Method 3 from previous example, which you thought you fixed, doesn't work!!! When you finally make it not raise an error, it turns out minimax chooses the worst strategy instead of the best. Who knows why. Weird and scary things happen in programming sometimes.

Week 7, recursion

    In past three weeks we studied more applications and examples of where recursion is used. We learned about trees, and linked lists.
    Trees are a special case of recursion which have leaves which correspond to recursion's base case, and nodes which  have children, which involve recursive code. Pretty much they are a set of nodes() with directed edges connecting pairs of nodes. First node of the tree is called root, and nodes which don't lead to any other nodes are called leaves. All the other nodes (node which is not root and not a leaf) are called internal nodes.  Each parent node can have a certain amount of children, but not more than the value of branching factor is(branching factor is the maximum amount of children a parent node can has), but each child node can have only one parent node(which would be sad if they were people).
    In a way linked lists can be seen as trees, but each parent node can has exactly one child, excluding the leaf(which here is called back) which references to None and has no children. Root is called front in a linked list. We should take great care when using them, and the main reason for that is: while "walking a linked list" you can not go back to a node if you already finished working with it, so you should be sure you won't need this node before you go to the next one. There obviously is a way to get back to it, but it would require starting walking from the beginning of the list and would be unfeasible. In addition there are some cases when you needed to change the back, and declare a new back, but since the old back is not the back anymore, you might not be able to access it in the way you would like to. Linked lists is a powerful, but dangerous tool.

Sunday, February 15, 2015

OOP

    At first I thought we are supposed to write about OOP after the reading eek, cause it didn't look like something we have already covered. Then I realized it is due before reading week. Looked it up and found out this is pretty much what we were doing throughout this whole course.
    Will start with Class first, because it's kind of hard to explain other concepts without it. Class allows us to create an object with certain set  of attributes, which are of the same(or very simmilar) types for all objects of one class.
    Inheritance lets us create subclasses (which are based on bigger classes called superclasses). They are needed in order to make code more flexible and easier to edit in the future without applying the same changes again and again for every little class we need. Instead we can just change superclass, and all the generic attributes inherited from it by subclasses will be changed as well. For example creating a superclass Shape which has one attribute which all shapes are going to use, regardless of what the actual shapes are, their sizes, colors etc. That is position. And then create subclasses of shape for example Square or Triangle which will have different amount of sides, hence won't be able to inherit this attribute from parent class.
    Methods do the same thing as functions do, except for they are attached to classes while functions are written in the module. We can use methods to create and change(aka mutate) characteristics of an object. If we take class Square from previous example, we would might want to write a method change_base which would change the length of base of square already created. There are 4 methods which have to be included in most classes: __init__,  __str__,  __repr__,  __eq__.
   __init__ allows you to create an instance of an object
    __str__ gives a string representation of an object description understandable for user
    __repr__ provides information on an instance of a class which is precise enough to create an exact copy of an object
    __eq__  lets us check if two objects are the same, based on attributes we want to check rather than just checking where the object is stored in memory.

Sunday, February 8, 2015

Week 5. Tracing recursion impressions

    Tracing recursion calls is a task which requires a lot of concentration and can be tedious at first, but with some practice it becomes a lot more understandable and easier. First thing I thought was "why does this keep happening? There is no loop! Whyyyy?" And then "How does it know when to stop?" and then "I am dumb.". Obviously after couple of traces and attempts to write some recursive functions it became clear that a loop is absolutely unnecessary, since these functions call themselves, and have an if statement in them, so they kind of already are loops on their own.
    Knowing about this concept would have saved some time and effort in previous assignments in 108 and school, but it's understandable why nobody teaches it right away. It is hard to understand if you are not familiar and are able to use other concepts. Even now it is a bit hard, even though quick sort demonstration during the class helped with understanding of actually writing recursive functions a bit.
    Lab 5 handout looks like we are going to write some recursion functions, which is a bit scary, since I still didn't manage to make most of the ones I was practicing on work properly, but isn't it why we all are doing this? For that feeling in the end when you manage to make things work?