How to resolve the algorithm Balanced brackets step by step in the Groovy programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Balanced brackets step by step in the Groovy programming language

Table of Contents

Problem Statement

Task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Balanced brackets step by step in the Groovy programming language

Source code in the groovy programming language

def random = new Random()

def factorial = { (it > 1) ? (2..it).inject(1) { i, j -> i*j } : 1 }
 
def makePermutation;
makePermutation = { string, i ->
    def n = string.size()
    if (n < 2) return string
    def fact = factorial(n-1)
    assert i < fact*n
 
    def index = i.intdiv(fact)
    string[index] + makePermutation(string[0..<index] + string[(index+1)..<n], i % fact)
}

def randomBrackets = { n ->
    if (n == 0) return ''
    def base = '['*n + ']'*n
    def p = random.nextInt(factorial(n*2))
    makePermutation(base, p)
}


boolean balancedBrackets(String brackets, int depth=0) {
    if (brackets == null || brackets.empty) return depth == 0
    switch (brackets[0]) {
        case '[': 
            return brackets.size() > 1  &&  balancedBrackets(brackets[1..-1], depth + 1)
        case ']':
            return depth > 0  &&  (brackets.size() == 1  ||  balancedBrackets(brackets[1..-1], depth - 1))
        default:
            return brackets.size() == 1  ?  depth == 0  :  balancedBrackets(brackets[1..-1], depth)
    }
}


Set brackets = []
(0..100).each {
    (0..8).each { r ->
        brackets << randomBrackets(r)
    }
}

brackets.sort { a, b ->
    a.size() <=> b.size() ?: a <=> b
} .each {
    def bal = balancedBrackets(it) ? "balanced:   " : "unbalanced: "
    println "${bal} ${it}"
}


  

You may also check:How to resolve the algorithm Number reversal game step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the Perl programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Quackery programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Groovy programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the ooRexx programming language