This week was all about linked lists. This is a basic data structure that connects information together. It is very simple and only requires two components to start with: the current data and the data it wants to link to.
``````````````
class Node:
def __init__(self, data, next):
self.data = data
self.next = next
``````````````
One simply may use the algorithm to organize an entire list of data with the sorting method one would like. This is convenient for huge amount of information that are random and troublesome to look into. Also, this huge amount of info may only have little diversity, otherwise after converting into linked lists, the information given would still be random anyways. For example if there is a list of numbers, we want to link each number with the next in non-decreasing value. So the structure would look something like: 1-2-3-4-5-6....
``````````````
1. look into this number_lsit and use the method 'sort' to put in desired order.
2. set current = number_list[0]
3. set next = number_list[1]
4. then number_linked_list = Node(current, next)
this would be the basic structure, all is left is to add a for loop, that for each pair of numbers on index 0&1, 1&2 etc., this linked list adds the values after.
``````````````
Wednesday, October 30, 2013
Saturday, October 19, 2013
Week 6 of Coding
For the entire time I've been preparing for the term test that happened on Wednesday. It was relatively easier than I thought, considering how much trouble I went through working on the past tests. Things like image pixels and page long test cases which I thought were going to appear did not.
Anyhow, hard work did not go wasted. Two of the questions on the test were almost exactly the same to some of those I went through! One of them was the tedious recursions. Although I might have figured it out anyways, but without the direct solution I would've spent the entire hour working on this problem. I hope no one calls me cheating.. The other was just some simple test cases, I was surprised how it only needed descriptions. Google-ing for explanations of set-up and tear-down functions and memorizing the base format was not pleasant, I actually wanted them on the test.
sidenote: the technique to not lose yourself in a recursion problem is:
- figure out when you want to come out of this loop.(when a value is ###, when you come to the last letter of a string, or finished browsing all occurences of ***)This is the base case.
- write different possibilities that occur in this loop.(like those horoscope tests that direct you to a type of personality after you choose the choices that best fit you)
- ALWAYS TO REMEMBER:in most cases, you have a value or a string or something recorded. You must have it added repetitively when you go through each recursion, or the total result gets lost. It might only record the value from the last loop. The trick is: value += or -= recursion loop.
One interesting question I enjoyed on the test was the first one. I am sure it messed up a lot of us. I feel lucky that I went through a question on stack-overflow.(people ask about computer coding questions on here)
'..... (min, max) for i in range(2:)....'
previously I thought you could only return one single term 'for i in range..', now I know that it would do for set of any number of terms. As long as possible. This was the key to the question.
Then we just had to calculate all the numbers, substitute into the pair of terms, and compare each pair of values....hold on! When we compare a pair of values. e.g. (2,3) and (4,1). How do we tell which one is bigger! Well 2 is smaller than 4 but 3 is bigger than 1, and put it together... my own strategy was just to go with my instincts. Python is not a human-being. It would go through the first variable of each, and it is 2 and 4. So ta-da (4,1) is bigger. No matter it was right or wrong, I would do everything this way to make sure I don't waste too much time one those one-mark questions.(Now I see why our intelligent prof put this as the first question.)
I do think that I've received a decent grade, however I still don't know whereabout to check it.
I do think that I've received a decent grade, however I still don't know whereabout to check it.
Saturday, October 12, 2013
Week 1 to 5 of Coding
The blogger caught procrastination. Stayed home and hoped it would not spread to other bloggers.
Subscribe to:
Posts (Atom)