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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Balanced brackets step by step in the Excel 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 Excel programming language

Source code in the excel programming language

bracketReport
=LAMBDA(bracketPair,
    LAMBDA(s,
        LET(
            depths, SCANLCOLS(
                LAMBDA(depth, charDelta, 
                    depth + charDelta
                )
            )(0)(
                codesFromBrackets(bracketPair)(
                    MID(s, 
                        SEQUENCE(1, LEN(s), 1, 1),
                        1
                    )
                )
            ),
            overClosePosn, IFNA(MATCH(-1, depths, 0), 0),
        
            IF(0 <> overClosePosn,
                "Overclosed at char " & (overClosePosn - 1),
                IF(0 <> LASTCOL(depths),
                    "Underclosed by end",
                    "OK"
                )
            )
        )
    )
)


testRandomBrackets
=LAMBDA(bracketPair,
    LAMBDA(n,
        LET(
            sample, CONCAT(
                bracketFromCode(bracketPair)(
                    RANDARRAY(1, n, -1, 1, TRUE)
                )
            ),

            APPENDCOLS(sample)(
                bracketReport(bracketPair)(sample)
            )
        )
    )
)


bracketFromCode
=LAMBDA(bracketPairString,
    LAMBDA(c,
        IF(0 <> c,
            IF(0 < c,
                MID(bracketPairString, 1, 1),
                MID(bracketPairString, 2, 1)
            ),
            "x"
        )
    )
)


codesFromBrackets
=LAMBDA(bracketPair,
    LAMBDA(s,
        LAMBDA(c,
            LET(
                posn, IFERROR(
                    FIND(c, bracketPair),
                    0
                ),

                rem, "0 for non-bracket chars, (1, -1) for (open, close)",
                IF(0 <> posn,
                    IF(1 < posn, -1, 1),
                    0
                )
            )
        )(
             MID(s,
                SEQUENCE(1, LEN(s), 1, 1),
                1
            )
        )
    )
)


APPENDCOLS
=LAMBDA(xs,
    LAMBDA(ys,
        LET(
            nx, COLUMNS(xs),
            colIndexes, SEQUENCE(1, nx + COLUMNS(ys)),
            rowIndexes, SEQUENCE(MAX(ROWS(xs), ROWS(ys))),

            IFERROR(
                IF(nx < colIndexes,
                    INDEX(ys, rowIndexes, colIndexes - nx),
                    INDEX(xs, rowIndexes, colIndexes)
                ),
                NA()
            )
        )
    )
)


CHARSROW
=LAMBDA(s,
    MID(s,
        SEQUENCE(1, LEN(s), 1, 1),
        1
    )
)


HEADCOL
=LAMBDA(xs,
    LET(REM, "The first item of each column in xs",

        INDEX(xs, 1, SEQUENCE(1, COLUMNS(xs)))
    )
)


HEADROW
=LAMBDA(xs,
    LET(REM, "The first item of each row in xs",

        INDEX(
            xs,
            SEQUENCE(ROWS(xs)),
            SEQUENCE(1, 1)
        )
    )
)


LASTCOL
=LAMBDA(xs,
    INDEX(
        xs,
        SEQUENCE(ROWS(xs), 1, 1, 1),
        COLUMNS(xs)
    )
)


SCANLCOLS
=LAMBDA(op,
    LAMBDA(a,
        LAMBDA(xs,
            APPENDCOLS(a)(
                LET(
                    b, op(a, HEADROW(xs)),

                    IF(2 > COLUMNS(xs),
                        b,
                        SCANLCOLS(op)(b)(
                            TAILROW(xs)
                        )
                    )
                )
            )
        )
    )
)


TAILCOL
=LAMBDA(cols,
    LET(REM, "The tail of each column in the grid.",

        INDEX(
            cols,
            SEQUENCE(ROWS(cols) - 1, 1, 2, 1),
            SEQUENCE(1, COLUMNS(cols))
        )
    )
)


TAILROW
=LAMBDA(xs,
    LET(REM,"The tail of each row in the grid",
        n, COLUMNS(xs) - 1,

        IF(0 < n,
            INDEX(
                xs,
                SEQUENCE(ROWS(xs), 1, 1, 1),
                SEQUENCE(1, n, 2, 1)
            ),
            NA()
        )
    )
)


  

You may also check:How to resolve the algorithm Chaos game step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the Pascal programming language
You may also check:How to resolve the algorithm Left factorials step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the PARI/GP programming language