diff --git a/src/Homework4/BracketChecker.fs b/src/Homework4/BracketChecker.fs new file mode 100644 index 0000000..3553cd9 --- /dev/null +++ b/src/Homework4/BracketChecker.fs @@ -0,0 +1,50 @@ +namespace Homework4 + +module BracketChecker = + type Facing = + | Fore + | Back + + type Bracket = + | Round + | Square + | Curly + + type Symbol = + | Bracket of Bracket * Facing + | Other + + let getSymbolType symbol = + match symbol with + | ')' -> Bracket(Round, Back) + | '(' -> Bracket(Round, Fore) + | ']' -> Bracket(Square, Back) + | '[' -> Bracket(Square, Fore) + | '}' -> Bracket(Curly, Back) + | '{' -> Bracket(Curly, Fore) + | _ -> Other + + let check brackets = + let rec checkRec currentBrackets currentStack = + match currentBrackets, currentStack with + | [], [] -> true + | [], _ -> false + + | h :: tail, [] -> + match getSymbolType h with + | Other -> checkRec tail [] + | Bracket(bracketType, Fore) -> + checkRec tail (bracketType :: currentStack) + | _ -> false + + | h :: tail, stackHead :: stackTail -> + match getSymbolType h with + | Other -> checkRec tail currentStack + | Bracket(bracketType, Fore) -> + checkRec tail (bracketType :: currentStack) + | Bracket(bracketType, Back) when bracketType = stackHead -> + checkRec tail stackTail + | _ -> false + + let charList = brackets |> List.ofSeq + checkRec charList [] diff --git a/src/Homework4/Homework4.fsproj b/src/Homework4/Homework4.fsproj index e831740..c766aad 100644 --- a/src/Homework4/Homework4.fsproj +++ b/src/Homework4/Homework4.fsproj @@ -6,6 +6,7 @@ + diff --git a/tests/Homeworks.Tests/BracketChecker.fs b/tests/Homeworks.Tests/BracketChecker.fs new file mode 100644 index 0000000..dfb3167 --- /dev/null +++ b/tests/Homeworks.Tests/BracketChecker.fs @@ -0,0 +1,37 @@ +module Homeworks.Tests.BracketChecker + +open Expecto +open Homework4.BracketChecker + +let getName brackets = + $"test on {brackets}" + +let createTest brackets expected = + test (getName brackets) { + let actual = check brackets + Expect.equal actual expected "Results must be the same" + } + +let generalTests = + [ createTest + "(()([][[{{(aadf) fdf} fdfa}]] klne)(){ rgqie}{} rgqrkqgie [][[r qiermgiqegi miemrgie{()()(({}))}]])" + true + + createTest "" true + + createTest "({([])})(2+3)() * ()(){}{kgkwemrgkmqe}{}{}[][qrgqerger][][]" true + + createTest "(]" false + + createTest + "(()([][[{{(aadf) fdf} fdfa}]] klne)() rgqie}{} rgqrkqgie [][[r qiermgiqegi miemrgie{()()(({}))}]])" + false + + createTest + "(()([][[{{(aadf) fdf} fdfa}]] klne)(){ rgqie}{} rgqrkqgie [[[r qiermgiqegi miemrgie{()()(({}))}]])" + false + + createTest + "(()([][[{{(aadf) fdf} fdfa}]] klne)(){ rgqie}{} rgqrkqgie [[[r qiermgiqegi miemrgie{())(({}))}]])" + false ] + |> testList "general tests" diff --git a/tests/Homeworks.Tests/Homeworks.Tests.fsproj b/tests/Homeworks.Tests/Homeworks.Tests.fsproj index b8f52cd..8dd7bf7 100644 --- a/tests/Homeworks.Tests/Homeworks.Tests.fsproj +++ b/tests/Homeworks.Tests/Homeworks.Tests.fsproj @@ -7,10 +7,12 @@ Homeworks.Tests + + diff --git a/tests/Homeworks.Tests/Main.fs b/tests/Homeworks.Tests/Main.fs index 4264e0b..00254a7 100644 --- a/tests/Homeworks.Tests/Main.fs +++ b/tests/Homeworks.Tests/Main.fs @@ -1,9 +1,9 @@ open Expecto +open Homeworks.Tests [] -let allTests = testList "all Tests" [] +let allTests = testList "all Tests" [ BracketChecker.generalTests ] [] let main argv = - allTests - |> runTestsWithCLIArgs [] argv + allTests |> runTestsWithCLIArgs [] argv