Asif_Anwar

Task-1

Nov 27th, 2021
977
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from array import *
  2.  
  3.  
  4. class StackChar:
  5.  
  6.     # Constructor
  7.     def __init__(self):
  8.         self.maxSize = 50
  9.         array1 = array('u', 'x' * self.maxSize)
  10.         self.stack = array1
  11.         self.top = 0
  12.  
  13.     # Adds element to the Stack
  14.     def push(self, data):
  15.         if self.top >= self.maxSize:
  16.             return "Stack Full!"
  17.         self.stack[self.top] = data
  18.         self.top += 1
  19.         return True
  20.  
  21.     # Removes element from the stack
  22.     def pop(self):
  23.         if self.top <= 0:
  24.             return "Stack Empty!"
  25.         item = self.stack[self.top-1]
  26.         self.top -= 1
  27.         return item
  28.  
  29.     # Returns the peek element of the stack
  30.     def peek(self):
  31.         if self.top <= 0:
  32.             return "Stack Empty!"
  33.         item = self.stack[self.top-1]
  34.         return item
  35.  
  36.     # Size of the stack
  37.     def size(self):
  38.         return self.top
  39.  
  40.  
  41. class StackInt:
  42.  
  43.     # Constructor
  44.     def __init__(self):
  45.         self.maxSize = 50
  46.         array1 = array('i', [0] * self.maxSize)
  47.         self.stack = array1
  48.         self.top = 0
  49.  
  50.     # Adds element to the Stack
  51.     def push(self, data):
  52.         if self.top >= self.maxSize:
  53.             return "Stack Full!"
  54.         self.stack[self.top] = data
  55.         self.top += 1
  56.         return True
  57.  
  58.     # Removes element from the stack
  59.     def pop(self):
  60.         if self.top <= 0:
  61.             return "Stack Empty!"
  62.         item = self.stack[self.top - 1]
  63.         self.top -= 1
  64.         return item
  65.  
  66.     # Return the peek element of the stack
  67.     def peek(self):
  68.         if self.top <= 0:
  69.             return "Stack Empty!"
  70.         item = self.stack[self.top - 1]
  71.         return item
  72.  
  73.     # Size of the stack
  74.     def size(self):
  75.         return self.top
  76.  
  77.  
  78. class CheckBalancedBracket:
  79.     def is_balanced(self, expression):
  80.         print(expression)
  81.         stack = StackChar()
  82.         indexes = StackInt()
  83.         dict = {')': '(', '}': '{', ']': '['}
  84.         index = 0
  85.         for c in expression:
  86.             index += 1
  87.             if c == ')' or c == '}' or c == ']':
  88.                 if dict[c] != stack.peek():
  89.                     print("This expression is NOT correct.")
  90.                     if indexes.peek() != "Stack Empty!":
  91.                         print(f"Error at character #{indexes.peek()}. '{stack.peek()}' - not closed.")
  92.                     else:
  93.                         print(f"Error at character #{index}. '{c}' - not opened.")
  94.                     return
  95.                 else:
  96.                     stack.pop()
  97.                     indexes.pop()
  98.             elif c == '(' or c == '[' or c == '{':
  99.                 stack.push(c)
  100.                 indexes.push(index)
  101.         if stack.size() == 0:
  102.             print("This expression is correct.")
  103.             return
  104.         else:
  105.             while stack.size() > 1:
  106.                 stack.pop()
  107.                 indexes.pop()
  108.             print("This expression is NOT correct.")
  109.             print(f"Error at character #{indexes.peek()}. '{stack.peek()}' - not closed.")
  110.             return
  111.  
  112.  
  113. arithmaticExpression = input("Enter the expression: ")
  114. ans = CheckBalancedBracket()
  115. ans.is_balanced(arithmaticExpression)
RAW Paste Data