Created
January 25, 2022 18:22
-
-
Save mdpabel/0e4350cb8e674df8a24c9d29bb686bd2 to your computer and use it in GitHub Desktop.
stack queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyQueue(object): | |
def __init__(self): | |
self.stack1 = [] | |
self.stack2 = [] | |
def peek(self): | |
if not self.stack2: | |
while self.stack1: | |
self.stack2.append(self.stack1.pop()) | |
return self.stack2[-1] | |
def pop(self): | |
if not self.stack2: | |
while self.stack1: | |
self.stack2.append(self.stack1.pop()) | |
return self.stack2.pop() | |
def put(self, value): | |
self.stack1.append(value) | |
queue = MyQueue() | |
t = int(input()) | |
for line in range(t): | |
values = map(int, input().split()) | |
values = list(values) | |
if values[0] == 1: | |
queue.put(values[1]) | |
elif values[0] == 2: | |
queue.pop() | |
else: | |
print(queue.peek()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# | |
# Complete the 'isBalanced' function below. | |
# | |
# The function is expected to return a STRING. | |
# The function accepts STRING s as parameter. | |
# | |
def isBalanced(s): | |
# Write your code here | |
stack = [] | |
map = { | |
")" : "(", | |
"}" : "{", | |
"]" : "[" | |
} | |
if len(s) % 2 == 1: | |
return "NO" | |
for item in s: | |
if item == "(" or item == "{" or item == "[": | |
stack.append(item) | |
elif len(stack) == 0: | |
return "NO" | |
elif len(stack) and (stack.pop() != map[item]): | |
return "NO" | |
if len(stack) > 0: | |
return "NO" | |
else: | |
return "YES" | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
t = int(input().strip()) | |
for t_itr in range(t): | |
s = input() | |
result = isBalanced(s) | |
fptr.write(result + '\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment