首页 > 解决方案 > 如何修复“无”答案并将输入作为参数?

问题描述

我正在参加mettl考试,问题是解决括号是否匹配。但我得到的结果是NONE。

我不确定如何将输入作为参数,请帮忙:

我试过改变,如果我提供一个硬编码的输入,它就会接受输入。

'''
# 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))

我希望至少能得到一个正常的 True。我不确定如何进行

标签: python-3.x

解决方案


推荐阅读