• <i id='jkR4X'><tr id='jkR4X'><dt id='jkR4X'><q id='jkR4X'><span id='jkR4X'><b id='jkR4X'><form id='jkR4X'><ins id='jkR4X'></ins><ul id='jkR4X'></ul><sub id='jkR4X'></sub></form><legend id='jkR4X'></legend><bdo id='jkR4X'><pre id='jkR4X'><center id='jkR4X'></center></pre></bdo></b><th id='jkR4X'></th></span></q></dt></tr></i><div id='jkR4X'><tfoot id='jkR4X'></tfoot><dl id='jkR4X'><fieldset id='jkR4X'></fieldset></dl></div>

      <small id='jkR4X'></small><noframes id='jkR4X'>

    1. <legend id='jkR4X'><style id='jkR4X'><dir id='jkR4X'><q id='jkR4X'></q></dir></style></legend>
      • <bdo id='jkR4X'></bdo><ul id='jkR4X'></ul>

      1. <tfoot id='jkR4X'></tfoot>

        将迭代转化为递归

        Turning iteration into recursion(将迭代转化为递归)
        <i id='lB9xp'><tr id='lB9xp'><dt id='lB9xp'><q id='lB9xp'><span id='lB9xp'><b id='lB9xp'><form id='lB9xp'><ins id='lB9xp'></ins><ul id='lB9xp'></ul><sub id='lB9xp'></sub></form><legend id='lB9xp'></legend><bdo id='lB9xp'><pre id='lB9xp'><center id='lB9xp'></center></pre></bdo></b><th id='lB9xp'></th></span></q></dt></tr></i><div id='lB9xp'><tfoot id='lB9xp'></tfoot><dl id='lB9xp'><fieldset id='lB9xp'></fieldset></dl></div>

              <tbody id='lB9xp'></tbody>
            <legend id='lB9xp'><style id='lB9xp'><dir id='lB9xp'><q id='lB9xp'></q></dir></style></legend>

            <small id='lB9xp'></small><noframes id='lB9xp'>

              <bdo id='lB9xp'></bdo><ul id='lB9xp'></ul>
              <tfoot id='lB9xp'></tfoot>

                  本文介绍了将迭代转化为递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想检查用户输入的string是否有均衡数量的()

                  I want to check if the string user entered has a balanced amount of ( and )'s

                  例如.()( 不平衡(()) 平衡

                  def check(string):
                  
                          counter=0
                          string=string.replace(" ","")
                  
                          if string[0] is "(":
                  
                             for x in string:
                                  if x is "(":
                                          counter=counter+1
                                  elif x is ")":
                                          counter=counter-1
                  
                             if counter1 is 0:
                                  print("Balanced")
                             else:
                                  print("Unbalanced")
                          else:
                                  print ("Unbalanced")
                  

                  所以这行得通,但是我如何通过递归解决这个问题?我试图思考每次递归调用它时如何使变量减小,一旦它为0,stop.s

                  so this works, but how do I solve this problem with recursion? I am trying to think how I can make a variable decrease each time i call it recursively and once it's 0, stop.s

                  推荐答案

                  算法的直接等效转换如下所示:

                  A direct, equivalent conversion of the algorithm would look like this:

                  def check(string, counter=0):
                    if not string:
                      return "Balanced" if counter == 0 else "Unbalanced"
                    elif counter < 0:
                      return "Unbalanced"
                    elif string[0] == "(":
                      return check(string[1:], counter+1)
                    elif string[0] == ")":
                      return check(string[1:], counter-1)
                    else:
                      return check(string[1:], counter)
                  

                  像这样使用它:

                  check("(())")
                  => "Balanced"
                  
                  check(")(")
                  => "Unbalanced"
                  

                  请注意,由于 elif counter <,上述算法考虑了右括号出现在 相应的左括号之前的情况.0 条件 - 因此修复了原始代码中存在的问题.

                  Notice that the above algorithm takes into account cases where the closing parenthesis appears before the corresponding opening parenthesis, thanks to the elif counter < 0 condition - hence fixing a problem that was present in the original code.

                  这篇关于将迭代转化为递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                      <i id='JrRef'><tr id='JrRef'><dt id='JrRef'><q id='JrRef'><span id='JrRef'><b id='JrRef'><form id='JrRef'><ins id='JrRef'></ins><ul id='JrRef'></ul><sub id='JrRef'></sub></form><legend id='JrRef'></legend><bdo id='JrRef'><pre id='JrRef'><center id='JrRef'></center></pre></bdo></b><th id='JrRef'></th></span></q></dt></tr></i><div id='JrRef'><tfoot id='JrRef'></tfoot><dl id='JrRef'><fieldset id='JrRef'></fieldset></dl></div>
                          <tbody id='JrRef'></tbody>
                        <legend id='JrRef'><style id='JrRef'><dir id='JrRef'><q id='JrRef'></q></dir></style></legend><tfoot id='JrRef'></tfoot>
                      1. <small id='JrRef'></small><noframes id='JrRef'>

                          <bdo id='JrRef'></bdo><ul id='JrRef'></ul>