Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions appveyor.yml
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
1 change: 1 addition & 0 deletions semester4/BracketsChecker/BracketsCheckTests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Program = let [<EntryPoint>] main _ = 0
22 changes: 22 additions & 0 deletions semester4/BracketsChecker/BracketsChecker.sln
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
36 changes: 36 additions & 0 deletions semester4/BracketsChecker/BracketsChecker/BracketsChecker.fs
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
Comment on lines +7 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В принципе, это всё можно было бы заменить Map-ом и вызывать соответствующие его методы


/// 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

Choose a reason for hiding this comment

The 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) []
11 changes: 11 additions & 0 deletions semester4/BracketsChecker/BracketsChecker/BracketsChecker.fsproj
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>