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
def main(): with open(FILE_INPUT) as input_file: # with open(FILE_OUTPUT, 'w') as output_file: # sys.stdout = output_file csv_reader = csv.reader(input_file, delimiter=',') line = 0 for row in csv_reader: command = row[0] param_one = row[1] param_two = row[2] # print line number and command print('{}:{},{},{}'.format(line, command, param_one, param_two)) # call appropriate method based on command if command == 'CREATE': linkedlist = LinkedList() else: exec_command(command, linkedlist, param_one, param_two) line += 1
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 []
from linkedlist_api import LinkedList import csv 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':
def __init__(self): '''Constructor''' self.queue = LinkedList()
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.list.debug_print() def cmd_create(self, *args): self.list = LinkedList() def cmd_add(self, *args): self.list.add(args[0]) def cmd_insert(self, *args): self.list.insert(int(args[0]), args[1]) def cmd_set(self, *args): self.list.set(int(args[0]), args[1]) def cmd_get(self, *args): print(self.list.get(int(args[0]))) def cmd_delete(self, *args): self.list.delete(int(args[0])) def cmd_swap(self, *args): self.list.swap(int(args[0]), int(args[1]))
def cmd_create(self, *args): self.list = LinkedList()
# # print(a) # ll.debug_print() from linkedlist_api import LinkedList import csv #def main(): 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': print(action,parameters) linkedList=LinkedList() elif action == 'DEBUG': linkedList.debug_print() elif action == 'ADD': print(action,parameters) linkedList.add(parameters[0]) elif action == 'SET': print(action,parameters) linkedList.set(int(parameters[0]),parameters[1]) elif action == 'GET': print(action,parameters) linkedList.get(int(parameters[0])) elif action == 'DELETE' : print(action,parameters) linkedList.delete(int(parameters[0])) elif action == 'INSERT':
from linkedlist_api import LinkedList, Node import csv import sys orig_stdout = sys.stdout 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]))
def test(): ll = LinkedList() ll.add("a") ll.add("b") ll.debug_print() ll.set(1, "B") ll.debug_print() ll.add("c") ll.debug_print() # ll.delete(3) ll.swap(0, 1) ll.debug_print()