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.
Sunday, 15 February 2015
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)!
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!!
Subscribe to:
Posts (Atom)
