Exemple #1
0
# -*- coding: utf-8 -*-

__author__ = """Chris Tabor ([email protected])"""

if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.text import gibberish
from MOAL.helpers.trials import test_speed
from MOAL.helpers.trials import run_trials
from MOAL.helpers.display import Section


dict_test = {gibberish(length=10): x for x in range(1000)}
dict_test['foo'] = 'bar'


@test_speed
def o_1():
    print('I am a O(1) function')
    print(dict_test['foo'])


@test_speed
def o_n():
    print('I am a O(n) function')
    for x in range(10, 1000):
        print(x * x)
Exemple #2
0
 def __init__(self, title, cargo=None, next=None):
     self.DEBUG = True
     self.title = title
     self.next = next
     self.cargo = cargo or gibberish(length=5)
Exemple #3
0
    def add_message(self, message, priority=0):
        return super(NetworkLogger, self).add_message(
            '[NETWORK] - ' + message, priority=priority)


if DEBUG:
    STACK_COUNT = 10

    with Section('Stacks'):
        stack = Stack()
        print('\n')
        print('First in push')
        print('\n')
        for _ in range(STACK_COUNT):
            stack.push('{} ... [ {} ]'.format(_, gibberish()))
            print(stack.head())

        print('\n')
        print(stack.size(), stack.view())
        print('\n')
        print('First out pop')
        print('\n')
        for _ in range(STACK_COUNT):
            print(stack.pop())

        print('\n')
        print('{} {}'.format(stack.size(), stack.view()))

    with Section('Loggers'):
        logger = Logger()
Exemple #4
0
 def make(self):
     return gibberish()
Exemple #5
0
 def fill_to(self, x):
     for _ in range(x):
         self.__setitem__(gibberish(length=3), gibberish(length=6))
Exemple #6
0
 def fill_to(self, x):
     for _ in range(x):
         self.__setitem__(gibberish(length=3), gibberish(length=6))
Exemple #7
0
class PrinterQueue(Queue):

    def add_job(self, name, doc):
        self.push({'name': name, 'doc': doc})
        print('Adding {} to queue for printing...'.format(name))

    def print_job(self):
        print('Printing... {}'.format(self.head()['name']))


if __name__ == '__main__':
    with Section('Queues'):
        q = Queue()
        for _ in range(5):
            print('en-queuing new item...')
            q.enqueue(gibberish())

    with Section('Double ended queue'):
        dq = Dequeue('backwards')
        for _ in range(5):
            print('en-queuing (dequeue) new item...')
            dq.enqueue(gibberish())

    with Section('Queue rotation example'):
        hps = HotPotatoSimulator(
            ['Tuvok', 'Neelix', 'Kim', 'Paris', 'Seven', 'Chakotay'], 20)

        for _ in range(7):
            hps.move()

    with Section('Printer queue example'):
Exemple #8
0
def populate_bst(bst, count=1):
    for _ in range(count):
        bst.put(randrange(1, 100), gibberish())
Exemple #9
0
            self.head.next.append(None)

    def insert(self, elem):
        # Create a new node with random height
        node = SkipNode(height=self.random_height(), elem=elem)
        print('Adding {} node to the list:'.format(elem))

        # Balance length before inserting to avoid issues with indices.
        self._balance_length(node)
        updated = self.update_list(elem)
        # Add the new element if it doesn't already exist.
        if self.find(elem, updated=updated) is None:
            for i, _ in enumerate(node.next):
                node.next[i] = updated[i].next[i]
                updated[i].next[i] = node


if __name__ == '__main__':
    with Section('Skip Lists'):
        gibs = [{
            'name': gibberish(), 'value': rr(10, 1000)
        } for _ in range(10)]
        sl = SkipList()
        for gib in gibs:
            sl.insert(elem=gib)

        for gib in gibs:
            sl.find(gib)

        sl.view()
Exemple #10
0
    def insert(self, elem):
        # Create a new node with random height
        node = SkipNode(height=self.random_height(), elem=elem)
        print('Adding {} node to the list:'.format(elem))

        # Balance length before inserting to avoid issues with indices.
        self._balance_length(node)
        updated = self.update_list(elem)
        # Add the new element if it doesn't already exist.
        if self.find(elem, updated=updated) is None:
            for i, _ in enumerate(node.next):
                node.next[i] = updated[i].next[i]
                updated[i].next[i] = node


if __name__ == '__main__':
    with Section('Skip Lists'):
        gibs = [{
            'name': gibberish(),
            'value': rr(10, 1000)
        } for _ in range(10)]
        sl = SkipList()
        for gib in gibs:
            sl.insert(elem=gib)

        for gib in gibs:
            sl.find(gib)

        sl.view()