23 Matching Annotations
  1. Dec 2019
    1. The base case

      it is strange that the drawSpiral does not explicityly code the abse case. However, it works well when lineLen<=0, where it do draw nothing, since the code did not specify the else case, so the drawSpiral will do nothing when lineLen<=0.

    2. When the turtle’s tail is down and the turtle moves it draws a line as it moves.

      this means the tail of the turtle would leave something on the ground --- the line.

    1. Error

      this is because I want to remove a item that is not in this linked list, but the remove code here assumes that the item is in the linked list so that the code can jump out its while loop and do the remove operation.

    2. if previous == None:

      here is a problem: this while loop do not has a outlet if Found is False, that is when the item can not be found in the linked list. In this case, the remove function would stuck in the while loop and an error would occur since the when current meet the last None, the None does not has a getNext().

      this remove method assume that the item is in the linked lists, so we can jump out of the while loop when Found==True, so we can preceed the next remove precedures.

    3. temp

      temp is the address that refer to the Node(item). in the class, a instance's name is just its address that refer to the instance. if you type temp=Node(item) temp in jupyter, you would get the address of the instance temp.

    4. Figure 7: Adding a New Node is a Two-Step Process

      this is more like a nested node, where the the first part of the node stores the present data, and the second part stores the next node.

      actually, temp in Listing 4 is address of the Node(item). since we do not define the object of the Node to be a specific data type --- str, int, or float in Python, so the name of the class object only stores the address of the object. See the code below running in jupyter-qtconsole:

      class Number:

      def __init__(self, number):
          self.number=number
      
      def __str__(self):
          return str(self.number)
      

      x=Number(19)

      x Out[4]: <main.Number at 0x1098d8cf8>

      str(x) Out[5]: '19'

      print(x) 19

    5. This means that the easiest place to add the new node is right at the head, or beginning, of the list. In other words, we will make the new item the first item of the list and the existing items will need to be linked to this new first item so that they follow.

      if we add the new node at the end of the existing list, then you need to go through the whole list and set the next of the last node to refer to this new node --- O(n). However, since the list we build is unordered, so we can set the order of the list according to our own ideas. So the easiest way to add a new node is just insert this node at the start of the list, that is the head of the list --- O(1).

      in this way, the linked list stored all the items in a reversed order compared to the adding order of the items.

    6. It is very important to note that the list class itself does not contain any node objects. Instead it contains a single reference to only the first node in the linked structure.

      so how to construct the linked list? the methods of this UnorderedList.

    7. Figure 4: A Typical Representation for a Node

      since we initially created a node with next set to None, we see that the node in Fig.4 is grounding.

    1. The only choice myf has is to show the actual reference that is stored in the variable (the address itself).

      this is very important. the name of this object stores the address of this object. this info is used in chapter 4.21 to implement a linked list.

    2. tgate.setNextPin(self)

      how to understand this line? this code is to determine the connector's togate to be connected to which pin (pinA or pinB) of the next BinaryGate. But why it is written this way?

    3. Assume we have two Fraction objects, f1 and f2. f1==f2 will only be True if they are references to the same object.

      this means even f1 and f2 have the same numerator and denominator, as long as they are not refering to the same object, they are not equal. this is the shallow equality.

    4. We can override many other methods for our new Fraction class. Some of the most important of these are the basic arithmetic operations.

      we can just rewrite the default methods with our self-defined ones, just like waht we do with 'str'.

    1. Real data about the number of print tasks per hour and the number of students per hour was necessary to construct a robust simulation.

      with real data, we can do machine learning instead of simulation.

    1. Note that in this example the value of the counting constant is greater than the number of names in the list.

      if the counter is less than the number of the names in the list, the result of the algorithm seem strange. ---- this is because the simqueue is the reversed verison of the input list, so 'Bill' would be the first in the queue, that is the right side of the list implementation of the queue.

    2. Bill in this case is the first item in the list and therefore moves to the front of the queue.

      this is why the result seems strange because the items in the simqueue is the reversed version of the namelist, since the first item 'Bill' would be the first in the queue, that is the right side of the list implementation of the queue.

    1. Divide by 2

      we see that we can write a number (like 233) in 1×2^7+1×2^6+1×2^5+0×2^4+1×2^3+0×2^2+0×2^1 +1×2^0 if we devide this number by 2, we can see we get : 1×2^6+1×2^5+1×2^4+0×2^3+1×2^2+0×2^1+0×2^0 and the reminder of this devision is 1, the last binary digit.

      so if we devide the number continously, and each time the reminder would be the binary digit, with the first reminder being the last digit in the sequence.

    1. parentheses, ( and ), are used for tuples and arithmetic expressions.

      only '( )' are used for arithmetric expressions, while '{ }' and '[ ]' can't be used for arithmetric expression; these two are recognized as dict, set and list.

    1. Our final solution to the anagram problem takes advantage of the fact that any two anagrams will have the same number of a’s, the same number of b’s, the same number of c’s, and so on.

      good observation! since we only have 26 possible characters, so we will only run 26n2 at most to see the result.