0

I'm trying to run a leetcode solution through a debugger so I can see how the solution works. Its number 20 on parenthesis. This is what I've plugged into the debugger..am I missing something?

screencapture of what I tried

class Solution:
    def isValid(self, s: str) -> bool:
        parenthesis = {"}":"{", 
        "]":"[", 
        ")":"("
        }
        stack = []
        for i in s:
           if i in parenthesis:
               if not stack or paren[i] != stack[-1]: 
                   return False
        
               else:
                    stack.pop()
           else:
                stack.append(i)
        return stack == []

Solution.isValid("{[]}")

I've tried making an instance of the class but I still get the same issue.

  • `Solution.isValid("{[]}")` should be `Solution().isValid("{[]}")`. Since it is a class method, so you need to call the `isValid()` method on a class instance. – Jay Dec 21 '22 at 06:07
  • Welcome to Stack Overflow. ```isValid()``` is a method and so you need to instantiate ```Solution``` before using it. – ewokx Dec 21 '22 at 06:07

2 Answers2

0

Creating an Object a of a Class Solution

Note you need to use print statment for the output. without print statement your console will show nothing.

class Solution:
    def isValid(self, s: str) -> bool:
        parenthesis = {"}":"{", 
        "]":"[", 
        ")":"("
        }
        stack = []
        for i in s:
           if i in parenthesis:
               if not stack or parenthesis[i] != stack[-1]: 
                   return False
               else:
                    stack.pop()
           else:
                stack.append(i)
        return stack == []

a=Solution()
print(a.isValid("{[]}"))

Output:-

True
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
0

I think you should create the object of a class first.

sol = Solution()
print(sol.isValid("{[]}")
ByUnal
  • 90
  • 1
  • 8