Skip to content
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

PreCourse-1 Submit #2068

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
30 changes: 21 additions & 9 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@ class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):

self.stack = []

def isEmpty(self):

return len(self.stack) == 0

def push(self, item):

self.stack.append(item)

def pop(self):


if self.isEmpty():
raise IndexError("Pop from an empty stack")
return self.stack.pop()

def peek(self):

if self.isEmpty():
raise IndexError("Peek from an empty stack")
return self.stack[-1]

def size(self):

return len(self.stack)

def show(self):

if self.isEmpty():
return "stack is empty"
else:
return f"last element is {self.stack[-1]}"

s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
print(s.pop())
102 changes: 75 additions & 27 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,80 @@

class Node:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self, data, next=None):
self.data = data
self.next = None


class Stack:
def __init__(self):

def push(self, data):

self.top = None

def push(self, val):
node = Node(val)
# Insert node at the start of the linked list
node.next = self.top
self.top = node

def pop(self):

if self.top is None:
print("Stack is empty")
return None
top_element = self.top
self.top = top_element.next
return top_element.data

def size(self):
stack_size = 0
current_node = self.top
while current_node:
stack_size += 1
current_node = current_node.next
return stack_size

def print_stack(self):
current_node = self.top
while current_node:
print('||',current_node.data,'||')
current_node = current_node.next
print('-------------')

def get_top(self):
if self.top is None:
return None
return self.top.data

def is_empty(self):
return self.top is None



a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
operation = do[0].strip().lower()
if operation == 'push':
a_stack.push(int(do[1]))
elif operation == 'pop':
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'quit':
break
a_stack.push(1)
a_stack.push(2)
a_stack.push(3)
a_stack.push(4)
a_stack.print_stack()
print('Size - ', a_stack.size())
print("pooped element - ", a_stack.pop())
a_stack.print_stack()
a_stack.get_top()
print('is stack empty? ', a_stack.is_empty())
print('Size - ', a_stack.size())

# while True:
# #Give input as string if getting an EOF error. Give input like "push 10" or "pop"
# print('push <value>')
# print('pop')
# print('quit')
# do = input('What would you like to do? ').split()
# #Give input as string if getting an EOF error. Give input like "push 10" or "pop"
# operation = do[0].strip().lower()
# if operation == 'push':
# a_stack.push(int(do[1]))
# elif operation == 'pop':
# popped = a_stack.pop()
# if popped is None:
# print('Stack is empty.')
# else:
# print('Popped value: ', int(popped))
# elif operation == 'quit':
# break
60 changes: 57 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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):
"""
Expand All @@ -17,16 +19,68 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""

new_node = ListNode(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node

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.
"""

current = self.head
while current:
if current.data == key:
return current
current = current.next
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
current = self.head
previous = None

while current:
if current.data == key:
if previous is None:
self.head = current.next
else:
previous.next = current.next
return True # Element removed
previous = current
current = current.next
return False

if __name__ == "__main__":
sll = SinglyLinkedList()

sll.append(1)
sll.append(2)
sll.append(3)

found_node = sll.find(2)
if found_node:
print(f"Found: {found_node.data}")
else:
print("Not found.")

result = sll.remove(2)
if result:
print("Element removed.")
else:
print("Element not found.")

found_node = sll.find(2)
if found_node:
print(f"Found: {found_node.data}")
else:
print("Not found.")
36 changes: 36 additions & 0 deletions tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.stack = []

def isEmpty(self):
return len(self.stack) == 0

def push(self, item):
self.stack.append(item)

def pop(self):
if self.isEmpty():
raise IndexError("Pop from an empty stack")
return self.stack.pop()

def peek(self):
if self.isEmpty():
raise IndexError("Peek from an empty stack")
return self.stack[-1]

def size(self):
return len(self.stack)

def show(self):
if self.isEmpty():
return "stack is empty"
else:
return f"last element is {self.stack[-1]}"

s = myStack()
s.push('1')
s.push('2')
print(s.show())
print(s.pop())