Linked Questions

Popular Questions

How to fix "None" answer and Take inputs as arguments?

Asked by At

I'm giving mettl exam and the question was for solving the parenthesis are matching or not.But all I'm getting as result is NONE.

I'm not sure how to take the input as argument, please help out:

I've tried changing, it is taking the input if I provide a hard coded one.

'''
# these are the metll instructions
class UserMainCode(object):
    @classmethod
    def areParenthesisBalanced(cls, input1):
        '''
        input1 : string

        Expected return type : string
        '''
        # Read only region end
        # Write code here
        pass
'''
# this is the code I tried
class Stack():
    def __init__(self):
        self.items=[]
    def push(self,item):
        self.items.append(item)
    def is_empty(self):
        return self.items == []
    def pop(self):
        return self.items.pop()
    def show_me(self):
        return self.items
    def peek(self):
        if not self.is_empty():
            return self.items[-1]

input1=[]     
def areParenthesisBalanced(input1):
    s=Stack()
    is_balanced=True
    index=0
def is_match(p1,p2):
    if p1=="(" and p2==")":
        return True
    elif p1=="[" and p2=="]":
        return True
    elif p1=="{" and p2=="}":
        return True
    else:
        return False

    while index< len(input1) and is_balanced:
        paren=input1[index]
        if paren in"({[":
            s.push(paren)
        else:
            if s.is_empty():
                is_balanced=False
            else:
                top = s.pop()
                if not is_match(top,paren):
                    is_balanced=False
        index+=1
        if s.is_empty() and is_balanced:
            return True
        else:
            return False


print (areParenthesisBalanced(input1))

I was hoping to atleast get a normal True. I'm not sure how to proceed

Related Questions