Advertisement
Asif_Anwar

Task-2

Nov 27th, 2021
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. class Node:
  2.     def __init__(self, data):
  3.         self.data = data
  4.         self.next = None
  5.  
  6.  
  7. class Stack:
  8.  
  9.     # Constructor
  10.     def __init__(self):
  11.         self.top = 0
  12.         self.head = None
  13.  
  14.     # Adds element to the Stack
  15.     def push(self, data):
  16.         self.top += 1
  17.         if self.head == None:
  18.             self.head = Node(data)
  19.         else:
  20.             newNode = Node(data)
  21.             newNode.next = self.head
  22.             self.head = newNode
  23.  
  24.     # Removes element from the stack
  25.     def pop(self):
  26.         if self.head == None:
  27.             return "Stack Empty!"
  28.         else:
  29.             self.top -= 1
  30.             item = self.head.data
  31.             self.head = self.head.next
  32.             return item
  33.  
  34.     # Returns the peek element of the stack
  35.     def peek(self):
  36.         if self.head == None:
  37.             return "Stack Empty!"
  38.         else:
  39.             item = self.head.data
  40.             return item
  41.  
  42.     # Checks if the stack is empty or not
  43.     def is_empty(self):
  44.         if self.head == None:
  45.             return True
  46.         else:
  47.             return False
  48.  
  49.     def size(self):
  50.         return self.top
  51.  
  52. class CheckBalancedBracket:
  53.     def is_balanced(self, expression):
  54.         print(expression)
  55.         stack = Stack()
  56.         indexes = Stack()
  57.         dict = {')': '(', '}': '{', ']': '['}
  58.         index = 0
  59.         for c in expression:
  60.             index += 1
  61.             if c == ')' or c == '}' or c == ']':
  62.                 if dict[c] != stack.peek():
  63.                     print("This expression is NOT correct.")
  64.                     if indexes.peek() != "Stack Empty!":
  65.                         print(f"Error at character #{indexes.peek()}. '{stack.peek()}' - not closed.")
  66.                     else:
  67.                         print(f"Error at character #{index}. '{c}' - not opened.")
  68.                     return
  69.                 else:
  70.                     stack.pop()
  71.                     indexes.pop()
  72.             elif c == '(' or c == '[' or c == '{':
  73.                 stack.push(c)
  74.                 indexes.push(index)
  75.         if stack.is_empty():
  76.             print("This expression is correct.")
  77.             return
  78.         else:
  79.             while stack.size() > 1:
  80.                 stack.pop()
  81.                 indexes.pop()
  82.             print("This expression is NOT correct.")
  83.             print(f"Error at character #{indexes.peek()}. '{stack.peek()}' - not closed.")
  84.             return
  85.  
  86.  
  87.  
  88.  
  89. arithmaticExpression = input("Enter the expression: ")
  90. ans = CheckBalancedBracket()
  91. ans.is_balanced(arithmaticExpression)
  92.  
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement