首页 > 编程语言 >(python)代码学习||2024.2.4||题目是codewars的【 All Balanced Parentheses】

(python)代码学习||2024.2.4||题目是codewars的【 All Balanced Parentheses】

时间:2024-02-06 11:56:36浏览次数:50  
标签:2024.2 Parentheses python current add right result stack left

题目链接:https://www.codewars.com/kata/5426d7a2c2c7784365000783/python

def balanced_parens(n):
    '''
    To construct all the possible strings with n pairs of balanced parentheses 
    this function makes use of a stack of items with the following structure:
        (current, left, right)
    Where:
        current is the string being constructed
        left is the count of '(' remaining
        right is the count of  ')' remaining
    '''
    stack = [('', n, 0)]
    result = []
    
    # Loop until we run out of items in the stack
    while stack:
        current, left, right = stack.pop()
        
        # if no '(' or ')' left to add, add current to the result
        if left == 0 and right == 0:
            result.append(current)
            
        # if we can, add a '(' and return to the stack
        if left > 0:
            stack.append((current + '(', left - 1, right + 1))
            
        # if we can, add a ')' and return to the stack    
        if right > 0:
            stack.append((current + ')', left, right - 1))
            
    return result

只有在放置了'('后才能放置')',因此stack中初始的元素('',n,0)中,n表示可放置的(有n个,0表示可放置的')'有0个。''表示当前以组成的字符串

标签:2024.2,Parentheses,python,current,add,right,result,stack,left
From: https://www.cnblogs.com/l25428455/p/18007012

相关文章

  • python性能分析line_profiler
    在编程世界中,效率是王道。对于Python开发者来说,line_profiler是一把锐利的剑,能够深入代码的每一行,找出性能瓶颈。今天,就让我们一起深入探索line_profiler,学习如何用它为你的Python程序注入强心剂,让代码效率飞跃。line_profiler:性能分析的利器line_profiler是一个Python工具,专......
  • Iron Python中使用NLTK库
    因为我是程序员,所以会写各种语言的爬虫模版,对于使用NLTK库也是有很的经验值得大家参考的。其实总的来说,NLTK是一个功能强大的NLP工具包,为研究人员和开发者提供了丰富的功能和资源,用于处理和分析文本数据。使用非常方便,而且通俗易懂,今天我将例举一些问题以供大家参考。1、问题背景......
  • python爬虫爬取豆瓣电影top250并写入Excel中
    importrequestsimportreimportopenpyxl#创建工作表wb=openpyxl.Workbook()ws=wb.active#调整列距forletterin['B','C']:ws.column_dimensions[letter].width=66#发送网络请求headers={"User-Agent":'Mozilla/5.0(WindowsNT10.0;Win64;x64)......
  • python发送、接收exchange邮件
    导包importdatetimefrompathlibimportPathimportpytzfromexchangelibimportConfiguration,Account,DELEGATE,Q,Credentials,HTMLBody,Message,FileAttachmentfromexchangelib.protocolimportCachingProtocol连接邮箱server='example.com'do......
  • 打造个性化日历:Python编程实现,选择适合你的方式!
    在本文中,我们将使用Python编写一个简单的日历程序。虽然市面上已经存在现成的日历功能,并且有第三方库可以直接调用实现,但我们仍然希望通过自己编写日历程序来引出我认为好用的日历实现。希望这篇文章能够对你有所帮助。在Python官方文档中,我们可以找到一个名为"calendar"的模块,它......
  • ML-Agents Python包安装
    Unity的机器学习工具包ML-Agents还是挺好用的,但是其Python后端在安装的过程中会出一些问题,在这里记录一下。为了方便多Python环境管理,我在搭建环境的时候使用了Anaconda包管理器。目前ML-Agents支持的Python版本为3.10.12,版本过高或过低都可能会缺少对应的依赖。打开一个PowerShe......
  • Python中利用all()来优化减少判断的代码
    ​ Python中,all()函数是一个非常实用的内置函数,用于检查可迭代对象中的所有元素是否都满足某个条件。当你需要对多个条件进行逻辑与(AND)操作时,使用all()可以使代码更加简洁和可读。 参考文档:Python中利用all()来优化减少判断的代码-CJavaPy1、使用all()减少判断要检查......
  • Python 机器学习 特征预处理
    1、缩放特征(FeatureScaling)特征预处理是一个重要的步骤,而特征缩放(FeatureScaling)是其中的一个关键环节。特征缩放通常用于标准化数据集中各个特征的范围,使它们在相似的尺度上。这一步骤对于许多机器学习算法特别重要,尤其是那些基于距离的算法(如K-近邻)和梯度下降法(如线性回归、......
  • 2024.2.5寒假每日总结27
    LeetCode跳跃游戏VI1696.跳跃游戏VI-力扣(LeetCode)题目描述给你一个下标从0开始的整数数组nums和一个整数k。一开始你在下标0处。每一步,你最多可以往前跳k步,但你不能跳出数组的边界。也就是说,你可以从下标i跳到[i+1,min(n-1,i+k)]包含两个端点的任......
  • Python文本转语音库:pyttsx3 初识
    1.安装pipinstallpyttsx32.示例#coding=utf-8importpyttsx3text="""在这个例子中,使用三引号可以创建多行字符串,换行符会自动包含在字符串中。请注意,在这些方法中,字符串的换行拼接可以根据需要进行布局,以增强代码的可读性和可维护性。"""engine=pyttsx3.init()......