From 1a314c09079cbd64e97cd18cfc825ccc324aeb2a Mon Sep 17 00:00:00 2001 From: Wangdi Tenzing Date: Thu, 17 Sep 2020 13:15:20 -0400 Subject: [PATCH 1/5] stack complete with passing tests --- stack/stack.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stack/stack.py b/stack/stack.py index 6e6d660ac7..87c973b468 100644 --- a/stack/stack.py +++ b/stack/stack.py @@ -13,13 +13,16 @@ class Stack: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = [] def __len__(self): - pass + return len(self.storage) def push(self, value): - pass + return self.storage.append(value) def pop(self): - pass + if len(self.storage)==0: + return None + else: + return self.storage.pop() From 59839b2a17b9190a15cc786b315bc1547cae6224 Mon Sep 17 00:00:00 2001 From: Wangdi Tenzing Date: Thu, 17 Sep 2020 20:47:50 -0400 Subject: [PATCH 2/5] 2 out of 3 tests passing in singly_linked_list need Need to complete --- singly_linked_list/singly_linked_list.py | 92 ++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index e69de29bb2..da7a7e40dd 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -0,0 +1,92 @@ +# a class that represents the individual elements in our LL +class Node: + def __init__(self,value=None, next_node=None): + self.value=value + self.next_node=next_node + + def get_value(self): + return self.value + + def get_next_node(self): + return self.next_node + + def set_next_node (self,new_next): + self.next_node=new_next + +class LinkedList: + def __init__(self): + #what attributes do we need to create a LinkedList ? + self.head=None + self.tail=None + + def add_to_head(self,value): + #create a new Node + new_node=Node(value) + if self.head is None: + #update head and tail attributes + self.head=new_node + self.tail=new_node + else: + #set next_node of my new Node to the head + new_node.set_next_node(self.head) + #update the head attribute + self.head=new_node + + + + + def add_to_tail(self,value): + #1. create a node from the value + new_node=Node(value) + # Whats the rule we want to set to indicate that the linked list is empty? + # wouldn't it be better to check the head ? + + #case1: consider the LL being empty + + if self.head is None: + #update the head and tail attributes + self.head=new_node + self.tail=new_node + #case2 : consider the LL not being empty + else: + + self.tail.set_next_node(new_node) + #reassign self.tail to refer to the new node + self.tail=new_node + + def remove_head(self): + # case1: empty list + if self.head is None: + return None + # return the value of the old head + else: + old_head=self.head.get_value() + if self.head==self.tail: + self.head==None + self.tail==None + else: + self.head=self.head.get_next_node() + return old_head + + def remove_tail(self): + # if we have a non empty linked list + if self.tail is None and self.head is None: + return + # we have to start at the head and move down the linked list + #until we get to the node right before the tail + #iterate over our linked list + + current =self.head + + while current.get_next_node()is not self.tail: + current=current.get_next_node() + + #at this point current is the node right before the tail + #set the tail to be none + val=self.tail.get_value() + self.tail=None + #move self.tail to the Node right before + self.tail=current + return val + + From 75ae273dfa62b980df14cb1913fdc9af53e93ee3 Mon Sep 17 00:00:00 2001 From: Wangdi Tenzing Date: Tue, 22 Sep 2020 11:31:32 -0400 Subject: [PATCH 3/5] Passing all tests except remove from tail --- singly_linked_list/singly_linked_list.py | 57 +++++++++++++++++------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index da7a7e40dd..57814088fc 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -69,24 +69,47 @@ def remove_head(self): return old_head def remove_tail(self): - # if we have a non empty linked list - if self.tail is None and self.head is None: - return - # we have to start at the head and move down the linked list - #until we get to the node right before the tail - #iterate over our linked list - - current =self.head + # if we have an emoty list we dont have to remove anything + if self.head is None : + return None + #else return value of the old head + else: + ret_value=self.tail.get_value() + + #if we have a list with one element + if self.head==self.tail: + self.head=None + self.tail=None + + #list with two elements + else: + # if current.next node is not the tail + # assign current to current.next.. then you set it to None making that the new tail + # return the current value + + cur_node=self.head + while cur_node.get_next_node()is not self.tail: + cur_node=cur_node.get_next_node() + # keep going + #update pointer of temp node (prev_tail)None + cur_node.set_next_node(None) + self.tail=cur_node + #return value + return ret_value - while current.get_next_node()is not self.tail: - current=current.get_next_node() + def contains(self,value): + cur_node=self.head + while cur_node is not None: + # if value exists + if cur_node.get_value()==value: + return True - #at this point current is the node right before the tail - #set the tail to be none - val=self.tail.get_value() - self.tail=None - #move self.tail to the Node right before - self.tail=current - return val + return False + + + + + + From ac7797554ccc622fbe13b57ed03088c8ad607666 Mon Sep 17 00:00:00 2001 From: Wangdi Tenzing Date: Tue, 22 Sep 2020 12:58:17 -0400 Subject: [PATCH 4/5] passing 3 tests failing 1 in queue --- queue/queue.py | 106 +++++++++++++++++++++++++++++++-- queue/singly_linked_list.py | 115 ++++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 queue/singly_linked_list.py diff --git a/queue/queue.py b/queue/queue.py index 0d2599ded7..3420ed58a7 100644 --- a/queue/queue.py +++ b/queue/queue.py @@ -13,16 +13,114 @@ Stretch: What if you could only use instances of your Stack class to implement the Queue? What would that look like? How many Stacks would you need? Try it! """ + +# class Node: +# def __init__(self, value=None, next_node=None): +# self.value=value +# self.next_node=next_node + +# def get_value(self): +# return self.value + +# def get_next_node(self): +# return self.next_node + +# def set_next_node(self,next_new): +# self.next_node=next_new + + +# class LinkedList: +# def __init__(self): +# self.head=None +# self.tail=None + +# def add_to_head(self,value): +# #create a new Node +# new_node=Node(value) +# if self.head is None: +# #update the head and tail attributes +# self.head=new_node +# self.tail=new_node +# else: +# #set the next node of the new node to the head +# new_node.set_next_node(self.head) +# #update the head attribute +# self.head=new_node + +# def add_to_tail(self,value): +# #create a new node +# new_node=Node(value) +# # if the linked list is empty (case 1) +# if self.head is None: +# #update the head and tail attributes +# self.head=new_node +# self.tail=new_node +# # the linked list is not empty (case 2) +# else: +# #update the next node of the tail +# self.tail.set_next_node(new_node) +# #update self.tail +# self.tail=new_node + +# def remove_head(self): +# # if it is an empty list (case1) +# if self.head is None: +# return None +# # else return the old value of the head +# else: +# ret_value=self.head.get_value() +# #list with one element +# if self.head ==self.tail: +# self.head =None +# self.tail=None +# else: +# self.head=self.head.get_next_node() +# return ret_value + +# def remove_tail(self): +# # if it is an empty list +# if self.head is None: +# return None +# else: +# ret_value=self.tail.get_value() +# #list with one element +# if self.head ==self.tail: +# self.head =None +# self.tail=None +# # if the list has more than 2 elements +# else: +# #if the next node is not the tail +# # assign the current_node to the next +# # set the current node to None by doing so the current node becomes the new tail +# current_node =self.head +# while current_node.next_node is not self.tail: +# current_node=current_node.next_node +# current_node.set_next_node(None) +# self.tail=current_node +# return ret_value + + +from singly_linked_list import LinkedList + + + + class Queue: def __init__(self): self.size = 0 - # self.storage = ? + self.storage=LinkedList() def __len__(self): - pass + return self.size def enqueue(self, value): - pass + self.storage.add_to_tail(value) + self.size +=1 + return self.storage def dequeue(self): - pass + if self.size >=1: + value=self.storage.remove_head() + self.size -=1 + return value + diff --git a/queue/singly_linked_list.py b/queue/singly_linked_list.py new file mode 100644 index 0000000000..57814088fc --- /dev/null +++ b/queue/singly_linked_list.py @@ -0,0 +1,115 @@ +# a class that represents the individual elements in our LL +class Node: + def __init__(self,value=None, next_node=None): + self.value=value + self.next_node=next_node + + def get_value(self): + return self.value + + def get_next_node(self): + return self.next_node + + def set_next_node (self,new_next): + self.next_node=new_next + +class LinkedList: + def __init__(self): + #what attributes do we need to create a LinkedList ? + self.head=None + self.tail=None + + def add_to_head(self,value): + #create a new Node + new_node=Node(value) + if self.head is None: + #update head and tail attributes + self.head=new_node + self.tail=new_node + else: + #set next_node of my new Node to the head + new_node.set_next_node(self.head) + #update the head attribute + self.head=new_node + + + + + def add_to_tail(self,value): + #1. create a node from the value + new_node=Node(value) + # Whats the rule we want to set to indicate that the linked list is empty? + # wouldn't it be better to check the head ? + + #case1: consider the LL being empty + + if self.head is None: + #update the head and tail attributes + self.head=new_node + self.tail=new_node + #case2 : consider the LL not being empty + else: + + self.tail.set_next_node(new_node) + #reassign self.tail to refer to the new node + self.tail=new_node + + def remove_head(self): + # case1: empty list + if self.head is None: + return None + # return the value of the old head + else: + old_head=self.head.get_value() + if self.head==self.tail: + self.head==None + self.tail==None + else: + self.head=self.head.get_next_node() + return old_head + + def remove_tail(self): + # if we have an emoty list we dont have to remove anything + if self.head is None : + return None + #else return value of the old head + else: + ret_value=self.tail.get_value() + + #if we have a list with one element + if self.head==self.tail: + self.head=None + self.tail=None + + #list with two elements + else: + # if current.next node is not the tail + # assign current to current.next.. then you set it to None making that the new tail + # return the current value + + cur_node=self.head + while cur_node.get_next_node()is not self.tail: + cur_node=cur_node.get_next_node() + # keep going + #update pointer of temp node (prev_tail)None + cur_node.set_next_node(None) + self.tail=cur_node + #return value + return ret_value + + def contains(self,value): + cur_node=self.head + while cur_node is not None: + # if value exists + if cur_node.get_value()==value: + return True + + return False + + + + + + + + From 7d4a85e7b35e497fedc44f820ab9b9579c6a149f Mon Sep 17 00:00:00 2001 From: Wangdi Tenzing Date: Tue, 22 Sep 2020 21:24:51 -0400 Subject: [PATCH 5/5] All the tests passing now for singly_linked_list --- singly_linked_list/singly_linked_list.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index 57814088fc..335ca35caa 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -62,8 +62,9 @@ def remove_head(self): else: old_head=self.head.get_value() if self.head==self.tail: - self.head==None - self.tail==None + self.head=None + self.tail=None + return old_head else: self.head=self.head.get_next_node() return old_head