Check the balance of parenthesis in Python

    0

    0

    CrypticSai-08

    def isbalanced(s):
        c= 0
        ans=False
        for i in s:
            if i == β€œ(β€œ:
                c += 1
            elif i == β€œ)”:
                c-= 1
            if c < 0:
                return ans
        if c==0:
            return not ans
        return ans
    s=input(β€œEnter a string of brackets: β€œ) 
    print(β€œGiven string is balanced :”,isbalanced(s))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.