Ejemplo n.º 1
0
 def add(self, item):
     """adds a new data to the head of the list """
     new_node = Node(item)  # create node with the data
     if self.is_empty():
         self.head = new_node
         self.tail = new_node
     else:
         new_node.set_next(self.head)  # point its next to the current head
         self.head = new_node  # set the node as the head
Ejemplo n.º 2
0
 def statement(self, p):
     if Debug: print("print")
     self.line_start = p.index
     if hasattr(p, 'expr'):
         return Node(('print'), [p[2]])
     else:
         return Node(
             ('error',
              "There was a print error at line {}, char {}. Token {}({})".
              format(p.error.lineno, p.error.index - self.line_start,
                     p.error.type, p.error.value)))
Ejemplo n.º 3
0
 def add(self, value):
     # add to the queue
     n = Node(value)
     if self.head is None:
         self.head = n
     else:
         self.tail.append(n)
     self.tail = n
    def test_is_ready(self):
        node = Node()

        self.assertFalse(node.is_ready(), 'ready too soon')

        sleep(1)

        self.assertTrue(node.is_ready(), 'not ready yet')
Ejemplo n.º 5
0
 def append(self, value):
     node = Node(value)
     if self.head == None:
         self.head = node
         self.tail = node
     else:
         self.tail.next = node
         self.tail = node
    def test_is_ready_after_add_child_and_remove_child(self):
        a = Node()

        sleep(1)

        self.assertTrue(a.is_ready(), 'a should be ready')

        b = Node()
        a.add_child(b)

        self.assertFalse(a.is_ready(), 'b should not be ready')
        self.assertFalse(b.is_ready(),
                         'a should not be ready (due to b addition)')

        a.remove_child(b)

        self.assertTrue(a.is_ready(),
                        'a should be ready again (due to b removal)')
    def add(self, item):
        """adds the new data to the appropriate position
        based on its value
        """
        new_node = Node(item)  # create node with the data
        if self.is_empty():
            self.head = new_node
            self.tail = new_node
        else:
            current = self.head
            previous = None
            stop = False
            while current is not None and not stop:
                if current.get_data() > item:
                    stop = True
                else:
                    previous = current
                    current = current.get_next()

            if previous is None:
                new_node.set_next(self.head)
                self.head = new_node
            else:
                new_node.set_next(current)
                previous.set_next(new_node)
Ejemplo n.º 8
0
    def insert(self, pos, item):
        """ Inserts item at position pos
        Assumes pos is a valid index
        """
        # if pos is zero add to head
        if pos == 0:
            self.add(item)
        else:
            previous = None
            current = self.head
            current_idx = 0
            index_reached = False
            while not index_reached:
                previous = current
                current = current.get_next()
                current_idx += 1
                if current_idx == pos:
                    index_reached = True

            new_node = Node(item)
            new_node.set_next(current)
            previous.set_next(new_node)
Ejemplo n.º 9
0
def bucket_sort(wart):
    n = count(wart.next)
    buckets = [Node(None) for _ in range(n)]

    while wart.next is not None:
        p = wart.next
        wart.next = p.next

        bucket_i = p.value * n // 10

        insert_into_bucket(buckets[bucket_i], p)

    for b in reversed(buckets):
        while b.next is not None:
            p = b.next
            b.next = p.next
            p.next = wart.next
            wart.next = p
    def test_is_ready_with_complex_hierarchy(self):
        a = Node()
        b = Node()
        a.add_child(b)

        c = Node()
        a.add_child(c)

        d = Node()
        a.add_child(d)

        e = Node()
        d.add_child(e)

        self.assertFalse(a.is_ready(), 'a should not be ready')
        self.assertFalse(b.is_ready(), 'b should not be ready')
        self.assertFalse(c.is_ready(), 'c should not be ready')
        self.assertFalse(d.is_ready(), 'd should not be ready')
        self.assertFalse(e.is_ready(), 'e should not be ready')

        sleep(1)

        self.assertTrue(a.is_ready(), 'a should be ready')
        self.assertTrue(b.is_ready(), 'b should be ready')
        self.assertTrue(c.is_ready(), 'c should be ready')
        self.assertTrue(d.is_ready(), 'd should be ready')
        self.assertTrue(e.is_ready(), 'e should be ready')

        f = Node()
        d.add_child(f)

        self.assertFalse(f.is_ready(), 'f should not be ready')
        self.assertFalse(d.is_ready(), 'd should not be ready (direct)')
        self.assertFalse(a.is_ready(), 'a should not be ready (indirect)')

        sleep(1)

        self.assertTrue(f.is_ready(), 'f should be ready')
        self.assertTrue(d.is_ready(), 'd should be ready (direct)')
        self.assertTrue(a.is_ready(), 'a should be ready (indirect)')
Ejemplo n.º 11
0
 def append(self, item):
     """appends item to the end of the node"""
     new_node = Node(item)
     self.tail.set_next(new_node)
     self.tail = new_node
Ejemplo n.º 12
0
    node.next = q
    p.next = node


def bucket_sort(wart):
    n = count(wart.next)
    buckets = [Node(None) for _ in range(n)]

    while wart.next is not None:
        p = wart.next
        wart.next = p.next

        bucket_i = p.value * n // 10

        insert_into_bucket(buckets[bucket_i], p)

    for b in reversed(buckets):
        while b.next is not None:
            p = b.next
            b.next = p.next
            p.next = wart.next
            wart.next = p


if __name__ == "__main__":
    t = [randint(0, 9) for _ in range(50)]
    print(t)
    l = Node(None, tab2list(t))
    bucket_sort(l)
    print(l.next)
Ejemplo n.º 13
0
 def expr(self, p):
     return Node('uminus', [p.expr])
Ejemplo n.º 14
0
 def expr(self, p):
     if Debug: print("NUMBER", p.NUMBER)
     return Node(('number', p.NUMBER))
Ejemplo n.º 15
0
 def expr(self, p):
     if Debug: print("expr", p[1], "expr")
     return Node(p[1], [p.expr0, p.expr1])
Ejemplo n.º 16
0
 def expr(self, p):
     if Debug: print("STRING", p.STRING)
     return Node(('string', p.STRING))
Ejemplo n.º 17
0
 def expr(self, p):
     if Debug: print("ID", p.ID)
     return Node(('id', p.ID))
Ejemplo n.º 18
0
 def expr(self, p):
     if Debug: print("BOOL", p.BOOL)
     return Node(('bool', p.BOOL))