-
Notifications
You must be signed in to change notification settings - Fork 0
Brackets check #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Brackets check #68
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| image: Visual Studio 2017 | ||
|
|
||
| image: Visual Studio 2019 | ||
|
|
||
| init: | ||
| - git config --global core.autocrlf true | ||
|
|
||
| environment: | ||
| matrix: | ||
| - solution: Semester4/BracketsChecker/BracketsChecker.sln | ||
|
|
||
| before_build: | ||
| - nuget restore semester2/6.1/HW6T2.sln | ||
| - nuget restore %solution% | ||
|
|
||
| build: | ||
| project: semester2/2.3/2.3.sln | ||
| project: $(solution) | ||
|
|
||
| test_script: | ||
| - dotnet test %solution% |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| <RootNamespace>TestProject1</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsUnit" Version="4.0.1" /> | ||
| <PackageReference Include="nunit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="BracketsCheckerTests.fs" /> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\BracketsChecker\BracketsChecker.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| module TestProject1 | ||
|
|
||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open BracketsChecker | ||
|
|
||
| let cases = | ||
| [ | ||
| "()", true | ||
| "[]", true | ||
| "{}", true | ||
| "", true | ||
| "1", true | ||
| ")(", false | ||
| "(]", false | ||
| "([{[]}])", true | ||
| "[])", false | ||
| "[[]]", true | ||
| "[1]", true | ||
| "([)]", false | ||
| "(123)", true | ||
| "]", false | ||
| "(", false | ||
| "(1)", true | ||
| "[453]", true | ||
| "[23}", false | ||
| ] |> List.map (fun (string, result) -> TestCaseData(string, result)) | ||
|
|
||
| [<Test>] | ||
| [<TestCaseSource("cases")>] | ||
| let bracketsTest str res = | ||
| bracketsChecker str |> should equal res |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module Program = let [<EntryPoint>] main _ = 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BracketsChecker", "BracketsChecker\BracketsChecker.fsproj", "{8751A404-2E55-4DBA-AC3E-6C90466F861F}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BracketsCheckTests", "BracketsCheckTests\BracketsCheckTests.fsproj", "{779136A1-1CC6-402A-A881-99A8E4337227}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {8751A404-2E55-4DBA-AC3E-6C90466F861F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {8751A404-2E55-4DBA-AC3E-6C90466F861F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {8751A404-2E55-4DBA-AC3E-6C90466F861F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {8751A404-2E55-4DBA-AC3E-6C90466F861F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {779136A1-1CC6-402A-A881-99A8E4337227}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {779136A1-1CC6-402A-A881-99A8E4337227}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {779136A1-1CC6-402A-A881-99A8E4337227}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {779136A1-1CC6-402A-A881-99A8E4337227}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| module BracketsChecker | ||
|
|
||
| open System | ||
|
|
||
| /// Сhecks that the parentheses are correctly placed in the expression | ||
| let bracketsChecker (str : String) = | ||
| let isNotBracket (ch : Char) = | ||
| ch <> '[' && ch <> '{' && ch <> '(' && ch <> ']' && ch <> '}' && ch <> ')' | ||
|
|
||
| /// Checks if symbol is a opening bracket | ||
| let isOpenBracket (bracket : Char) = | ||
| bracket = '[' || bracket = '{' || bracket = '(' | ||
|
|
||
| /// Checks if symbols are opening and closing brackets of the same type | ||
| let isMatchingBrackets (first : Char) (second : Char) = | ||
| match first with | ||
| | '(' -> second = ')' | ||
| | '[' -> second = ']' | ||
| | '{' -> second = '}' | ||
| | _ -> true | ||
|
|
||
| /// Checks if brackets in line are correct | ||
| let rec checkLine (str : List<Char>) (memory : List<Char>) = | ||
| if str = [] | ||
| then memory = [] | ||
| else | ||
| let current = List.head str | ||
| if (isNotBracket current) then | ||
| checkLine (List.tail str) (memory) | ||
| elif (isOpenBracket current) then | ||
| checkLine (List.tail str) (current :: memory) | ||
| elif (memory <> [] && isMatchingBrackets (List.head memory) (current)) then | ||
| checkLine (List.tail str) (List.tail memory) | ||
| else false | ||
|
Comment on lines
+24
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Алгоритмически правильно, но технически лучше через match переписать. Вообще, rule of thumb таково, что List.head, List.tail и подобные функции редко встречаются где-то кроме параметров функций высших порядков, поэтому если их много, имеет смысл подумать, не будет ли код в полтора раза короче, если делать match по образцу (тут участвуют два списка, но никто не мешает матчить по паре). |
||
|
|
||
| checkLine (Seq.toList str) [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="BracketsChecker.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В принципе, это всё можно было бы заменить Map-ом и вызывать соответствующие его методы