python-3.x - 如何修复“无”答案并将输入作为参数?
问题描述
我正在参加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。我不确定如何进行
解决方案
推荐阅读
- android - 无法在 recyclerview Android 中启动新活动 onItemClick
- vb.net - DynamicDatabaseFactory.CreateDatabase 连接字符串解析出错
- amazon-dynamodb - 生成不重复的数据并保存在 DynamoDB 中
- python - 在python中将布尔值归因于变量和将字符串归因于变量之间是否存在显着的速度差异?
- powershell - 使用 for 循环复制具有递增值的属性
- javascript - 尝试使用reduce在Javascript中添加路由父URL
- tensorflow - 我可以在图像分类场景中为单个图像设置多个标签吗
- angular - 使用 TranslateService 的服务的 Angular 单元测试
- c# - c#如何从包含格式的字符串中提取格式(例如string =“Printed on {0:dd MMM yyyy}”,我想要dd MMM yyyy
- python - 使用 pyyaml 生成带有覆盖的 yaml 锚点/引用