This week, I've decided to revisit recursion!
Looking at other people's blogs, such as this one and this one, they, based on their week 11 slog update, just like me, also seemed to have the most trouble with Linked Lists and recursion.
Good news is the midterm was okay for me!!
So though I can't imagine how... (unwarranted Defying Gravity reference. Sorry.)
My attempt at understanding recursion and Linked Lists were actually correct!
Hooray!!
Exams are fast approaching and the good news is that the strike is over so there will be more hours available for help!
Time to practice, practice, practice!
Practice recursion!
Practice LinkedLists!
Monday, March 30, 2015
Sunday, March 22, 2015
Week 10
There were midterms last week.
It will always look at the value given under the if __name__ == '__main__': statement.
i.e., since in the function h(1), the value of N would be 1, not 5, as the first line says. M, however, will keep it's value as 2.
Then looking at h(1), N == 1.
So f(g(N)) first looks at g(N), which would return 2 * 1, which gives 2.
So next, it looks at f(2), which gives 2 * g(2 + 1). g(3) returns a value of 6.
Therefore, f(2) returns a value of 12.
f(g(N)) returns a value of 12, which is what h(1) evaluates to.
h(1) would return 12.
p.s. Strike still not over.
Good news is, sample solutions for the labs were posted, so the confusion I had about lab 7 regarding implementing the functions, solved.
That's probably a problem.
I have difficulty implementing functions myself, but when I see sample code, I understand it.
In other words, I can't come up with some of the code myself.
I was freaking out for the most part of week 9 because of that reason.
However, I think the midterm went alright for the most part.
Unfortunately, I totally have trouble reading the instructions.
The very first question was asking about a Binary Search Tree but I completely missed that and just drew a regular Tree. A tree that wasn't binary.
So lost a couple of marks there.
But I absolutely know what I did wrong there.
Then there was a question on LinkedLists and I wasn't very sure how to implement it either.
I don't remember the question clearly, but I remember struggling to decide whether to look at the size of the list, or whether to use a while loop.
I don't remember the question clearly, but I remember struggling to decide whether to look at the size of the list, or whether to use a while loop.
Again, hopefully, sample solutions to the test will be posted soon so that I can see what's going on.
Will elaborate on Term Test #2 after sample solutions are posted.
This week in lectures, we talked about which value python takes when given something like:
This week in lectures, we talked about which value python takes when given something like:
It will always look at the value given under the if __name__ == '__main__': statement.
i.e., since in the function h(1), the value of N would be 1, not 5, as the first line says. M, however, will keep it's value as 2.
Then looking at h(1), N == 1.
So f(g(N)) first looks at g(N), which would return 2 * 1, which gives 2.
So next, it looks at f(2), which gives 2 * g(2 + 1). g(3) returns a value of 6.
Therefore, f(2) returns a value of 12.
f(g(N)) returns a value of 12, which is what h(1) evaluates to.
h(1) would return 12.
p.s. Strike still not over.
No help on labs!! Eeeeeeek.
Monday, March 16, 2015
Week 9
Assignment 2 was due last week!
I find that, after these two assignments, comparing to the assignments I did in CSC108 last semester, the code is so much longer.
I have this problem where I feel like I'm doing something wrong after I've written about 10 lines of code and the function still doesn't work.
Which function proved me very wrong?
the function winner for the game Tippy.
That code for winner was so so so so so very long.
It was also very redundant. We had to check all around the squares of the most recent move, and the code was all very similar, just changing different directions each time.
What resulted was just lines and lines of basically the same code.
But speaking of labs, I was initially thrown off by the amount of code we had to write, because we had to change to what the back and front pointed to each time.
Later, I figured that we just always have to remember about the back and front of the LinkedList.
Every time we add a value somewhere in the list, we have to manually change the value that the back or front of the LinkedList points to.
Actually, some of the functions were kind of complicated, and there were no labs because of the strike, so I didn't get as much help with the labs as I usually do...
So... I shall just muddle through in my somewhat confused state for now...
I find that, after these two assignments, comparing to the assignments I did in CSC108 last semester, the code is so much longer.
I have this problem where I feel like I'm doing something wrong after I've written about 10 lines of code and the function still doesn't work.
Which function proved me very wrong?
the function winner for the game Tippy.
That code for winner was so so so so so very long.
It was also very redundant. We had to check all around the squares of the most recent move, and the code was all very similar, just changing different directions each time.
What resulted was just lines and lines of basically the same code.
But speaking of labs, I was initially thrown off by the amount of code we had to write, because we had to change to what the back and front pointed to each time.
Later, I figured that we just always have to remember about the back and front of the LinkedList.
Every time we add a value somewhere in the list, we have to manually change the value that the back or front of the LinkedList points to.
Actually, some of the functions were kind of complicated, and there were no labs because of the strike, so I didn't get as much help with the labs as I usually do...
So... I shall just muddle through in my somewhat confused state for now...
Sunday, March 8, 2015
Week 8: Impressions of Week 7
Week 7, we continued studying Trees, specifically, binary trees.
In this week's lab, we looked at evaluating a simple math statement, such as 3 + 4.
The node would be the operation, i.e., '+', and its children would either be numbers, or another operation.
That means that we would have to pay attention to the order of operations by placing parentheses around the correct evaluation statements.
The first function that we had to write, parenthesize, was not difficult.
All we had to do was access the value stored in each node, and place parenthesize before and after the return statement.
However, I had trouble with the lab after that function.
For example, the next function, list_longest_path, I managed to get down the base cases, which is
1. if the root node is None, return [].
2. I knew I had to check the length of the left nodes and compare it to the right nodes, but had no idea how to implement. The TA told me my base case was correct, and whatever I had for the following elif statement containing the recursive statement was missing something to compare the length of the list of the left and right nodes.
Actually, I had trouble implementing the functions after parenthesize...
I felt like I was asking the TA way too many questions and I still didn't know how to do it. I didn't want him to get too frustrated with me...
Hopefully there will be sample solutions soon!
In this week's lab, we looked at evaluating a simple math statement, such as 3 + 4.
The node would be the operation, i.e., '+', and its children would either be numbers, or another operation.
That means that we would have to pay attention to the order of operations by placing parentheses around the correct evaluation statements.
The first function that we had to write, parenthesize, was not difficult.
All we had to do was access the value stored in each node, and place parenthesize before and after the return statement.
However, I had trouble with the lab after that function.
For example, the next function, list_longest_path, I managed to get down the base cases, which is
1. if the root node is None, return [].
2. I knew I had to check the length of the left nodes and compare it to the right nodes, but had no idea how to implement. The TA told me my base case was correct, and whatever I had for the following elif statement containing the recursive statement was missing something to compare the length of the list of the left and right nodes.
Actually, I had trouble implementing the functions after parenthesize...
I felt like I was asking the TA way too many questions and I still didn't know how to do it. I didn't want him to get too frustrated with me...
Hopefully there will be sample solutions soon!
Monday, March 2, 2015
Week 7: ADT + Recursion
On my last blog post, I already discussed object-oriented programming.
This week, I will blog about abstract data types (which will also include recursion!).
So far, we have learned about Trees, and how to access the information located at the children of the parent of the tree.
If we have to access the information from the children numerous times, we would have to write a recursive method, which is, of course, down my weakest alley so far yet again.
For example, last week in class, we looked at the bst_contains and evaluate methods.
bst_contains looks at a sorted tree, and sees if there is a specific value within that tree. If given a value at the node, all the numbers that are less than the value are sorted to the left and all the numbers greater than the value at the node will be sorted to the right. That is where the code should be looking.
It uses recursion to find the value in the tree.
For example, if the value at the parent node is 5, and we are looking for the number 9, since 9 > 5, the code should go to the right of the node and call on bst_contains again. This means that the base case should have a statement that evaluates to false, and another to true.
One base case we have is if the node is None (there are no children), then the statement will return false.
If the value is greater than the parent node, we will look at node.right (right of the parent), and keep calling on bst_contains until we either reach or will not reach the value we are trying to find. If the value is not in the tree, it will return false (due to the statement we wrote above in the last paragraph), but if the value is finally reached, we will have an else statement that says that if the value == node.data (the value we are looking for), then it will return True.
Next, we wrote an evaluate method. This also looks at the abstract data type Tree.
The values contained at the nodes this time are not just values, but actually evaluation symbols as well, such as +, -, *, and /.
Look at
def evaluate(b):
the base case is that if b has no children, then we just return b:
if b.left is None and b.right is None:
return b
but if that is not the case, we have to format the information stored at the nodes in a way such that python can evaluate it.
For example, if the parent node was '*' and the children nodes were 3.0 and 4.0, we want python to evaluate 3.0 * 4.0.
So in the else statement (if the parent node is not a leaf):
else:
return eval(str(evaluate(b.left)) + b.data + str(evaluate(b.right))
Wowowwowowowwow so many nested methods.
Previously, I said I wanted a statement that would say 3.0 * 4.0.
However, if the children nodes were not values, but yet again another one of +, -, *, and /, we will have to use recursion and keep adding to the evaluation statement.
We have to call evaluate again on the left and right nodes because we have to keep finding the children nodes until we reach a leaf, which is when the code will evaluate the base case if statement.
Then we have to turn the statement into str because b.data is also a string, and we use string concatenation to form, for example, '3.0 * 4.0'.
eval will evaluate the string to return 12.0.
That's abstract data types and recursion for you.
This week, I will blog about abstract data types (which will also include recursion!).
So far, we have learned about Trees, and how to access the information located at the children of the parent of the tree.
If we have to access the information from the children numerous times, we would have to write a recursive method, which is, of course, down my weakest alley so far yet again.
For example, last week in class, we looked at the bst_contains and evaluate methods.
bst_contains looks at a sorted tree, and sees if there is a specific value within that tree. If given a value at the node, all the numbers that are less than the value are sorted to the left and all the numbers greater than the value at the node will be sorted to the right. That is where the code should be looking.
It uses recursion to find the value in the tree.
For example, if the value at the parent node is 5, and we are looking for the number 9, since 9 > 5, the code should go to the right of the node and call on bst_contains again. This means that the base case should have a statement that evaluates to false, and another to true.
One base case we have is if the node is None (there are no children), then the statement will return false.
If the value is greater than the parent node, we will look at node.right (right of the parent), and keep calling on bst_contains until we either reach or will not reach the value we are trying to find. If the value is not in the tree, it will return false (due to the statement we wrote above in the last paragraph), but if the value is finally reached, we will have an else statement that says that if the value == node.data (the value we are looking for), then it will return True.
Next, we wrote an evaluate method. This also looks at the abstract data type Tree.
The values contained at the nodes this time are not just values, but actually evaluation symbols as well, such as +, -, *, and /.
Look at
def evaluate(b):
the base case is that if b has no children, then we just return b:
if b.left is None and b.right is None:
return b
but if that is not the case, we have to format the information stored at the nodes in a way such that python can evaluate it.
For example, if the parent node was '*' and the children nodes were 3.0 and 4.0, we want python to evaluate 3.0 * 4.0.
So in the else statement (if the parent node is not a leaf):
else:
return eval(str(evaluate(b.left)) + b.data + str(evaluate(b.right))
Wowowwowowowwow so many nested methods.
Previously, I said I wanted a statement that would say 3.0 * 4.0.
However, if the children nodes were not values, but yet again another one of +, -, *, and /, we will have to use recursion and keep adding to the evaluation statement.
We have to call evaluate again on the left and right nodes because we have to keep finding the children nodes until we reach a leaf, which is when the code will evaluate the base case if statement.
Then we have to turn the statement into str because b.data is also a string, and we use string concatenation to form, for example, '3.0 * 4.0'.
eval will evaluate the string to return 12.0.
That's abstract data types and recursion for you.
Subscribe to:
Posts (Atom)
