From 8f2a540893ba39ae9e9444a66d38e107ca07735b Mon Sep 17 00:00:00 2001 From: krishnavarun7 <91163011+krishnavarun7@users.noreply.github.com> Date: Sun, 20 Feb 2022 21:24:56 -0500 Subject: [PATCH 1/2] Pre course 1 pyhton --- Exercise_1.py | 13 +++++++++++-- Exercise_2.py | 15 +++++++++++++++ Exercise_3.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..6ca4f7a15 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -2,19 +2,28 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.a = [] def isEmpty(self): + return self.a == [] def push(self, item): + self.a.append(item) def pop(self): - + if (len(self.a)>0): + self.a.pop() + else: + return "stack underflow" def peek(self): + return self.arr[-1] def size(self): - + + return len(self.a) def show(self): + return self.a s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..407a22bfe 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,25 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = None def push(self, data): + if self.head == None: + self.head = Node(data) + else: + nw = Node(data) + nw.next = self.head + self.head = nw + def pop(self): + if self.head is not None: + ppn = self.head + self.head = self.head.next + ppn.next = None + return ppn.data + else: + return None a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..f77167e5d 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -2,7 +2,12 @@ class ListNode: """ A node in a singly-linked list. """ - def __init__(self, data=None, next=None): + + def __init__(self, data, next=None): + self.data = data + self.next = None + + class SinglyLinkedList: def __init__(self): @@ -13,12 +18,31 @@ def __init__(self): self.head = None def append(self, data): + if self.head == None: + self.head = ListNode(data) + + else: + temp = self.head + while(temp.next) != None: + temp = temp.next + temp.next = ListNode(data) + + """ Insert a new element at the end of the list. Takes O(n) time. """ def find(self, key): + + temp = self.head + while (temp.next != None): + if (temp.data == key): + return key + else: + temp = temp.next + + return temp.next """ Search for the first element with `data` matching `key`. Return the element or `None` if not found. @@ -26,7 +50,25 @@ def find(self, key): """ def remove(self, key): + """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + temp = self.head + if temp is not None: + if temp.data == key: + self.head = temp.next + temp = None + return + while (temp is not None): + if temp.data == key: + break + prev = temp + temp = temp.next + if temp == None: + return None + prev.next = temp.next + + temp = None + From bc48f5d8d8eba99c5c94979fe8f1a4cfd5e3e64c Mon Sep 17 00:00:00 2001 From: Varun Vuppala Date: Thu, 25 Dec 2025 18:16:54 -0600 Subject: [PATCH 2/2] Added the code to the files --- Exercise_1.py | 22 ++++++++------- Exercise_3.py | 75 +++++++++++++++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 6ca4f7a15..ff1de95a2 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -2,28 +2,30 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): - self.a = [] + self.stack = [] def isEmpty(self): - return self.a == [] + return len(self.stack) == 0 def push(self, item): - self.a.append(item) + self.stack.append(item) def pop(self): - if (len(self.a)>0): - self.a.pop() - else: - return "stack underflow" + if self.isEmpty(): + return "Stack is Empty" + + return self.stack.pop() def peek(self): - return self.arr[-1] + if self.isEmpty(): + return "Stack is Empty" + return self.stack[-1] def size(self): - return len(self.a) + return len(self.stack) def show(self): - return self.a + return self.stack s = myStack() diff --git a/Exercise_3.py b/Exercise_3.py index f77167e5d..dba3a3c1a 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -5,7 +5,7 @@ class ListNode: def __init__(self, data, next=None): self.data = data - self.next = None + self.next = next @@ -18,14 +18,17 @@ def __init__(self): self.head = None def append(self, data): - if self.head == None: - self.head = ListNode(data) - else: - temp = self.head - while(temp.next) != None: - temp = temp.next - temp.next = ListNode(data) + if self.head == None: + self.head = ListNode(data) + return + + curr = self.head + while curr.next: + curr = curr.next + curr.next = ListNode(data) + + """ @@ -35,14 +38,13 @@ def append(self, data): def find(self, key): - temp = self.head - while (temp.next != None): - if (temp.data == key): - return key - else: - temp = temp.next - - return temp.next + curr = self.head + while (curr): + if (curr.data == key): + return curr + + curr = curr.next + return None """ Search for the first element with `data` matching `key`. Return the element or `None` if not found. @@ -50,25 +52,34 @@ def find(self, key): """ def remove(self, key): - + if self.head and self.head.data == key: + temp = self.head + self.head = self.head.next + temp.next = None + return + + curr = self.head + while curr and curr.next: + if (curr.next.data == key): + temp = curr.next + curr.next = curr.next.next + temp.next = None + return + curr = curr.next + return None """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ - temp = self.head - if temp is not None: - if temp.data == key: - self.head = temp.next - temp = None - return - while (temp is not None): - if temp.data == key: - break - prev = temp - temp = temp.next - if temp == None: - return None - prev.next = temp.next + def print_list(self): + curr = self.head + + if curr is None: + print("List is empty") + return - temp = None + while curr: + print(curr.data, end=" -> ") + curr = curr.next + print("None")