-
Notifications
You must be signed in to change notification settings - Fork 0
Создала pull request HW4 #4
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: main
Are you sure you want to change the base?
Changes from all commits
649a478
5feb2e2
526042b
6efabf5
48aef1e
5c92b22
e513857
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>true</GenerateProgramFile> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="BracketSequenceTest.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
| <PackageReference Include="NUnit" Version="4.2.2" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="4.3.0"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="FsUnit" Version="6.0.1"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\BracketSequence\BracketSequence.fsproj" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 25.0.1706.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BracketSequence.Tests", "BracketSequence.Tests.fsproj", "{2B02A4C0-83AC-48BE-BDAD-C3935BA442E6}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {2B02A4C0-83AC-48BE-BDAD-C3935BA442E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {2B02A4C0-83AC-48BE-BDAD-C3935BA442E6}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {2B02A4C0-83AC-48BE-BDAD-C3935BA442E6}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {2B02A4C0-83AC-48BE-BDAD-C3935BA442E6}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {7EFC385E-DD21-4D37-9CB3-4FF735185470} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| module BracketSequence.Tests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open BracketSequenceChecker | ||
|
|
||
| [<Test>] | ||
| let ``Function checks correctly sequence of brackets`` () = | ||
| let brSeqCorr = "(({})[])" | ||
| let brSeqIncorr = "((({})[])" | ||
|
|
||
| (isCorrectBracketSequence brSeqCorr, isCorrectBracketSequence brSeqIncorr) | ||
| |> should equal (true, false) | ||
|
|
||
| [<Test>] | ||
| let ``Function checks correctly sequence of brackets with other symbols`` () = | ||
| let brSeq = "(t({}d)[]g)" | ||
|
|
||
| isCorrectBracketSequence brSeq |> should be True | ||
|
|
||
| [<Test>] | ||
| let ``Function checks correctly empty string`` () = | ||
| isCorrectBracketSequence "" |> should be True | ||
|
|
||
| [<Test>] | ||
| let ``Function checks correctly string with no braskets`` () = | ||
| let brSeq = "abc" | ||
|
|
||
| isCorrectBracketSequence brSeq |> should be True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace BracketSequence | ||
|
|
||
| module BracketSequenceChecker = | ||
| let bracketList = Map [(')', '('); ('}', '{'); (']', '[')] | ||
|
|
||
| let isOpen (bracket: char) = | ||
| bracket = '(' || bracket = '{' || bracket = '[' | ||
|
|
||
| let isClose (bracket: char) = | ||
| bracket = ']' || bracket = ')' || bracket = '}' | ||
|
Comment on lines
+6
to
+10
Contributor
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. Есть же bracketList (который, правда, уже не List). А то если хочется новый вид скобок, надо будет тут поправить и там поправить, что неудобно |
||
|
|
||
| let getRelativeOpen (bracket: char) = bracketList.[bracket] | ||
|
|
||
| let isCorrectBracketSequence (bracketString: string) = | ||
| let rec internalBracketSequenceChecker charList bracketsStack = | ||
| match charList with | ||
| | fstChar :: tail -> | ||
| if isOpen fstChar then | ||
| internalBracketSequenceChecker tail (fstChar :: bracketsStack) | ||
| elif isClose fstChar then | ||
| match bracketsStack with | ||
| | lastBracket :: brackets -> | ||
| if lastBracket = getRelativeOpen (fstChar) then | ||
| internalBracketSequenceChecker tail brackets | ||
| else | ||
| false | ||
| | _ -> false | ||
| else | ||
| internalBracketSequenceChecker tail bracketsStack | ||
| | [] -> bracketsStack = [] | ||
|
|
||
| internalBracketSequenceChecker (bracketString |> Seq.toList) [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
|
Contributor
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. .NET 7 устарел |
||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="BracketSequence.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 25.0.1706.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BracketSequence", "BracketSequence.fsproj", "{A2CB2149-29E8-46F6-886C-2055C0147B2D}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BracketSequence.Tests", "..\BracketSequence.Tests\BracketSequence.Tests.fsproj", "{7E89457D-5A00-410F-A24F-50F10C906788}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {A2CB2149-29E8-46F6-886C-2055C0147B2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A2CB2149-29E8-46F6-886C-2055C0147B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A2CB2149-29E8-46F6-886C-2055C0147B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A2CB2149-29E8-46F6-886C-2055C0147B2D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {7E89457D-5A00-410F-A24F-50F10C906788}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {7E89457D-5A00-410F-A24F-50F10C906788}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {7E89457D-5A00-410F-A24F-50F10C906788}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {7E89457D-5A00-410F-A24F-50F10C906788}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {B3D8A4FD-3A28-4BCD-BFD8-85BD77F30461} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>true</GenerateProgramFile> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="PhoneBookTests.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
| <PackageReference Include="NUnit" Version="4.2.2" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="4.3.0"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="FsUnit" Version="6.0.1"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| <PackageReference Include="FsCheck" Version="2.16.6"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\PhoneBook\PhoneBook.fsproj" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| module PhoneBook.Tests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open System.IO | ||
| open PhoneBook | ||
|
|
||
| let testFilePath = "testFileBase.txt" | ||
|
|
||
| let contactMax = ({ Name = "Max"; Phone = "123" }) | ||
| let contactVasya = ({ Name = "Vasya"; Phone = "124" }) | ||
|
|
||
| [<Test>] | ||
| let ``Write and read test`` () = | ||
| File.WriteAllText(testFilePath, "") | ||
| let data = [ contactMax; contactMax ] | ||
| writeContactsToFile data testFilePath | ||
| readContactsFromFile testFilePath |> should equal data | ||
|
|
||
| [<Test>] | ||
| let ``Add new record test`` () = | ||
| let data = [ contactMax ] | ||
|
|
||
| let expected = ([ contactVasya; contactMax ], true) | ||
|
|
||
| addContactToBase "Vasya" "124" data |> should equal expected | ||
|
|
||
| [<Test>] | ||
| let ``Can not add record with existed in base phone or name`` () = | ||
| let data = [ contactMax ] | ||
| let (newData, _) = addContactToBase "Vasya" "123" data | ||
| let (newNewData, _) = addContactToBase "Max" "345" newData | ||
| (newNewData, newData) |> should equal (data, data) | ||
|
|
||
| [<Test>] | ||
| let ``Finding ny name and phone test`` () = | ||
| let name = "Max" | ||
| let phone = "123" | ||
| let data = [ contactMax ] | ||
| let res1 = findContactByNameInBase name data | ||
| let res2 = findContactByPhoneInBase phone data | ||
|
|
||
| (res1, res2) |> should equal (Some(contactMax), Some(contactMax)) | ||
|
|
||
| [<Test>] | ||
| let ``isContactInFile test`` () = | ||
| let name = "Max" | ||
| let phone = "123" | ||
| File.WriteAllText(testFilePath, "") | ||
| let data = [ contactMax ] | ||
| writeContactsToFile data testFilePath | ||
| isContactInFile name phone testFilePath |> should be True | ||
|
|
||
| [<Test>] | ||
| let ``isContactInBase test`` () = | ||
| let data = [ contactMax ] | ||
|
|
||
| isContactInBase "Max" "123" data |> should be True | ||
|
|
||
| [<Test>] | ||
| let ``Write contacts from base to file test`` () = | ||
| File.WriteAllText(testFilePath, "") | ||
|
|
||
| let phoneBase = [ contactVasya; contactMax ] | ||
|
|
||
| writeContactsToFile phoneBase testFilePath | ||
| readContactsFromFile testFilePath |> should equal phoneBase | ||
|
|
||
| [<Test>] | ||
| let ``Write contactMaxs from file to base`` () = | ||
| File.WriteAllText(testFilePath, "") | ||
|
|
||
| let data = [ contactMax; contactVasya ] | ||
|
|
||
| writeContactsToFile data testFilePath | ||
|
|
||
| writePhoneBaseFromFile testFilePath |> should equal data | ||
|
|
||
| [<Test>] | ||
| let ``Correct phone test`` () = | ||
| (isCorrectPhone "88005555", isCorrectPhone "880055d5") | ||
| |> should equal (true, false) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| namespace PhoneBook | ||
|
|
||
| open System.IO | ||
| open System.Text.RegularExpressions | ||
| open Newtonsoft.Json | ||
|
|
||
| module PhoneBook = | ||
| type Contact = { Name: string; Phone: string } | ||
|
|
||
| let readContactsFromFile phoneBaseFile = | ||
| if File.Exists(phoneBaseFile) then | ||
| let json = File.ReadAllText(phoneBaseFile) | ||
|
|
||
| if json = "" then | ||
| [] | ||
| else | ||
| JsonConvert.DeserializeObject<Contact list>(json) | ||
| else | ||
| [] | ||
|
|
||
| let writeContactsToFile (contacts: Contact list) phoneBaseFile = | ||
| if contacts = [] then | ||
| () | ||
| else | ||
| let json = | ||
| JsonConvert.SerializeObject((contacts @ (readContactsFromFile phoneBaseFile)), Formatting.Indented) | ||
|
Contributor
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. А зачем всё-таки их мерджить с содержимым файла перед записью? Мы ведь в contacts перед началом работы данные из файла уже зачитали, так что получатся дублирующиеся записи, нет? |
||
|
|
||
| File.WriteAllText(phoneBaseFile, json) | ||
|
|
||
| let isContactInFile nameForFind phoneToFind phoneBaseFile = | ||
| List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneToFind) | ||
| <| readContactsFromFile phoneBaseFile | ||
|
Comment on lines
+30
to
+32
Contributor
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. Это, кажется, не используется нигде |
||
|
|
||
| let isContactInBase nameForFind phoneToFind phoneBase = | ||
| List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneToFind) phoneBase | ||
|
|
||
| let writePhoneBaseFromFile phoneBaseFile = readContactsFromFile phoneBaseFile | ||
|
Contributor
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. read, может быть? |
||
|
|
||
| let isCorrectPhone (phone: string) = | ||
| let pattern = @"^[0-9]+$" | ||
| let regex = new Regex(pattern) | ||
| regex.IsMatch(phone) | ||
|
|
||
| let addContactToBase name phone (phoneBase: Contact List) = | ||
| if (not (isContactInBase name phone phoneBase)) && isCorrectPhone (phone) then | ||
| (({ Name = name; Phone = phone } :: phoneBase), true) | ||
| else | ||
| (phoneBase, false) | ||
|
|
||
| let findContactByPhoneInBase phone (phoneBase: Contact List) = | ||
| List.tryFind (fun contact -> contact.Phone = phone) phoneBase | ||
|
|
||
| let findContactByNameInBase name (phoneBase: Contact List) = | ||
| List.tryFind (fun contact -> contact.Name = name) phoneBase | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="PhoneBook.fs" /> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Newtonsoft.Json" Version="13.0.3"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| <PackageReference Include="FSharp.Core" Version="9.0.100"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 25.0.1706.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "PhoneBook", "PhoneBook.fsproj", "{628FF28C-A8BC-4E9E-9B00-1E61DA100811}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "PhoneBook.Tests", "..\PhoneBook.Tests\PhoneBook.Tests.fsproj", "{443CCD9A-A63A-4874-9FC3-22685C16E1F2}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {628FF28C-A8BC-4E9E-9B00-1E61DA100811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {628FF28C-A8BC-4E9E-9B00-1E61DA100811}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {628FF28C-A8BC-4E9E-9B00-1E61DA100811}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {628FF28C-A8BC-4E9E-9B00-1E61DA100811}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {443CCD9A-A63A-4874-9FC3-22685C16E1F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {443CCD9A-A63A-4874-9FC3-22685C16E1F2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {443CCD9A-A63A-4874-9FC3-22685C16E1F2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {443CCD9A-A63A-4874-9FC3-22685C16E1F2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {DE39838F-555F-4B22-BF4A-9189FD1CA344} | ||
| EndGlobalSection | ||
| EndGlobal |
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.
Не сокращайте имена