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

Completed Pre1 #2003

Open
wants to merge 2 commits 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
41 changes: 29 additions & 12 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# Time Complexity : for each operation, O(1)
# Space Complexity : O(N) where N is the maximum length of integer inputs

class myStack:

#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):

def isEmpty(self):

def push(self, item):

def pop(self):

def __init__(self):
self.stack = []

def isEmpty(self):
if len(self.stack) != 0:
return True
return False

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

def size(self):

def show(self):

def pop(self):
return self.stack.pop()

def peek(self):
if self.stack:
return self.stack[-1]

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

def show(self):
return self.stack


s = myStack()
s.push('1')
s.push('2')
print(s.peek())
print(s.isEmpty())
print(s.pop())
print(s.show())
18 changes: 18 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Time Complexity : for both operations, O(1)
# Space Complexity : O(N) where N is the maximum length of integer inputs

class Node:
def __init__(self, data):
Expand All @@ -6,11 +8,27 @@ def __init__(self, data):

class Stack:
def __init__(self):
self.head = None

def push(self, data):
newNode = Node(data)
if self.head == None:
self.head = newNode
else:
newNode.next = self.head
self.head = newNode

def pop(self):
if self.head == None:
return -1

temp = self.head
val = self.head.data
self.head = self.head.next

del temp

return val
a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
43 changes: 43 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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 @@ -11,22 +13,63 @@ def __init__(self):
Takes O(1) time.
"""
self.head = None
self.tail = None

def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
if self.head == None:
self.head = ListNode(data)
self.tail = self.head
else:
self.tail.next = ListNode(data)
self.tail = self.tail.next

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.
"""
cur = self.head
while cur:
if cur.data == key:
return cur
cur = cur.next

return None

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

if self.head.data == key:
temp = self.head
self.head = self.head.next

if self.head == None:
self.tail = None

del temp
return

prev, cur = self.head, self.head.next
while cur:
if cur.data == key:
prev.next = cur.next

if cur == self.tail:
self.tail = prev

del cur
return
prev = cur
cur = cur.next