Sunday, 5 April 2015

CSC148: how the course made me "think differently".

CSC148: the course and the challenges:

  • After the introduction to python (CSC108) course was the big challenge CSC148. Sure for many students in the class it was nothing, they already knew how to do all the programming stuff. But do realize, some students were very new to programming.
  • It is very hard for a student who is not used to programming at all, learn a programming language and do well in it. 
  • CSC148 had many challenges, but these challenges were on the good side. Meaning the assignments and the labs allowed the student not just to do programming but also think like a computer scientist. 
How the challenges made my thinking better:
  • a programmer can make a program, a computer scientist can make a program that is efficient, powerful, and fast.
  • The assignments in this course had changed the way I think. It allowed me to first tackle the problem without using the computer first (drawing diagrams, writing out algorithms on the paper). This allowed me to think of the exact outcome of my algorithm before I even run the program. 
  • Solving problems with out using the computer really allows for the brain to think faster and think of the most efficient way. 
  • Of course not all the time the outcome is the way I wanted to be. But writing the algorithm, drawing diagrams helps to locate the problem fast and fix them.
Applying the thinking in areas other than programming:
  • Learning to think has allowed me to do understand concepts fast in other courses. 
  • sounds great because i'm doing pretty well in other harder courses, such as math. which sounds great because i can get my studies done faster. (but yeah people do annoy me for help). 
* Now that the course is done, I know how to think like a computer scientist, i will be trying to make programs my self in python. see if i can program a game which has a smart AI (which i learned to make in this course). 
* this course was fun and helpful. Thank you Diane Horton and Danny Heap for teaching me how to think like a computer scientist. 

Sunday, 29 March 2015

Comment on earlier SLOGS

why slogs?

  • As my first slog/blog said that geeks should write because it can help them think and learn from their mistakes. 
  • so in this slog i'll say the things i learned from writing my own slogs.
Comment on my slogs:
  • There may be some mistakes that i may have made during writing the slogs, but i would say i agree with the ideas that i wrote earlier.
  • ideas include 1. drawing diagrams when writing code:
    • Drawing diagrams help a lot, even the most professional programmers use diagrams to develop complex software. 
    • The diagrams help understand the outcome of the algorithm that is being written, and then write the algorithm. This makes writing any code much much simpler.
  • 2. testing your code frequently. 
    • Testing your code overtime your write a function or a complex code helps you kill the errors that are in the code.
    • Testing with one or four values won't help, test your code with the values that are near its limit or test it with values out of the limit. This way you know the boundaries to your function/code and that can help you kill errors that may occur.





  • 3.  recursive codes:
    • the idea for recursive code was that to think of the simplest input/output of the code first then write the complex recursive code.
    • another advice for recursive code is to understand what the outcome is. and to do that you must draw diagrams. this will allow you to think of the problem and solve it quickly.
    • another advice for writing recursive code is to first think of why you need the recursion, and what would the recursion do on the given input. This will help you draw the diagram and resolve your problem.
As said above, when you're writing your code, DRAW DIAGRAMS. This will help you understand the code and the problem. Test your code frequently in order to find your mistakes and fix them. Its all about trail and error. 

Monday, 23 March 2015

Testing your code!

When you write any code, simple or complex, it is important to test it for many cases so you, as a computer scientist, have a sense of if you code works with best/worst cases. If you don't do this you will have many problems when releasing your code to the public or submitting your code as an assignment for a class.

Unittests in python:

  • Python has a great built in framework called Unitest, which is used to test cases for your code. 
    Implementing Unittest:
  • in order to implement a unittest, create a class for your tests (name it some ting that start with "Test". this class is a subclass of unitest.Testcase, so make sure you implement that.
  • the next two methods are optional to include in the class:
  • one is setup(self), this method is called to prepare the test case. it is called at the start the test. in this method you are to initialize any variables to be used in the test cases.
  • the other method is teardown(). called after each test, but only be called if the setup() succeeds. this method will raise an error rather than test failure. thus you will have an exact knowledge of where your code has errors. 
Implementing errors:
  • inorder to test your code, you must begin with writing a test method in your unittest class. Every test method must begin with the word "test" in order for the unitttest framework to test the code.
  • in the method you may test your code, and if error use this implementation:
  • assert <<condition>>, <<raise error string>>.
  • the above is an assert code, where first the condition is tested, if the condition is false the raise error string will execute and tell you the error.
this will only test your code to see if your code runs with specific inputs or not. if not then you can change your code to make a better code.

Sunday, 15 March 2015

Recursion with multiple conditions.

So far if you've been reading my blog, i've discussed about recursion with one base case and one else statement that has recursive part. But lets discuss next level of recursion.

Multiple conditions for recursion:

  • Sometimes when writing a function you may come across a situation where you have to use recursion on a specific value on a specific condition, but if case 1 doesn't pass the condition then you may have to use recursion for another value.
  • take an example of this binary search tree:

  • As you can see above, 8 is the root and everything on the left of the root is smaller than 8, and everything on the right is larger. making it a Binary Search Tree (BST). 
  • BST allows for quicker search for a specific object/value anywhere in the tree. because of left being smaller and right being bigger. (much line binary search algorithm) 
Example of multiple condition recursion in BST:
  • In BST, suppose you're implementing this function:

  • this function goes through a BST tree and returns a list of nodes which are in between the start and end value.
  • in order to write the body we must first draw a diagram, in this case:
  • lets think about the base case. in this function if there is no node, then return an empty list, thus return [] if node is None else.....
  • now consider the example list_between(b, 3, 11), where it has to visit both left and right side of each node. 
  • this requires multiple if statements inside the else.
  • now lets the start value, which is 3, and look at the definition of the BST. (everything of the left is smaller than the root and everything on the right is larger). this makes our code much much simpler.
  • 3 < 8, which means if the root node is larger than start value, visit left using recursion to find values. 
  • and the end value which is 11, 11 > 8, meaning if the root node is smaller than end value, visit right using recursion. 
  • no what if the root Node is start <= node <= end? We must return that value because it is in between both start and end (or it equals start and end). 
  • therefore the we no have multiple conditions for recursion.
  • and now we can concatenate and return a list with the nodes in b/w the start and end.
Having multiple conditions for recursion makes writing the code much harder, but if diagrams are used it makes the writing the code way more easier!

Sunday, 8 March 2015

Linked Lists

What are Linked Lists?
Linked lists are made up of nodes, each node in the list refers to the next node, shown in the diagram below.

  • linked lists can be empty, which is represented by an empty node (or in python: None).
  • can be list of nodes referring to the next node. 
  • each list has a a front/head and a back/tail, where back/tail has the nest node = None.




How are Linked Lists useful?
  • linked lists are useful for making abstract data structures such as lists, queues, and stacks.
  • the advantage of using L.L is that items can be easily inserted or removed without interrupting the other data in the list.
Linked Lists in python.
  • inorder to write the code for the linked list, first make a diagram of how the linked list work. (this is the basic idea for writing any code, MAKE A DIAGRAM!). in this case we will use the one shown above.
  • first we need to store a node and a reference to next node. thus write a class with the __init__ consisting of node, and next. set the "next", by default, to None. so if there is no reference to another node, it will be, by default, None.
  • next we need to write code for front/head and back/tail (advantage of this is that we can easily find front and back and insert/remove object at that place). Implement another class and leave the parameters of __init__ empty. but inside the body set self.front and self.back to None.
  • the way appending and prepending to a linked list node is made much much simpler.
Thing(s) to learn from this:
  • To write any complicated code, make a diagram, understand what is happening in the diagram and you'll instantly know of how to write the code. (Writing the code itself may take a while).
  • when writing for/while loops (or looping through anything), make a tree of what value am i looping through and what would happen to that value if something is applied to it. 
These are a few things you can do to write a code, and it will get you thinking much more faster. 

Sunday, 1 March 2015

Recursive code and advice on how to write it.

What is Recursion?
Recursion (in computer science) is a way to solve a complex problem by breaking the problem down into smaller and smaller problem until you reach a point where its small enough and you can solve it easily.

How recursive function works:
In computer science, recursive function is a function that calls it self. as such:

def sum_list(L):
    """
    (int or arbitrarily nested list of int) -> int
    
    Return the sum of all ints in L.
    
    >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])
    36
    """
    
    if isinstance(L, list):
        return sum([sum_list(x) for x in L])
    
    else:
        return L

Here is a problem (which i learned in class). Before looking at the if statement look at the else statement. the else statement returns the most simplest value. As explained above in the definition, recursive code breaks down the problem into smallest possible problem where the problem can easily be solved. 
In this case, if the smallest problem is not a list then return whatever the value for L is.
Otherwise, if L is a list then break the list down into smaller lists until there is no list in the object L. that is where the else statement comes into play to return the simplest value. 

Understanding recursion:
  • here is how sum_list function would work in the above example:
  • >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]]) # first the function is called.
  • Then the items in the object L are identified. in this case, since the highlighted item is a list the if statement will execute the the following would happen:
  • sum([sum_list(1) for x in L]) # since the for-loop iterates through each item, the first item that goes is 1. Now we know that if the item is not a list, it would just return the item so the sum() would look like this:
  • sum([1, sum_list([2,3]), sum_list([4, 5, [6, 7], 8]])
  • sum ([1, 2, 3, 4, 5, 6, 7, 8]) # all the items would be returned from the recursion.
  • 36 #value 36 is returned from the sum() method.
How to write recursive code?
  1. Figure out what the simplest value should be returned. This is the easiest part, figure out the part of the problem that is the easiest to solve and write an if statement about that, leave the else statement for now.
  2. Figure out some simple examples that would need to use recursion the break down the problem into smaller pars.
  3. Now write an else statement to breakdown the problem to get to the simplest problem and feed it to the if statement written in step 1.
  • The hardest part is to write the recursive else statement, because sometime the problem is complex enough such that the else statement may need inner if/else statements for recursion to take place.
Recursive code allows for complex problems to be solved fast, where iterating through the problem (w/out recursion) and solving it would be complex and time consuming.
Recursion is used almost everywhere in computer science, sorting out items in a list (merge_sort()), finding a specific item in a Tree() (where Tree is a class which contains data in a tree formation) etc. This allows for problems to be solved more quickly and efficiently!

Sunday, 15 February 2015

Trees and Recursion!

Working with Tree data structures.
Trees are connected graphs with nodes representing an object and edges are lines connecting nodes. Trees are widely used for social networking or in your own computer (the file directories).
trees usually looks like this:
with the topmost node called the root node and it has children, connected by edges/branches. Children can be a node for more children or it can be a leaf node. A leaf node is a node that has no children at all. Knowing a leaf node is very helpful  for writing an algorithm for calculating the height.
In order to determine the height, you must add up the max path length + 1, a path is a sequence of nodes (n1, n2... ni).

Writing the height algorithm.
writing the algorithm may sound complicated using recursion so here are some steps:
1) determine the simplest return value. in this case would be if we have a node which is a leaf, return 1. therefore if there is no children return 1.
2)  other wise we use recursion to count up the maximum path and add 1 to it. thus the else would look like this:
else:
   return max([height(c) for c in t.children]) + 1
## this would go through the children and use them as nodes to count up the height.

This is how to write a recursive code. Its a 2-3 step process.
1) determine the simplest way to return the value and write an if statement about it.
2) figure out the complex way to return the value.
3) write an else statement which would use recursion to go through each item.
(steps 2 and 3 can be merged).

Recursive codes may get complicated you learn about them, but as long as you follow these steps, it would get easier.


Saturday, 7 February 2015

Testing your code!

In Computer science it is very important to test your code to see it works with every test cases, otherwise when your client runs your code, if it doesn't work with a certain test case, you may get in trouble!

The testing part may be time consuming, but the thing is you can learn if there is a mistake that you have made and improve your code by changing somethings about your code.

before writing a function, I plan out my code, to see what it will do  and plan out the certain arguments the function will take. This way I am sure of what test cases I have to do and I'm sure of the arguments that are invalid to this function.

when certain test cases do not pass i have to go back to the code and fix those mistakes. Finding the mistake is the easy part, improving the code is the hard part. This takes time for me, BUT i learn a lot from my mistakes.

In order to make test cases for your code, do the following:
1. Plan out your code, see what the outcome of your code will be.
2. Plan out what the code is required to do and based on that plan out some test cases.
3. Doing every single test case is very time consuming, so divide up those test cases into parts. These parts should contain smiliest test cases so you can pick the MOST important case in each of those parts and test them.
4. Once you're done with the parts write your code and test the most important test cases so you're sure of what the code can do!

Testing allows for improvement in the code, and if your code works with every input that a user can give, you have designed the perfect code (in my view)!

Monday, 2 February 2015

inheritance in classes and recursion.

Learning about Inheritance in classes in python seems difficult, thats why i needed analogy of inheritance. 
Now i like to think about inheritance as versions of games. Take any console game for example, such as call of duty of GTA 5, and so on. There have been many versions of these games. For a game like GTA, think of all the versions, what do they have in common. Getting in the car, shooting, flying vehicles. all these can be put in  General  describes the game. 
Just like that we can describe the inheritance. All the general stuff that describes the game can be updated and modified to make the newer and better version of it. Therefore computer scientist make a general class for subclasses that inherits the data or methods from the general class.

Next thing to learn about was The recursion. 
recursion basically calling the same function inside itself, to make its job easier. This is used in merge sort. merge sort basically cuts down or divides a list of elements into smaller chunks until it has only one element, then it starts the sorting process. The dividing part is where the recursion comes into play. 
merge sort algorithm basically calls on itself to keep dividing the elements until there is only one element left. so that way its easier to sort.
once you understand the recursion and inheritance class design, that is where the journey starts!!

Thursday, 22 January 2015

why geeks should write?
hmmm let me think...
...
well based on what I have read from the links the instructors provided us to read, I think the material that geeks post are very very useful for undergrads like me.

Now just to make some stuff clear not all geeks are like this guy:












so back to the topic. Geeks should write because.
1.  Based on their experience with "coding", they give very important advices.
2.  Their advices might solve one's problem.
3.  The blogs improve their writing skills or if not writing skills, the blogs might just solve their problem if they're stuck somewhere.

And thus i think geeks should write not only for their benefit but they can assist in advising and solving other's problems.

Based on what I read one of those geeks provided certain features of Linux that i didn't know about, and it lead to me doing more and more research on Linux and finally now, installing Linux on my computer. I knew about Linux being very "personalizable" BUT didn't know about no viruses and smooth running of the OS and FREEE!

Thus, geeks should write so they can inspire others to try something new. Who knows that might become someone's favourite thing. And blogs might just improve a geeks grammar or this blog might just inspire someone to read other geeks blogs!