Skip to content

Completed PreCourse1 #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : Calculating the Time Complexity for the Push Operation. Worst case would be O(n), in case of resizing the Array
#Your code here along with comments explaining your approach

class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
# Use a list to store stack elements
def __init__(self):

self.stack = []

# T: O(1), S: O(1)
# Check if the stack is empty
def isEmpty(self):

return len(self.stack) == 0

# T: O(n), S: (n)
# Add an element to the stack
def push(self, item):

self.stack.append(item)

# T: O(1), S: O(n)
# Check if the stack is non empty
# Remove an element from the top of the stack
def pop(self):


if self.isEmpty():
print("Stack underflow. Cannot pop from an empty stack")
return None
return self.stack.pop()

# T: O(1), S: (1)
# Check if the stack is non empty
# Return the top of the stack w/o removing it
def peek(self):

if self.isEmpty():
print("Stack is Empty. No elements to peek.")
return None
return self.stack[-1]

# T: O(1), S: O(1)
# Return the length of the stack
def size(self):

return len(self.stack)

# T: O(n), S: (n)
# Check if the stack is non empty
# Print all the elements in the stack
def show(self):
if self.isEmpty():
print("Stack is empty")
else:
print(f"Stack Elements: {self.stack}")



s = myStack()
Expand Down
25 changes: 22 additions & 3 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@ def __init__(self, data):

class Stack:
def __init__(self):

self.top = None

# Create a new Node
# Set its next pointer to head
# Update the Head to the New Node
# T: O(1), S: O(n)
def push(self, data):

newNode = Node(data)
newNode.next = self.top
self.top = newNode
print(f"Pushed: {data}")

# T: O(1), S: O(n)
# Check if the Head is null
# If not, update the Head to Head.next
def pop(self):

if not self.top:
print("Stack Underflow: Cannot pop from an empty stack.")
return None
poppedData = self.top.data
self.top = self.top.next
print(f"Popped: {poppedData}")
return poppedData

a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
54 changes: 51 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,78 @@ class ListNode:
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):

self.data = data
self.next = next

class SinglyLinkedList:
def __init__(self):
# Dummy Node
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
self.head = ListNode(-1)
self.tail = self.head

# T: O(n), S: O(1)
def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""

"""Add a node with the given data to the end of the list."""
newNode = ListNode(data)
if not self.head:
self.head = newNode
print(f"Appended {data} as the head node.")
return
current = self.head
while current.next:
current = current.next
current.next = newNode
print(f"Appended {data} to the linked list.")

# T: O(n), S: O(1)
def find(self, key):
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""
"""Find a node with the given value and return it."""
current = self.head
while current:
if current.data == key:
print(f"Found value: {key}")
return current
current = current.next
print(f"Value {key} not found in the linked list.")
return None

# T: O(n), S: O(1)
def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
"""Remove the first node with the given value."""
if not self.head:
print("Cannot remove from an empty list.")
return

# If the head node is the one to be removed
if self.head.data == key:
print(f"Removed the head node with value: {key}")
self.head = self.head.next
return

# Traverse the list to find the node to remove
current = self.head
while current.next:
if current.next.data == key:
print(f"Removed node with value: {key}")
current.next = current.next.next
return
current = current.next

print(f"Value {key} not found in the linked list.")