Пример #1
0
class Queue(object):
    '''
    A linked list implementation of a queue.

    This contains a LinkedList internally.  It does not extend LinkedList.
    In other words, this class uses "Composition" rather than "Inheritance".
    '''
    def __init__(self):
        '''Constructor'''
        self.queue = LinkedList()

    def debug_print(self):
        '''Prints a representation of the entire queue.'''
        self.queue.debug_print()

    def enqueue(self, item):
        '''Adds an item to the end of the queue'''
        self.queue.add(item)

    def dequeue(self):
        '''
        Dequeues the first item from the list.  This involves the following:
            1. Get the first node in the list.
            2. Delete the node from the list.
            3. Return the value of the node.
        '''
        beg = self.queue._get_node(0)
        self.queue.delete(0)
        return beg.value

    def size(self):
        '''Returns the number of items in the queue'''
        return self.queue.size
Пример #2
0
class Processor(object):
    def run(self, f):
        '''Processes the given file stream.'''
        for line_i, line in enumerate(f):
            # get the line parts
            line = line.rstrip()
            print('{}:{}'.format(line_i, line))
            parts = line.split(',')
            # call this command's function
            try:
                func = getattr(self, 'cmd_{}'.format(parts[0].lower()))
                func(*parts[1:])
            except Exception as e:
                print('Error: {}'.format(e))

    def cmd_debug(self, *args):
        self.ll.debug_print()

    def cmd_create(self, *args):
        self.ll = LinkedList()

    def cmd_add(self, *args):
        self.ll.add(args[0])

    def cmd_insert(self, *args):
        self.ll.insert(int(args[0]), args[1])

    def cmd_set(self, *args):
        self.ll.set(int(args[0]), args[1])

    def cmd_get_node(self, *args):
        print(self.ll._get_node(int(args[0])))

    def cmd_delete(self, *args):
        self.ll.delete(int(args[0]))

    def cmd_swap(self, *args):
        self.ll.swap(int(args[0]), int(args[1]))

    def cmd_get(self, *args):
        self.ll.get(int(args[0]))
Пример #3
0
def array_runner(command, arg1, arg2):
    global a

    if arg1.isdigit():
        arg1 = int(arg1)
    if arg2.isdigit():
        arg2 = int(arg2)
    if command == 'CREATE':
        a = LinkedList()
    elif command == 'DEBUG':
        a.debug_print()
    elif command == 'ADD':
        a.add(arg1)
    elif command == 'SET':
        a.set(arg1, arg2)
    elif command == 'GET':
        a.get(arg1)
    elif command == 'DELETE':
        a.delete(arg1)
    elif command == 'INSERT':
        a.insert(arg1, arg2)
    elif command == 'SWAP':
        a.swap(arg1, arg2)
    return []
Пример #4
0
linkedList = LinkedList()

linkedList.add(1)
linkedList.add(2)
linkedList.add(3)
linkedList.debug_print()
linkedList.set(1,0)
a=linkedList.get(1)
linkedList.debug_print()
linkedList.swap(0,1)
print(a)
linkedList.debug_print()
linkedList.insert(1,9)
linkedList.debug_print()
linkedList.delete(0)
linkedList.debug_print()

with open('data_example.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in reader:
        items = row[0].split(",",1)
        parameters=items[1].split(",")
        action = items[0]
        if action ==  'CREATE':
            linkedList = LinkedList()
        elif action == 'DEBUG':
            linkedList.debug_print()
        elif action == 'ADD':
            print(action,parameters)
            linkedList.add(parameters[0])
Пример #5
0
f = open('output.txt', 'w')
sys.stdout = f
line_count = 0

with open('data.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        print("{}:{}".format(line_count, ','.join(str(item) for item in row)))
        command = row[0]
        if command == 'CREATE':
            linked_list = LinkedList()
        elif command == 'DEBUG':
            linked_list.debug_print()
        elif command == 'ADD':
            linked_list.add(row[1])
        elif command == 'GET':
            linked_list.get(int(row[1]))
        elif command == 'SET':
            linked_list.set(int(row[1]), row[2])
        elif command == 'INSERT':
            linked_list.insert(int(row[1]), row[2])
        elif command == 'DELETE':
            linked_list.delete(int(row[1]))
        elif command == 'SWAP':
            linked_list.swap(int(row[1]), int(row[2]))

        line_count += 1

sys.stdout = orig_stdout
f.close()