File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Stack :
2
+ def __init__ (self ):
3
+ self .items = []
4
+ def push (self , value ):
5
+ self .items .append (value )
6
+ def pop (self ):
7
+ return self .items .pop ()
8
+ def size (self ):
9
+ return len (self .items )
10
+ def is_empty (self ):
11
+ return len (self .items ) == 0
12
+
13
+ def equation_checker (equation ):
14
+ stack = Stack ()
15
+ for letter in equation :
16
+ if len (equation ) == 0 :
17
+ print ("Pass an equaltion" )
18
+ return False
19
+ if letter == "(" :
20
+ stack .push ("(" )
21
+ elif letter == ")" :
22
+ if stack .size () == 0 :
23
+ print ("Unbalanced equation" )
24
+ return False
25
+ stack .pop ()
26
+ if (stack .size () == 0 ):
27
+ print ("Balanced equation" )
28
+ return True
29
+ else :
30
+ print ("Unbalanced equation" )
31
+ return False
32
+
33
+ print ("Pass" if (equation_checker ('((3^2 + 8)*(5/2))/(2+6)' )) else "Fail" )
34
+ print ("Pass" if not (equation_checker ('((3^2 + 8)*(5/2))/(2+6))' )) else "Fail" )
You can’t perform that action at this time.
0 commit comments