def reverse_queue(input_que): stack = Stack.Stack() while not input_que.is_empty(): stack.push(input_que.dequeue()) while not stack.is_empty(): input_que.enqueue(stack.pop()) return input_que
def reverse_first_k(input_que, k): stack = Stack.Stack() count = 0 while count < k: stack.push(input_que.dequeue()) count += 1 while not stack.is_empty(): input_que.enqueue(stack.pop()) for i in range(input_que.size - k): input_que.enqueue(input_que.dequeue())
def check_palindrome(input_str): stk = Stack.Stack() palindrome = False for char in input_str: stk.push(char) for char in input_str: if char == stk.pop(): palindrome = True else : palindrome = False return palindrome
def reverse_elements(input_string): """ Algorithm to reverse the elements of the string using stack :param input_string: :return: """ new_stack = Stack.Stack() for char in input_string: new_stack.push(char) while not new_stack.is_empty(): print(new_stack.pop())
def check_consecutive(input_stac): temp_stack = Stack.Stack() while not input_stac.is_empty(): temp_stack.push(input_stac.pop()) while not temp_stack.is_empty(): temp = temp_stack.pop() if not temp_stack.is_empty(): if abs(temp - temp_stack.pop()) != 1: return False else: return True return True
def check_paranthesis(input_string): stack = Stack.Stack() open_braces = '{([' closed_braces = '})]' for element in input_string: if element in open_braces: stack.push(element) elif element in closed_braces: if not stack.is_empty(): poped_element = stack.pop() else: return 'unbalanced paranthesis' if closed_braces.index(element) != open_braces.index( poped_element): return 'Unbalanced paranthesis' if stack.is_empty(): return 'balanced paranthesis' else: return 'unbalanced paranthesis'
def print_reverse_llo(root): """ Complete reversing of the order :param root: :return: """ if not root: return None else: que = Queue.Queue1() stack = Stack.Stack() que.enqueue(root) while not que.is_empty(): node = que.dequeue() if node.get_left(): que.enqueue(node.get_left()) if node.get_right(): que.enqueue(node.get_right()) stack.push(node) while not stack.is_empty(): print(stack.pop().get_data())
def _check_palindrome(input_string): """ Algorithm to check if an input string is a palindrome :param input_string: :return: """ length = len(input_string) stack = Stack.Stack() mid=0 if length%2==0: mid = length//2 else: mid = length//2+1 for i in range(mid): stack.push(input_string[i]) if length%2 ==1: stack.pop() for i in range(mid): if stack.pop() != input_string[mid+i]: return 'not palindrome' else : return 'palindrome'
from Karumanchi.Stack import Stack def check_consecutive(input_stac): temp_stack = Stack.Stack() while not input_stac.is_empty(): temp_stack.push(input_stac.pop()) while not temp_stack.is_empty(): temp = temp_stack.pop() if not temp_stack.is_empty(): if abs(temp - temp_stack.pop()) != 1: return False else: return True return True if __name__ == "__main__": temp_stack = Stack.Stack() temp_stack.push(4) temp_stack.push(5) temp_stack.push(-2) temp_stack.push(-3) temp_stack.push(11) temp_stack.push(10) temp_stack.push(5) temp_stack.push(6) print(check_consecutive(temp_stack))
def __init__(self): self.stack1=Stack.Stack() self.stack2=Stack.Stack() self.size = 0