为了账号安全,请及时绑定邮箱和手机立即绑定

创建一个函数并从中调用两个不同的结果

创建一个函数并从中调用两个不同的结果

皈依舞 2024-01-11 16:21:38
我正在尝试检查匹配方程的平衡。下面的代码是我目前拥有的。#Creating the math equation the use check_html on.e = "10 - (3 + (2+1)*8)"  def check_html(html_string):   d = Stack()balanced = Truefor symbol in html_string: if symbol == "(":    d.push(symbol)elif symbol == ")":    if d.is_empty():        balanced = False    else:        d.pop()if not d.is_empty:balanced = Falsestr1 = "10 - (3 + (2+1)*8)"str2 = "10 - (3 + (2+1)*8))"print ("is the first valid?", check_html(str1))print ("is the second valid?", check_html(str2))print("Is it balanced? ", balanced) 这段代码的输出是is the first valid? Noneis the second valid? NoneIs it balanced?  True应该说第一个是 TRUE,第二个是 FALSE。我现在做错了什么。
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

你必须使用html_string而不是e在线


for symbol in html_string:  # `html_string` instead` of `e`

就这样。


顺便说一句:最好在Stack()函数内部创建和使用 - 因此当您使用新字符串运行函数时,它将创建新的空堆栈。


编辑:完整功能


# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

               d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    print("Is it balanced? ", balanced)


# --- main ---


e = "10 - (3 + (2+1)*8)"

check_html(e)

check_html( e+')' )

如果你想check_html()在print()then 函数中使用,你应该return balanced使用of print()


# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

               d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    return balanced


# --- main ---


e = "10 - (3 + (2+1)*8)"

print("Is it balanced? ", check_html(e) )

print("Is it balanced? ", check_html( e+')' ) )

编辑:带有自己的类的完整工作示例Stack


# --- classes ---


class Stack:


    def __init__(self):

        self.data = []


    def push(self, item):

        self.data.append(item)


    def pop(self):

        return self.data.pop(-1)


    def is_empty(self):

        return len(self.data) == 0



# --- functions ---


def check_html(html_string):

    d = Stack()


    balanced = True


    for symbol in html_string: 

        if symbol == "(":

            d.push(symbol)

        elif symbol == ")":

            if d.is_empty():

                balanced = False

            else:

                d.pop()


    if not d.is_empty(): # you forgot `()

        balanced = False


    return balanced


# --- main ---


e = "10 - (3 + (2+1)*8)"

print("Is it balanced?", check_html(e) )


#print("Is it balanced?", check_html(e + ')') )

f = e + ')'

print("Is it balanced?", check_html(f) )


#print("Is it balanced?", check_html('('+e+')') )

f = '(' + e + ')'

print("Is it balanced?", check_html(f) )


#print("Is it balanced?", check_html('('+e) )

f = '(' + e

print("Is it balanced?", check_html(f) )


查看完整回答
反对 回复 2024-01-11
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信