Sha256: 0153861203a732f71c5149975e45dd29ad10f64a26858993b6509972c236888b

Contents?: true

Size: 1.06 KB

Versions: 26

Compression:

Stored size: 1.06 KB

Contents

module BracketPush exposing (isPaired)

import Dict exposing (Dict)
import List
import String


isPaired : String -> Bool
isPaired input =
    let
        onlyBrackets =
            String.filter (flip List.member bracketList)

        tryMatching bracket visited =
            case visited of
                [] ->
                    bracket :: visited

                top :: visited ->
                    if isPairMatch top bracket then
                        visited
                    else
                        bracket :: top :: visited

        isPairMatch prev current =
            case Dict.get prev bracketDict of
                Nothing ->
                    False

                Just rightBracket ->
                    current == rightBracket
    in
    input
        |> onlyBrackets
        |> String.foldl tryMatching []
        |> List.isEmpty


bracketList : List Char
bracketList =
    [ '{', '}', '[', ']', '(', ')' ]


bracketDict : Dict Char Char
bracketDict =
    Dict.fromList
        [ ( '{', '}' )
        , ( '[', ']' )
        , ( '(', ')' )
        ]

Version data entries

26 entries across 26 versions & 1 rubygems

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