From f8fe94454015f735af3aa99719d86fbceb2d95e5 Mon Sep 17 00:00:00 2001 From: MrWeast Date: Thu, 30 Nov 2023 06:32:39 -0500 Subject: [PATCH 1/3] create unordered list in python added unordered list in python --- src/python/unordered_linked_list.py | 115 ++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/python/unordered_linked_list.py diff --git a/src/python/unordered_linked_list.py b/src/python/unordered_linked_list.py new file mode 100644 index 00000000..9e795e51 --- /dev/null +++ b/src/python/unordered_linked_list.py @@ -0,0 +1,115 @@ + +class Node: + + def __init__(self,value,rptr=None) -> None: + self.value = value + self.rptr = rptr + + @property + def get_value(self): + return self.value + + @property + def next(self): + return self.rptr + + @next.setter + def next(self,ptr): + self.rptr = ptr + + +class Unordered_Linked_List: + def __init__(self) -> None: + self.first_node = None + + def insert(self,value): # inserts a new node with the given value + if self.first_node is None: + self.first_node = Node(value) + else: + tmp = Node(value) + tmp.next = self.first_node + self.first_node = tmp + + def find(self,value): # returns true if the specified value is in your list + ptr = self.first_node + while ptr is not None: + if ptr.value == value: + print(f'{value} is in your list') + return True + else: + ptr = ptr.next + + print(f'{value} is not in your list') + return False + + def size(self): # returns size of the list + ptr = self.first_node + i = 0 + while ptr is not None: + ptr = ptr.next + i+=1 + print(f'Your list is of size {i}') + return i + + def remove(self,value): # removes all instances of a given value + ptr = self.first_node + prev = None + while ptr is not None: + if ptr.value == value: + if ptr == self.first_node: + tmp = ptr.next + self.first_node = tmp + else: + prev.next = ptr.next + + prev = ptr + ptr = ptr.next + + def show(self): + ptr = self.first_node + val = [] + while ptr is not None: + val.append(ptr.get_value) + ptr = ptr.next + + print(val) + +def main(): + list = Unordered_Linked_List() + + list.insert(1) + list.insert(3) + list.insert(5) + list.insert(2) + + list.size() + list.show() + + list.find(3) + list.find(9) + + list.remove(1) + list.remove(3) + list.remove(5) + list.remove(2) + + list.show() + + list.insert(1) + list.insert(3) + list.insert(5) + list.insert(3) + + list.show() + + list.remove(3) + + list.show() + + + + +if __name__ == "__main__": + main() + + From 3a5ca4285668660e31a14f62799d92c8f9097774 Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 00:00:23 -0500 Subject: [PATCH 2/3] pep8 and readme fix I think this should fix the pep8 issues and I added the readme file link --- README.md | 2 +- src/python/unordered_linked_list.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a39f8c30..97588cf7 100644 --- a/README.md +++ b/README.md @@ -2230,7 +2230,7 @@ In order to achieve greater coverage and encourage more people to contribute to - + diff --git a/src/python/unordered_linked_list.py b/src/python/unordered_linked_list.py index 9e795e51..65d6acb1 100644 --- a/src/python/unordered_linked_list.py +++ b/src/python/unordered_linked_list.py @@ -22,7 +22,8 @@ class Unordered_Linked_List: def __init__(self) -> None: self.first_node = None - def insert(self,value): # inserts a new node with the given value + def insert(self,value): + # inserts a new node with the given value if self.first_node is None: self.first_node = Node(value) else: @@ -30,7 +31,8 @@ def insert(self,value): # inserts a new node with the given value tmp.next = self.first_node self.first_node = tmp - def find(self,value): # returns true if the specified value is in your list + def find(self,value): + # returns true if the specified value is in your list ptr = self.first_node while ptr is not None: if ptr.value == value: @@ -51,7 +53,8 @@ def size(self): # returns size of the list print(f'Your list is of size {i}') return i - def remove(self,value): # removes all instances of a given value + def remove(self,value): + # removes all instances of a given value ptr = self.first_node prev = None while ptr is not None: From b98c6923bc87bbb718e6139b45e37b76284321f4 Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 06:57:42 -0500 Subject: [PATCH 3/3] formatted using black and added logo used black and added suggested logo change --- README.md | 2 +- src/python/unordered_linked_list.py | 51 +++++++++++++---------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 97588cf7..9def01ab 100644 --- a/README.md +++ b/README.md @@ -2231,7 +2231,7 @@ In order to achieve greater coverage and encourage more people to contribute to - + diff --git a/src/python/unordered_linked_list.py b/src/python/unordered_linked_list.py index 65d6acb1..230547ed 100644 --- a/src/python/unordered_linked_list.py +++ b/src/python/unordered_linked_list.py @@ -1,28 +1,26 @@ - class Node: - - def __init__(self,value,rptr=None) -> None: + def __init__(self, value, rptr=None) -> None: self.value = value self.rptr = rptr @property def get_value(self): return self.value - + @property def next(self): return self.rptr - + @next.setter - def next(self,ptr): + def next(self, ptr): self.rptr = ptr - + class Unordered_Linked_List: def __init__(self) -> None: self.first_node = None - - def insert(self,value): + + def insert(self, value): # inserts a new node with the given value if self.first_node is None: self.first_node = Node(value) @@ -30,30 +28,30 @@ def insert(self,value): tmp = Node(value) tmp.next = self.first_node self.first_node = tmp - - def find(self,value): + + def find(self, value): # returns true if the specified value is in your list ptr = self.first_node while ptr is not None: if ptr.value == value: - print(f'{value} is in your list') + print(f"{value} is in your list") return True else: ptr = ptr.next - - print(f'{value} is not in your list') + + print(f"{value} is not in your list") return False - - def size(self): # returns size of the list + + def size(self): # returns size of the list ptr = self.first_node i = 0 while ptr is not None: ptr = ptr.next - i+=1 - print(f'Your list is of size {i}') + i += 1 + print(f"Your list is of size {i}") return i - - def remove(self,value): + + def remove(self, value): # removes all instances of a given value ptr = self.first_node prev = None @@ -64,19 +62,20 @@ def remove(self,value): self.first_node = tmp else: prev.next = ptr.next - + prev = ptr ptr = ptr.next - + def show(self): ptr = self.first_node val = [] while ptr is not None: val.append(ptr.get_value) ptr = ptr.next - + print(val) + def main(): list = Unordered_Linked_List() @@ -102,7 +101,7 @@ def main(): list.insert(3) list.insert(5) list.insert(3) - + list.show() list.remove(3) @@ -110,9 +109,5 @@ def main(): list.show() - - if __name__ == "__main__": main() - -