From ec4069195851fcbbb7fd0dc53883ca718f520aba Mon Sep 17 00:00:00 2001 From: estoup Date: Thu, 22 Oct 2020 14:37:02 -0500 Subject: [PATCH 1/3] initial linked list solution --- .gitignore | 1 + python/linked_list.py | 64 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..db28329 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,67 @@ class LinkList: # write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList + def __init__(self, head=None): + self.head = head + self.length = 0 def add(self, data): - # write your code to ADD an element to the Linked List - pass + if not self.head: + new_node = Node(data) + self.head = new_node + else: + previous_node = self.head + while previous_node.next_node: + current_node = previous_node.next_node + previous_node = current_node + new_node = Node(data) + previous_node.next_node = new_node + self.length += 1 + def remove(self, data): - # write your code to REMOVE an element from the Linked List - pass + if self.head.data == data: + self.head = self.head.next_node + else: + previous_node = self.head + current_node = self.head.next_node + while current_node.data != data: + previous_node = current_node + current_node = current_node.next_node + previous_node.next_node = current_node.next_node + self.length -= 1 + def get(self, element_to_get): - # write you code to GET and return an element from the Linked List - pass + current_node = self.head + for _i in range(0, element_to_get): + current_node = current_node.next_node + return current_node.data + # ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + + def __init__(self, data, next_node=None): + self.data = data + self.next_node=next_node + + +ll = LinkList() +ll.add(1) +ll.add(2) +ll.add(3) + +print(ll.head) +print(ll.head.data) +print(ll.head.next_node.data) +print(ll.head.next_node.next_node.data) +print(ll.length) + +# ll.remove(3) + +print(ll.head.data) +print(ll.head.next_node.data) +print(ll.length) + +print(ll.get(0)) + From 2222caf5db7ba9fabdd5362924577569811e2c0e Mon Sep 17 00:00:00 2001 From: estoup Date: Thu, 22 Oct 2020 17:29:48 -0500 Subject: [PATCH 2/3] queue complete --- python/queue.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/python/queue.py b/python/queue.py index f52c1a1..5bf6caf 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,21 @@ class Queue: # write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue + def __init__(self): + self.total = 0 + self.queue = [] - def enqueue(self): + def enqueue(self, data): # write your code to add data to the Queue following FIFO and return the Queue - pass + self.queue.append(data) + self.total += 1 + return self.queue - def dequeue(self, data): + def dequeue(self): # write your code to removes the data to the Queue following FIFO and return the Queue - pass + self.queue.pop(0) + self.total -= 1 + return self.queue def size(self): # write your code that returns the size of the Queue - pass + return self.total From 1dff0bfc1aadba38be6a2bf76b4530835e6a07d5 Mon Sep 17 00:00:00 2001 From: estoup Date: Fri, 23 Oct 2020 08:18:43 -0500 Subject: [PATCH 3/3] non-stretch solution --- python/queue.py | 2 ++ python/stack.py | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/python/queue.py b/python/queue.py index 5bf6caf..0177482 100644 --- a/python/queue.py +++ b/python/queue.py @@ -12,6 +12,8 @@ def enqueue(self, data): def dequeue(self): # write your code to removes the data to the Queue following FIFO and return the Queue + if not self.queue: + return 'Nothing to dequeue' self.queue.pop(0) self.total -= 1 return self.queue diff --git a/python/stack.py b/python/stack.py index dd102b1..f9e6405 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,23 @@ class Stack: # write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack + def __init__(self): + self.total = 0 + self.stack = [] - def push(self): + def push(self, data): # write your code to add data following LIFO and return the Stack - pass + self.stack.append(data) + self.total += 1 + return self.stack - def pop(self, data): + def pop(self): # write your code to removes the data following LIFO and return the Stack - pass + if not self.stack: + return 'Nothing to pop' + self.stack.pop() + self.total -= 1 + return self.stack def size(self): # write your code that returns the size of the Stack - pass + return self.total