Sha256: db6cb42d667ba8302e2c0f17d916d3c9f9a4914d7958e1125417cf53721000ac

Contents?: true

Size: 1.36 KB

Versions: 64

Compression:

Stored size: 1.36 KB

Contents

class BracketPush {
    bracketPairs: Map<string, string>
    expression: string

    constructor(expression: string) {
        this.bracketPairs = new Map<string, string>()
        this.bracketPairs.set('{', '}')
        this.bracketPairs.set('[', ']')
        this.bracketPairs.set('(', ')')

        this.expression = expression
    }

    isPaired(): boolean {
        const bracketStack: string[] = []
        const expressionSplitted = this.expression.split('')

        for (let i = 0; i <= expressionSplitted.length - 1; i++) {
            const element = expressionSplitted[i]

            if (this.isOpeningBracket(element)) {
                bracketStack.push(element)
            } else if (this.isClosingBracket(element)) {
                if (bracketStack === []) {
                    return false
                }

                if (this.bracketPairs.get(String(bracketStack.pop())) !== element) {
                    return false
                }
            }
        }

        return bracketStack.length === 0
    }

    private isOpeningBracket(bracket: string): boolean {
        return this.bracketPairs.has(bracket)
    }

    private isClosingBracket(bracket: string): boolean {
        if (bracket === '}' || bracket === ']' || bracket === ')') {
            return true
        } else {
            return false
        }
    }
}

export default BracketPush

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/bracket-push/bracket-push.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/bracket-push/bracket-push.example.ts