def check_palindrome(ll): """ Check if a Linked List is a Palindrome Args: ll(LinkedList): The linked list to check Returns: Boolean: Indicates if linked list is a palindrome or not """ fast = ll.head slow = ll.head # stack to store the first half of the linked list s = Stack() while fast is not None and fast.next is not None: s.push(slow.value) slow = slow.next fast = fast.next.next # Handles the case of odd number of nodes in the linked list if fast is not None: slow = slow.next # compare the second half of the linked list with # contents of the stack while slow is not None: top = s.pop() if top == slow.value: slow = slow.next else: return False return True
def rec_merge_sort(st, size): """ Merge sort recursively after dividing each stack Args: st(Stack): Stack to sort size(Integer): The size of the stack to sort """ if size == 1: return else: size1 = size // 2 if size % 2 == 0: size2 = size1 else: size2 = size1 + 1 s1 = Stack() s2 = Stack() for i in range(0, size1): s1.push(st.pop()) for i in range(0, size2): s2.push(st.pop()) rec_merge_sort(s1, s1.size()) # print('s1 size is ', s1.size()) rec_merge_sort(s2, s2.size()) # print('s2 size is ', s2.size()) temp = merge(s1, s2) for i in range(temp.size()): st.push(temp.pop()) # print('st size is ', st.size()) return
class Tower(object): """ Creates a tower instance Attributes: disks(Stack): Stack to store disks index(Integer): Index of tower """ def __init__(self, index): # Each tower has a stack size of 5 self.disks = Stack() self.index = index def add(self, disk): """ Adds a disk to this tower Args: disk(Integer): Disk to add """ if not self.disks.is_empty() and self.disks.peek() <= disk: print("Error placing disk {0} !!".format(disk)) else: self.disks.push(disk) # print(self.disks.size()) def move_top_to(self, dest): """ Moves top disk of this tower to tower "dest" Args: dest(Tower): The tower to which the disk should be moved to """ top = self.disks.pop() dest.add(top) print("Move disk {0} from tower {1} to tower {2}".format( top, self.index, dest.index)) def move_disks(self, n, destination, buffer): """ Move disks from origin to destination Args: n(Integer): Number of disks to move destination(Tower): The destination for the disks buffer(Tower): Helper tower to move disks between origin and destination """ if n > 0: self.move_disks(n - 1, buffer, destination) self.move_top_to(destination) buffer.move_disks(n - 1, destination, self)
class StackWithMin(Stack): """ Stack to store minimum value along with items Attributes: s2(Stack): A stack to keep track of the minimum values """ def __init__(self): super().__init__() self.s2 = Stack() def push(self, item): """ Push item into the stack Args: item(any): Item to push """ if item <= self.min(): self.s2.push(item) super().push(item) def min(self): """ Find the minimum value in the stack """ if self.is_empty(): return sys.maxsize return self.s2.peek() def pop(self): """ Pops an item from the stack Returns: Integer: The popped value """ value = super().pop() if value == self.min(): self.s2.pop() return value
def merge(s1, s2): """ Merge sub-stacks Args: s1(Stack) s2(Stack) Returns: Stack: Merged stack """ work = Stack() while s1.size() > 0 and s2.size() > 0: if s1.peek() > s2.peek(): work.push(s1.pop()) else: work.push(s2.pop()) while s1.size() > 0: work.push(s1.pop()) while s2.size() > 0: work.push(s2.pop()) return work
def sort_stack(s): """ Sort a stack Time complexity: O(N^2) Space complexity: O(N) Args: s(Stack): The stack to sort Returns: Stack: Sorted stack """ res = Stack() while not s.is_empty(): temp = s.pop() while not res.is_empty() and res.peek() > temp: s.push(res.pop()) res.push(temp) return res
def __init__(self, index): # Each tower has a stack size of 5 self.disks = Stack() self.index = index
class MyQueue(object): """ Queue implementation using Stacks Attributes: stack_newest(Stack): Stack to store new incoming elements stack_oldest(Stack): Stack to store elements of stack_newest in reverse order """ def __init__(self): self.stack_newest = Stack() self.stack_oldest = Stack() def size(self): """ Determine the size of the stack Returns: Integer: The size of the stack """ return self.stack_newest.size() + self.stack_oldest.size() def add(self, val): """ Adds new value to queue Args: val(Integer): Value to insert in the queue """ self.stack_newest.push(val) def shift_stacks(self): """ Shift elements from stack_newest to stack_oldest """ if self.stack_oldest.is_empty(): while not self.stack_newest.is_empty(): self.stack_oldest.push(self.stack_newest.pop()) def peek(self): """ Peeks the last element in the stack "stack_oldest" Returns: Integer: The front of the queue """ self.shift_stacks() return self.stack_oldest.peek() def pop(self): """ Pops the first element from the queue Returns: Integer: The popped value """ self.shift_stacks() return self.stack_oldest.pop()
def __init__(self): self.stack_newest = Stack() self.stack_oldest = Stack()
def __init__(self): super().__init__() self.s2 = Stack()
def sort_stack(s): """ Sort a stack Time complexity: O(N^2) Space complexity: O(N) Args: s(Stack): The stack to sort Returns: Stack: Sorted stack """ res = Stack() while not s.is_empty(): temp = s.pop() while not res.is_empty() and res.peek() > temp: s.push(res.pop()) res.push(temp) return res x = Stack() x.push(5) x.push(2) x.push(7) x.push(3) x = sort_stack(x) print(x.pop()) print(x.pop()) print(x.pop()) print(x.pop())
""" work = Stack() while s1.size() > 0 and s2.size() > 0: if s1.peek() > s2.peek(): work.push(s1.pop()) else: work.push(s2.pop()) while s1.size() > 0: work.push(s1.pop()) while s2.size() > 0: work.push(s2.pop()) return work s = Stack() s.push(7) s.push(2) s.push(5) s.push(3) merge_sort(s) print("Size of sorted stack is ", s.size()) print("Element no. {0} is {1} ".format(s.size(), s.pop())) print("Element no. {0} is {1} ".format(s.size(), s.pop())) print("Element no. {0} is {1} ".format(s.size(), s.pop())) print("Element no. {0} is {1} ".format(s.size(), s.pop()))