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
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.