Example #1
0
def add_to_back(alist, val):
    """
    Purpose
        Insert val into alist at the end of the node chain
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
    Post-conditions:
        The list increases in size.
        The new value is last in the list.
    Return:
        :return None
    """

    if size(alist) == 0:
        new_node = node.create(val, None)
        alist['head'] = new_node
        alist['tail'] = new_node
        alist['size'] += 1
    else:
        cur_last_node = alist['tail']
        new_node = node.create(val, None)
        node.set_next(cur_last_node, new_node)
        alist['tail'] = new_node
        alist['size'] += 1
    return None
Example #2
0
def add_to_front(alist, val):
    """
    Purpose
        Insert val into alist at the front of the node chain
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
    Post-conditions:
        The list increases in size.
        The new value is at index 0.
        The values previously in the list appear after the new value.
    Return:
        :return None
    """

    if size(alist) == 0:
        new_node = node.create(val, None)
        alist['head'] = new_node
        alist['tail'] = new_node
        alist['size'] += 1
    else:
        new_node = node.create(val, alist['head'])
        alist['head'] = new_node
        alist['size'] += 1
    return None
Example #3
0
def copy(node_chain, new_chain=None, head=None):
    """
    Purpose:
        Create a new node-chain
    Pre-conditions:
        :param node_chain: a node chain, possibly empty
    return:
        :return: a seperate distinct chain that has the same values as node-chain
    """

    if node_chain is None:
        if new_chain is None:
            return None
        node.set_next(new_chain, None)
        return head
    else:
        value = node.get_data(node_chain)
        if new_chain is None:
            new_chain = node.create(value, None)
            head = new_chain
            return copy(node.get_next(node_chain), new_chain, head)
        else:
            node.set_next(new_chain, node.create(value))
            return copy(node.get_next(node_chain), node.get_next(new_chain),
                        head)
Example #4
0
def copy_chain(node_chain):
    """
    Purpose:
        Make a new node chain with the same values as in node_chain.
    Pre-conditions:
        :param node_chain: a node chain, possibly None
    Return:
        :return: A copy of node chain, with new nodes, but the same data.
    """
    # special case: empty node chain
    if node_chain is None:
        return None
    else:
        walker = node_chain

        # remember the first node
        result = node.create(node.get_data(walker))
        walker = node.get_next(walker)
        walker2 = result

        # walk along the first chain, making copies of each node
        while walker is not None:
            node.set_next(walker2, node.create(node.get_data(walker)))
            walker2 = node.get_next(walker2)
            walker = node.get_next(walker)

        # return the anchor to the copy
        return result
Example #5
0
def insert_value_sorted(node_chain, number_value):
    """
    Purpose:
        Insert the given number_value into the node-chain so that it appears after a previous value that is <= value. If the node_chain was empty, new value is simply placed at the front.
    Pre-conditions:
        :param node_chain: a node-chain, possibly empty, containing only numbers
        :param number_value: a numerical value to be inserted
        Assumption:  node_chain only contains numbers (which can be compared to the given number_value)
    Post-condition:
        The node-chain is modified to include a new node with number_value as its data after a previous node's data value is <= number_value.
    Return
        :return: the node-chain with the new value in it
    """

    #if the node chain is empty, add the new node
    if a5q2.count_chain(node_chain) < 1:
        node_chain = node.create(number_value, None)

    #if chain is not empty, find the proper place
    else:
        cur_node = node_chain
        prev_node = None
        next_node = node.get_next(node_chain)

        while True:
            cur_val = node.get_data(cur_node)

            #check if the value should be placed in front of current node
            if cur_val == number_value + 1:
                #check if the target node is the first node
                if prev_node == None:
                    node_chain = node.create(number_value, cur_node)
                else:
                    new_node = node.create(number_value, cur_node)
                    node.set_next(prev_node, new_node)
                break

            #check if the value should be placed after the current node
            if cur_val == number_value - 1:
                #check if target node is the last node in chain
                if next_node == None:
                    new_node = node.create(number_value, None)
                    node.set_next(cur_node, new_node)
                else:
                    new_node = node.create(number_value, next_node)
                    node.set_next(cur_node, new_node)
                break

            #break if at the end of the node chain
            if next_node == None:
                break
            #move to next node if not at the end of the chain
            else:
                prev_node = cur_node
                cur_node = next_node
                next_node = node.get_next(next_node)

    return node_chain
Example #6
0
def copy(node_chain):
    """
    purpose: create a new node chain that is identical to node_chain
    Preconditions:
    node_chain: a node chain
    post-conditions: an identical node chain is created
    return: the newly created node chain
    """
    if node_chain == None:
        return None
    if node.get_next(node_chain) == None:  #base case, 1 node
        return node.create(node.get_data(node_chain))
    else:
        value = node.get_data(node_chain)
        return node.create(value, copy(node.get_next(node_chain)))
Example #7
0
def create(cloud_name, size, machine_name=None, key_name=None, svc=None, \
           location=None, security_group=None, network=None, data_gb=None):

    machine_id = launch(cloud_name, size, machine_name, key_name, \
      location=None, security_group=None, network=None, data_gb=None)
    if machine_id == None:
        return

    node.create(cloud_name, machine_id)

    if svc == None:
        return

    component = None
    if svc in ('pg95', 'pg96', 'pg10', 'pg11', 'pg12', 'pg13'):
        component = svc
        svc = 'postgres'

    service.install(cloud_name, machine_id, svc, component)

    return
Example #8
0
def add_to_front(alist, val):
    """
    Purpose
        Insert val into alist at the front of the node chain
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
    Post-conditions:
        The list increases in size.
        The new value is at index 0.
        The values previously in the list appear after the new value.
    Return:
        :return None
    """
    new = node.create(val)
    if alist["head"] is None:
        alist["head"] = new
        alist["tail"] = new
    else:
        alist["head"] = node.create(val, alist["head"])
    alist["size"] += 1
Example #9
0
def insert_at(node_chain, value, index):
    """
    Purpose:
        Insert the given value into the node-chain so that
        it appears at the the given index.
    Pre-conditions:
        :param node_chain: a node-chain, possibly empty
        :param value: a value to be inserted
        :param index: the index where the new value should appear
        Assumption:  0 <= index <= n
                     where n is the number of nodes in the chain
    Post-condition:
        The node-chain is modified to include a new node at the
        given index with the given value as data.
    Return
        :return: the node-chain with the new value in it
    """

    # special case: insert at index 0
    if index == 0:
        return node.create(value, node_chain)

    # walk along the chain until the indicated index
    walker = node_chain
    counter = 0
    prev = None
    while walker is not None and counter < index:
        prev = walker
        walker = node.get_next(walker)
        counter += 1

    # If there is a node at the index
    if counter == index:
        # insert a new node
        node.set_next(prev, node.create(value, walker))
    else:
        # insert at the very end
        node.set_next(prev, node.create(value, None))

    return node_chain
Example #10
0
def insert_value_sorted(node_chain, number_value):
    """
    Purpose:
        Insert the given number_value into the node-chain so that it appears after a previous value that is <= value. If the node_chain was empty, new value is simply placed at the front.
    Pre-conditions:
        :param node_chain: a node-chain, possibly empty, containing only numbers
        :param number_value: a numerical value to be inserted
        Assumption:  node_chain only contains numbers (which can be compared to the given number_value)
    Post-condition:
        The node-chain is modified to include a new node with number_value as its data after a previous node's data value is <= number_value.
    Return
        :return: the node-chain with the new value in it
    """
    if node_chain == None:
        return node.create(number_value)
#1 node
    anode = node_chain
    current = node.get_data(anode)
    if number_value <= current:  #add to front, regardless of node size
        return node.create(number_value, anode)
    if node.get_next(anode) == None:  #add after
        node.set_next(anode, node.create(number_value))
        return anode
#2 nodes
    next1 = node.get_data(node.get_next(anode))
    if node.get_next(node.get_next(anode)) == None:
        if number_value <= next1:  #add in middle
            node.set_next(anode, node.create(number_value,
                                             node.get_next(anode)))
            return anode
        if number_value > next1:
            node.set_next(node.get_next(anode), node.create(number_value))
            return anode
#3+
    next1 = node.get_data(node.get_next(anode))
    while node.get_next(
            node.get_next(anode)) != None:  #step until the second last one
        if number_value > current and number_value <= next1:  #if larger than current and smaller than next
            node.set_next(anode, node.create(number_value,
                                             node.get_next(anode)))
            return node_chain
        anode = node.get_next(anode)
        current = node.get_data(anode)
        next1 = node.get_data(node.get_next(anode))
    if number_value <= next1:  #check last 2 values
        node.set_next(anode, node.create(number_value, node.get_next(anode)))
        return node_chain
    node.set_next(node.get_next(anode), node.create(number_value))
    return node_chain
Example #11
0
def reverse_chain(node_chain):
    """
    Purpose:
        Completely reverses the order of the given node_chain.
    Pre-conditions:
        :param node_chain: a node chain, possibly empty
    Post-conditions:
        The front of the node_chain is altered to be the back, with all nodes now pointing next the opposite direction.
    Return:
        :return: The resulting node chain that has had its order reversed
    """
    if node_chain == None:
        return None
    if node.get_next == None:  # only 1 data, return node chain
        return node_chain
    anode = node_chain
    new_node = node.create(node.get_data(anode))  # end of new node
    while node.get_next(
            anode
    ) != None:  # step through original and create new node in reverse
        new_node = node.create(node.get_data(node.get_next(anode)), new_node)
        anode = node.get_next(anode)
    return new_node
Example #12
0
def push(stack, value):
    """
    Purpose
        adds the given data value to the given stack
    Pre-conditions:
        stack: a stack created by create()
        value: data to be added
    Post-condition:
        the value is added to the stack
    Return:
        (none)
    """
    new_node = node.create(value, stack['top'])
    stack['top'] = new_node
    stack['size'] += 1
Example #13
0
def insert_value_at_index(alist, val, idx):
    """
    Purpose
        Insert val into alist at index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a valid index for the list
    Post-conditions:
        The list increases in size.
        The new value is at index idx.
        The values previously in the list at idx or later appear after the new value.
    Return:
        :return If the index is valid, insert_value_at_index returns True.
        :return If the index is not valid, insert_value_at_index returns False.
    """

    #check if idx beyond alist['size'] + 1 or idx is negative, return False
    if alist['size'] - idx < 0 or idx < 0:
        return False

    #if index is the first index or is empty, add new val at the front
    if idx == 0 or is_empty(alist):
        add_to_front(alist, val)
        return True

    #if index is immediately after the last index of alist, add new val to back
    elif idx == alist['size']:
        add_to_back(alist, val)
        return True

    #general case
    else:
        cur_node = alist['head']
        next_node = node.get_next(cur_node)
        next_idx = 1  #index of next_node

        while next_node is not None:
            if next_idx == idx:
                new_node = node.create(val, next_node)
                node.set_next(cur_node, new_node)
                alist['size'] += 1
                return True
            else:
                cur_node = next_node
                next_node = node.get_next(next_node)
                next_idx += 1
Example #14
0
def copy(chain):
    """
    Purpose
        Make a completely new copy of the given node-chain.
    Preconditions:
        :param chain: a node-chain
    Post-conditions:
        None
    Return:
        :return: A new copy of the chain is returned
    """
    if chain == None:
        return None
    else:
        newnode = node.create(node.get_data(chain))
        newnext = copy(node.get_next(chain))
        node.set_next(newnode, newnext)
        return newnode
Example #15
0
def add_to_back(alist, val):
    """
    Purpose
        Insert val into alist at the end of the node chain
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
    Post-conditions:
        The list increases in size.
        The new value is last in the list.
    Return:
        :return None
    """
    anode = node.create(val)
    if is_empty(alist):
        alist['head'] = anode
    else:
        node.set_next(alist['tail'], anode)
    alist['tail'] = anode
    alist['size'] += 1
Example #16
0
def insert_value_at_index(alist, val, idx):
    """
    Purpose
        Insert val into alist at index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a valid index for the list
    Post-conditions:
        The list increases in size.
        The new value is at index idx.
        The values previously in the list at idx or later appear after the new value.
    Return:
        :return If the index is valid, insert_value_at_index returns True.
        :return If the index is not valid, insert_value_at_index returns False.
    """
    if idx > alist["size"] or idx < 0:  #if index out of range
        return False
    new = node.create(val)
    if alist["head"] is None and idx is 0:  #if empty list
        add_to_front(alist, val)
        return True
    else:
        if idx is 0:  #if add to front
            add_to_front(alist, val)
            return True
        if idx is size(alist):  #if add to rear: idx is the same as size
            add_to_back(alist, val)
            return True
    walker = node.get_next(alist["head"])
    previous = alist["head"]
    index = 1
    while True:  #add in between
        if index == idx:
            node.set_next(previous, new)
            node.set_next(new, walker)
            alist["size"] += 1
            return True
        walker = node.get_next(walker)
        previous = node.get_next(previous)
        index += 1
Example #17
0
def add_to_back(alist, val):
    """
    Purpose
        Insert val into alist at the end of the node chain
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
    Post-conditions:
        The list increases in size.
        The new value is last in the list.
    Return:
        :return None
    """
    new = node.create(val)
    if alist["tail"] is None:
        alist["head"] = new
        alist["tail"] = new
    else:
        node.set_next(alist["tail"], new)
        alist["tail"] = new
    alist["size"] += 1
Example #18
0
def enqueue(queue, value):
    """
    Purpose
        adds the given data value to the given queue
    Pre-conditions:
        queue: a queue created by create()
        value: data to be added
    Post-condition:
        the value is added to the queue
    Return:
        (none)
    """
    new_node = node.create(value, None)

    if is_empty(queue):
        queue['front'] = new_node
        queue['back'] = new_node
    else:
        prev_last_node = queue['back']
        node.set_next(prev_last_node, new_node)
        queue['back'] = new_node

    queue['size'] += 1
Example #19
0
def insert_value_at_index(alist, val, idx):
    """
    Purpose
        Insert val into alist at index idx
    Preconditions:
        :param alist: a list created by create()
        :param val:   a value of any kind
        :param idx:   a valid index for the list
    Post-conditions:
        The list increases in size.
        The new value is at index idx.
        The values previously in the list at idx or later appear after the new value.
    Return:
        :return If the index is valid, insert_value_at_index returns True.
        :return If the index is not valid, insert_value_at_index returns False.
    """
    if idx < 0 or idx > size(alist):
        return False

    if idx == 0:
        add_to_front(alist, val)
        return True

    if idx == size(alist):
        add_to_back(alist, val)
        return True

    # general case
    anode = alist['head']
    i = 1
    while anode is not None and i < idx:
        anode = node.get_next(anode)
        i += 1
    new_node = node.create(val, node.get_next(anode))
    node.set_next(anode, new_node)
    alist['size'] += 1
    return True
Example #20
0
import node as node

# create the chain of nodes
# chain-->[ 3 | *-]-->[ 2 | *-]-->[ 1 | *-]-->[ 5 | *-]-->[ 4 | / ]

chain = node.create(4, next=None)
chain = node.create(5, chain)
chain = node.create(1, chain)
chain = node.create(2, chain)
chain = node.create(3, chain)

# Exercises:
# Remove the 3 from the sequence

chain = node.get_next(chain)

# Add 6 to the front

chain = node.create(6, chain)

# add 7 to the end
anode = node.create(7)

bnode = chain
while node.get_next(bnode) != None:
    bnode = node.get_next(bnode)

node.set_next(bnode, anode)

# Extra exercises:
# Exercise 1
Example #21
0
#Course: CMPT 145-01
#Lab: L03

# CMPT 145: Assignment 5 Question 3
# test script

import a5q1 as a5q1
import a5q3 as a5q3
import node as node

test_contains_duplicates = [{
    'inputs': None,
    'outputs': False,
    'reason': 'Empty node chain'
}, {
    'inputs': node.create(1),
    'outputs': False,
    'reason': 'node chain with one node'
}, {
    'inputs':
    node.create('two', node.create('two')),
    'outputs':
    True,
    'reason':
    'node chain with two nodes that are duplicates'
}, {
    'inputs':
    node.create(1, node.create('two', node.create(1))),
    'outputs':
    True,
    'reason':
Example #22
0
#Name: Jason Tran
#NSID: jat687
#Student Number: 11101081
#Course: CMPT 145-01
#Lab: L03

import node as node
import a7q6 as a7q6

#####################################################################
# test a7q6.average()
# Unit testing

# Nodes for testing
empty_node = None
single_node = node.create(1)
several_node = node.create(0)

for i in range(1, 5):
    several_node = node.create(i, several_node)

test_average = [
    {
        'inputs': [empty_node],
        'outputs': [0],
        'reason': 'empty node'
    },
    {
        'inputs': [single_node],
        'outputs': [1],
        'reason': 'single node'
Example #23
0
    :return: None
    """
    print(counter['reason']+'\t', counter['successes'], 'of', counter['tests'], 'tests passed')


# end of the counter ADT
###############################################################################################


split_counter = createCounter('A5Q3 split()\t')
test_split_chain = [
    {'inputs' : None,
     'outputs': ["EMPTY","EMPTY"],
     'reason' : 'Empty node chain'},

    {'inputs' : node.create(1),
     'outputs': ["EMPTY", "[ 1 | / ]"],
     'reason' : 'node chain with one node'},

    {'inputs' : node.create(1, node.create('two')),
     'outputs': ["[ 1 | / ]","[ two | / ]"],
     'reason' : 'node chain with two nodes'},

    {'inputs' : node.create(1, node.create('two', node.create(3))),
     'outputs': ["[ 1 | / ]", "[ two | *-]-->[ 3 | / ]"],
     'reason' : 'node chain with three nodes'},

    {'inputs' : node.create(1, node.create('two', node.create(3,node.create('four')))),
     'outputs': ["[ 1 | *-]-->[ two | / ]","[ 3 | *-]-->[ four | / ]"],
     'reason' : 'node chain with four nodes'},
Example #24
0
    Post_conditions:
        Displays a string to the console
    Return:
        None
    """
    print(to_string(node))


test_subst = [
    {
        'inputs': [None, 3, 5],
        'outputs': None,
        'reason': 'empty chain'
    },
    {
        'inputs': [node.create('one', None), 1, 2],
        'outputs': node.create('one', None),
        'reason': 'short chain no target'
    },
    {
        'inputs': [node.create('one', None), 'one', 'two'],
        'outputs': node.create('two', None),
        'reason': 'short chain with target'
    },
    {
        'inputs': [
            node.create('a', node.create('b', node.create('c',
                                                          node.create(1)))),
            'one', 'two'
        ],
        'outputs':
Example #25
0
            value = node.get_data(walker)
            # represent the next with an arrow-like figure
            result += ' *-]-->[ '+str(value)+' |'

        # at the end of the chain, use '/'
        result += ' / ]'

    return result


test_count_chain = [
    {'inputs' : None,
     'outputs': 0,
     'reason' : 'Empty node chain'},

    {'inputs' : node.create(1),
     'outputs': 1,
     'reason' : 'node chain with one node'},

    {'inputs' : node.create(1, node.create('two')),
     'outputs': 2,
     'reason' : 'node chain with two nodes'},

    {'inputs' : node.create(1, node.create('two', node.create(3))),
     'outputs': 3,
     'reason' : 'node chain with three nodes'},
]

count_chain_counter = createCounter('A5Q2 count_chain()')

for t in test_count_chain:
Example #26
0
    """
    print(counter['reason'] + '\t', counter['successes'], 'of',
          counter['tests'], 'tests passed')


# end of the counter ADT
###############################################################################################

test_to_string = [
    {
        'inputs': None,
        'outputs': 'EMPTY',
        'reason': 'Empty node chain'
    },
    {
        'inputs': node.create(1),
        'outputs': '[ 1 | / ]',
        'reason': 'node chain with one node'
    },
    {
        'inputs': node.create(1, node.create('two')),
        'outputs': '[ 1 | *-]-->[ two | / ]',
        'reason': 'node chain with two nodes'
    },
    {
        'inputs': node.create(1, node.create('two', node.create(3))),
        'outputs': '[ 1 | *-]-->[ two | *-]-->[ 3 | / ]',
        'reason': 'node chain with three nodes'
    },
]
Example #27
0
def no_join():
    node.create()
Example #28
0
#a7q6_testing enz889 Enhan Zhao cmpt145
import a7q6
import node
#test to_string
test_to_string = [
    {'inputs' : None,
     'outputs': "EMPTY",
     'reason' : 'Empty node chain'},

    {'inputs' : node.create(1),
     'outputs': "[ 1 | / ]",
     'reason' : 'node chain with one node'},

    {'inputs' : node.create(1, node.create(2, node.create(3))),
     'outputs': "[ 1 | * -]-->[ 2 | * -]-->[ 3 | / ]",
     'reason' : 'node chain with multiple nodes'},]
for t in test_to_string:
    inputs = t['inputs']
    expected = t['outputs']
    result = a7q6.to_string(inputs)
    assert result == expected, 'to_string(): got '\
        +str(result)+' expected '+str(expected)+' -- ' +t['reason']

#test copy()
test_copy = [
    {'inputs' : None,
     'outputs': None,
     'reason' : 'Empty node chain'},

    {'inputs' : node.create(1),
     'outputs': node.create(1),
Example #29
0
    Display the final count for the number of successful tests.
    :param counter: a counter.
    :return: None
    """
    print(counter['reason'] + '\t', counter['successes'], 'of',
          counter['tests'], 'tests passed')


test_to_string = [
    {
        'inputs': None,
        'outputs': 'EMPTY',
        'reason': 'Empty node chain'
    },
    {
        'inputs': node.create(1),
        'outputs': '[ 1 | / ]',
        'reason': 'node chain with one node'
    },
    {
        'inputs': node.create(1, node.create('two')),
        'outputs': '[ 1 | *-]-->[ two | / ]',
        'reason': 'node chain with two nodes'
    },
    {
        'inputs': node.create(1, node.create('two', node.create(3))),
        'outputs': '[ 1 | *-]-->[ two | *-]-->[ 3 | / ]',
        'reason': 'node chain with three nodes'
    },
]
Example #30
0
# CMPT 145: Assignment 5 Question 2
# test script

import a5q1 as a5q1
import a5q2 as a5q2
import node as node

test_count_chain = [
    {
        'inputs': None,
        'outputs': 0,
        'reason': 'Empty node chain'
    },
    {
        'inputs': node.create(1),
        'outputs': 1,
        'reason': 'node chain with one node'
    },
    {
        'inputs': node.create(1, node.create('two')),
        'outputs': 2,
        'reason': 'node chain with two nodes'
    },
    {
        'inputs': node.create(1, node.create('two', node.create(3))),
        'outputs': 3,
        'reason': 'node chain with three nodes'
    },
]
Example #31
0
    TOPO = Topology_Service.Topology()
    node.add_service(TOPO)

    if len(sys.argv) > 2:
        node_name = sys.argv[2]
        node_port = int(sys.argv[3])
        othernode = node.Node_Info(node_name, node_port)
        node.join(othernode)
        print node.thisNode
        print othernode
        #f = lambda : node.join(othernode)
        #t = Thread(target=f)
        #t.start()
    else:
        node.create()
        #t = Thread(target = node.create)
        #t.start()
    node.startup()
    while True:
        cmd = raw_input(">>")
        if cmd[:6] == "get f ":
            x = int(cmd[6:])
            print node.fingerTable[x]
        elif cmd == 'q' or cmd ==  'Q' :
            node.my_polite_exit()
            exit()
        elif cmd == 'g' or cmd == 'G':
            print "estimate:", node.estimate_ring_density()
        elif cmd[:4] == "get ":
            database.get_record(cmd[4:])