From 649a478d722d1c5c903bc0173bed82dc07666f0a Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Sun, 10 Nov 2024 18:35:29 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=20=D1=81=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BracketSequence.Tests.fsproj | 37 +++++++++++++++++++ BracketSequence.Tests/BracketSequenceTest.fs | 28 ++++++++++++++ BracketSequence.Tests/Program.fs | 5 +++ BracketSequence/BracketSequence.fs | 26 +++++++++++++ BracketSequence/BracketSequence.fsproj | 12 ++++++ BracketSequence/BracketSequence.sln | 31 ++++++++++++++++ 6 files changed, 139 insertions(+) create mode 100644 BracketSequence.Tests/BracketSequence.Tests.fsproj create mode 100644 BracketSequence.Tests/BracketSequenceTest.fs create mode 100644 BracketSequence.Tests/Program.fs create mode 100644 BracketSequence/BracketSequence.fs create mode 100644 BracketSequence/BracketSequence.fsproj create mode 100644 BracketSequence/BracketSequence.sln diff --git a/BracketSequence.Tests/BracketSequence.Tests.fsproj b/BracketSequence.Tests/BracketSequence.Tests.fsproj new file mode 100644 index 0000000..dd6444a --- /dev/null +++ b/BracketSequence.Tests/BracketSequence.Tests.fsproj @@ -0,0 +1,37 @@ + + + + net7.0 + + false + false + true + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + + + + + + + + + + + + diff --git a/BracketSequence.Tests/BracketSequenceTest.fs b/BracketSequence.Tests/BracketSequenceTest.fs new file mode 100644 index 0000000..b1dd3a5 --- /dev/null +++ b/BracketSequence.Tests/BracketSequenceTest.fs @@ -0,0 +1,28 @@ +module BracketSequence.Tests + +open NUnit.Framework +open FsUnit +open BracketSequenceChecker + +[] +let ``Function checks correctly sequence of brackets`` () = + let brSeqCorr = "(({})[])" + let brSeqIncorr = "((({})[])" + + (isCorrectBracketSequence brSeqCorr, isCorrectBracketSequence brSeqIncorr) |> should equal (true, false) + +[] +let ``Function checks correctly sequence of brackets with other symbols`` () = + let brSeq = "(t({}d)[]g)" + + isCorrectBracketSequence brSeq |> should be True + +[] +let ``Function checks correctly empty string`` () = + isCorrectBracketSequence "" |> should be True + +[] +let ``Function checks correctly string with no braskets`` () = + let brSeq = "abc" + + isCorrectBracketSequence brSeq |> should be True \ No newline at end of file diff --git a/BracketSequence.Tests/Program.fs b/BracketSequence.Tests/Program.fs new file mode 100644 index 0000000..5da7e48 --- /dev/null +++ b/BracketSequence.Tests/Program.fs @@ -0,0 +1,5 @@ +module Program = + + [] + let main _ = 0 + diff --git a/BracketSequence/BracketSequence.fs b/BracketSequence/BracketSequence.fs new file mode 100644 index 0000000..7187173 --- /dev/null +++ b/BracketSequence/BracketSequence.fs @@ -0,0 +1,26 @@ +namespace BracketSequence + +module BracketSequenceChecker = + let bracketList = [('(', ')'); ('[', ']'); ('{', '}')] + + let isOpen (bracket: char) = bracket = '(' || bracket = '{' || bracket = '[' + + let isClose (bracket: char) = bracket = ']' || bracket = ')' || bracket = '}' + + let relativeOpen (bracket: char) = List.find (fun x -> snd x = bracket) bracketList |> fst + + 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 = relativeOpen(fstChar) then internalBracketSequenceChecker tail brackets + else false + | _ -> false + else internalBracketSequenceChecker tail bracketsStack + | [] -> if bracketsStack = [] then true + else false + internalBracketSequenceChecker (bracketString |> Seq.toList) [] \ No newline at end of file diff --git a/BracketSequence/BracketSequence.fsproj b/BracketSequence/BracketSequence.fsproj new file mode 100644 index 0000000..51cae49 --- /dev/null +++ b/BracketSequence/BracketSequence.fsproj @@ -0,0 +1,12 @@ + + + + net7.0 + true + + + + + + + diff --git a/BracketSequence/BracketSequence.sln b/BracketSequence/BracketSequence.sln new file mode 100644 index 0000000..0780b8c --- /dev/null +++ b/BracketSequence/BracketSequence.sln @@ -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 From 5feb2e21433dfc91abba1e3bec09e3f70bdcfc5e Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Sun, 10 Nov 2024 20:21:44 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=202=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=20=D1=81=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PointFree.Tests/PointFree.Tests.fsproj | 37 ++++++++++++++++++++++++++ PointFree.Tests/PointFreeTest.fs | 14 ++++++++++ PointFree.Tests/Program.fs | 5 ++++ PointFree/PointFree.fs | 13 +++++++++ PointFree/PointFree.fsproj | 12 +++++++++ PointFree/PointFree.sln | 31 +++++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 PointFree.Tests/PointFree.Tests.fsproj create mode 100644 PointFree.Tests/PointFreeTest.fs create mode 100644 PointFree.Tests/Program.fs create mode 100644 PointFree/PointFree.fs create mode 100644 PointFree/PointFree.fsproj create mode 100644 PointFree/PointFree.sln diff --git a/PointFree.Tests/PointFree.Tests.fsproj b/PointFree.Tests/PointFree.Tests.fsproj new file mode 100644 index 0000000..e786e9a --- /dev/null +++ b/PointFree.Tests/PointFree.Tests.fsproj @@ -0,0 +1,37 @@ + + + + net7.0 + + false + false + true + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + + + + + + + + + + + + diff --git a/PointFree.Tests/PointFreeTest.fs b/PointFree.Tests/PointFreeTest.fs new file mode 100644 index 0000000..d846b60 --- /dev/null +++ b/PointFree.Tests/PointFreeTest.fs @@ -0,0 +1,14 @@ +module PointFree.Tests + +open NUnit.Framework +open FsCheck +open PointFree + + +[] +let ``Functions are equal`` () = + let functionResAreEq (x: int) (l: int list) = (func'1 x l = func'2 x l) && + (func'2 x l = func'3 x l) && (func'3 x l = func'4 x l) && + (func'4 x l = func'5 x l) + + Check.Quick functionResAreEq \ No newline at end of file diff --git a/PointFree.Tests/Program.fs b/PointFree.Tests/Program.fs new file mode 100644 index 0000000..5da7e48 --- /dev/null +++ b/PointFree.Tests/Program.fs @@ -0,0 +1,5 @@ +module Program = + + [] + let main _ = 0 + diff --git a/PointFree/PointFree.fs b/PointFree/PointFree.fs new file mode 100644 index 0000000..fe7a39d --- /dev/null +++ b/PointFree/PointFree.fs @@ -0,0 +1,13 @@ +namespace PointFree + +module PointFree = + + let func'1 x l = List.map (fun y -> y * x) l + + let func'2 x = List.map (fun y -> y * x) + + let func'3 x = List.map (fun y -> (*) y x) + + let func'4 x = List.map ((*) x) + + let func'5 = List.map << (*) \ No newline at end of file diff --git a/PointFree/PointFree.fsproj b/PointFree/PointFree.fsproj new file mode 100644 index 0000000..d49e68a --- /dev/null +++ b/PointFree/PointFree.fsproj @@ -0,0 +1,12 @@ + + + + net7.0 + true + + + + + + + diff --git a/PointFree/PointFree.sln b/PointFree/PointFree.sln new file mode 100644 index 0000000..2a27ac9 --- /dev/null +++ b/PointFree/PointFree.sln @@ -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}") = "PointFree", "PointFree.fsproj", "{2BED6737-912E-4273-B30E-ED0DB93363C4}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "PointFree.Tests", "..\PointFree.Tests\PointFree.Tests.fsproj", "{2A7F66CA-078A-42A6-AD6B-C2051975BE6B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2BED6737-912E-4273-B30E-ED0DB93363C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2BED6737-912E-4273-B30E-ED0DB93363C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2BED6737-912E-4273-B30E-ED0DB93363C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2BED6737-912E-4273-B30E-ED0DB93363C4}.Release|Any CPU.Build.0 = Release|Any CPU + {2A7F66CA-078A-42A6-AD6B-C2051975BE6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2A7F66CA-078A-42A6-AD6B-C2051975BE6B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2A7F66CA-078A-42A6-AD6B-C2051975BE6B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2A7F66CA-078A-42A6-AD6B-C2051975BE6B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CB1E270B-8D1D-4072-A2E7-0C57406967F1} + EndGlobalSection +EndGlobal From 526042b2a08e483b2b56ce8d0a1ac841ec1669f4 Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Mon, 11 Nov 2024 03:19:46 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PhoneBook.Tests/.DS_Store | Bin 0 -> 6148 bytes PhoneBook.Tests/PhoneBook.Tests.fsproj | 40 +++++++++ PhoneBook.Tests/PhoneBookTests.fs | 76 +++++++++++++++++ PhoneBook.Tests/Program.fs | 5 ++ PhoneBook/.DS_Store | Bin 0 -> 6148 bytes PhoneBook/PhoneBook.fs | 62 ++++++++++++++ PhoneBook/PhoneBook.fsproj | 18 ++++ PhoneBook/PhoneBook.sln | 31 +++++++ PhoneBook/Program.fs | 110 +++++++++++++++++++++++++ 9 files changed, 342 insertions(+) create mode 100644 PhoneBook.Tests/.DS_Store create mode 100644 PhoneBook.Tests/PhoneBook.Tests.fsproj create mode 100644 PhoneBook.Tests/PhoneBookTests.fs create mode 100644 PhoneBook.Tests/Program.fs create mode 100644 PhoneBook/.DS_Store create mode 100644 PhoneBook/PhoneBook.fs create mode 100644 PhoneBook/PhoneBook.fsproj create mode 100644 PhoneBook/PhoneBook.sln create mode 100644 PhoneBook/Program.fs diff --git a/PhoneBook.Tests/.DS_Store b/PhoneBook.Tests/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..99a7005889933a9e5114c53510f14e7a9f53d655 GIT binary patch literal 6148 zcmeHKL2uJA6n<_CO*bJGX`o#uMdDhOj!l)OT|zbvyG>+*!$2jWWedyfs!7*FRVioq zF`e$w-2Kfm{Ee~#>!h(tIYZWFbM$b&N0S}0Zs_p{!Snzb~6 zf^(>kNv0CCIGvM!fdSsTIwh2nJj#~#?O+8NDq@&rOy>Xyq5vOuA(E;gj1ebSjQ#n+# zP)+49*Rhf5@LFDL(B7QSxBI&tf2Y@9bo}|=i(bdy-RUnDE$_**=lib*7uls$UkvdA z-=vmD7H{AyDt0wj(L`oSp25Q2byDEsbA^d-=()|rQ#z(|cuE>k2vl@IT{=N4rvlog zPbkMUK~E)@*sA1uydqa6)ADQt&;Iso){ucmIzt)Lr5U{v^P>`jrW412W8hvhV9!Ty z{a&-+7Vj8v46I^+&j%mM7+5R~>Z1dNegXg+Fl#|w|NH}eHUI;Qr9oIA!h`}%sIX5A zVc!nIgrh&O{-r?^PC}22Ja%MZUns&pJ%q2_NdyL6=@@VfEHh9y%{Jfvum1f0Uk-9D z$ADwtzhXc%4x_^mOR{(CoyGB8>q9?4S=g^M_&Wp@eHFu(ui{Op7TA5R00WDqL0BN} OM?lly3dg`-W#AY7a*qH2 literal 0 HcmV?d00001 diff --git a/PhoneBook.Tests/PhoneBook.Tests.fsproj b/PhoneBook.Tests/PhoneBook.Tests.fsproj new file mode 100644 index 0000000..3c56873 --- /dev/null +++ b/PhoneBook.Tests/PhoneBook.Tests.fsproj @@ -0,0 +1,40 @@ + + + + net7.0 + + false + false + true + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + + + + + + + + + + + + + + + diff --git a/PhoneBook.Tests/PhoneBookTests.fs b/PhoneBook.Tests/PhoneBookTests.fs new file mode 100644 index 0000000..2e39f92 --- /dev/null +++ b/PhoneBook.Tests/PhoneBookTests.fs @@ -0,0 +1,76 @@ +module PhoneBook.Tests + +open NUnit.Framework +open FsUnit +open System.IO +open PhoneBook + +let testFilePath = "testFileBase.txt" + +[] +let ``Write and read test`` () = + File.WriteAllText(testFilePath,"") + let data = [({Name = "Max"; Phone = "123"}); ({Name = "Max"; Phone = "123"})] + writeContactsToFile data testFilePath + readContactsFromFile testFilePath |> should equal data + +[] +let ``Add new record test`` () = + let data = [({Name = "Max"; Phone = "123"})] + let expected = ([({Name = "Vasya"; Phone = "124"}); ({Name = "Max"; Phone = "123"})], true) + addContactToBase "Vasya" "124" data |> should equal expected + +[] +let ``Can not add record with existed in base phone or name`` () = + let data = [({Name = "Max"; Phone = "123"})] + let (newData, _) = addContactToBase "Vasya" "123" data + let (newNewData, _) = addContactToBase "Max" "345" newData + (newNewData, newData) |> should equal (data, data) + +[] +let ``Finding ny name and phone test`` () = + let name = "Max" + let phone = "123" + let contact = ({Name = name; Phone = phone}) + let data = [contact] + let res1 = findContactByNameInBase name data + let res2 = findContactByPhoneInBase phone data + + (res1, res2) |> should equal (Some(contact), Some(contact)) + +[] +let ``isContactInFile test`` () = + let name = "Max" + let phone = "123" + File.WriteAllText(testFilePath,"") + let data = [({Name = name; Phone = phone})] + writeContactsToFile data testFilePath + isContactInFile name phone testFilePath |> should be True + +[] +let ``isContactInBase test`` () = + let data = [({Name = "Max"; Phone = "123"})] + + isContactInBase "Max" "123" data |> should be True + +[] +let ``Write contacts from base to file test`` () = + File.WriteAllText(testFilePath, "") + let data = [({Name = "Max"; Phone = "123"})] + writeContactsToFile data testFilePath + let contacts = [({Name = "Vasya"; Phone = "124"}); ({Name = "Max"; Phone = "123"})] + writePhoneBaseToFile contacts testFilePath + readContactsFromFile testFilePath |> should equal contacts + +[] +let ``Write contacts from file to base`` () = + File.WriteAllText(testFilePath,"") + let data = [({Name = "Max"; Phone = "123"}); ({Name = "Vasya"; Phone = "124"})] + let contacts = [({Name = "Max"; Phone = "123"})] + writeContactsToFile data testFilePath + + writePhoneBaseFromFile contacts testFilePath |> should equal data + +[] +let ``Correct phone test`` () = + (isCorrectPhone "88005555", isCorrectPhone "880055d5")|> should equal (true, false) \ No newline at end of file diff --git a/PhoneBook.Tests/Program.fs b/PhoneBook.Tests/Program.fs new file mode 100644 index 0000000..5da7e48 --- /dev/null +++ b/PhoneBook.Tests/Program.fs @@ -0,0 +1,5 @@ +module Program = + + [] + let main _ = 0 + diff --git a/PhoneBook/.DS_Store b/PhoneBook/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..cb5201d788987ec8cccf92cd09a87f810e9b2ddd GIT binary patch literal 6148 zcmeHKK~LK-6n<{Ik;0I27!sFBk#?x6=_iQ7zpj~2Ctd&fp^~E{ll`G4WW7F8 zPCsX|rzVja%U+?mu?cuhui5ptr_tON>=O`1pTK2Rx)?j`%G@Y%q z0(O6R8!Np^w>YPOQ{XWQ@b@8rGlmvRgZk5fLSF%Z4RmWmUH|+81MUEZ7E6QZfeAwe z8mh2Y3}NpM!q5>9ZGLIc&`Ic-F^-*C*c*zlR}T^BbP}OKS2_ip0*eaNP1EP||M=ni z|02n?oB~dPl~O=7j^d*TOR{I{y~Xia8^FK7**LE>s3|DyajYwR6mP?|Ar^227+Ne1 Sq6g-F1hfpUa0>iW1^xnoVwrmY literal 0 HcmV?d00001 diff --git a/PhoneBook/PhoneBook.fs b/PhoneBook/PhoneBook.fs new file mode 100644 index 0000000..05431c7 --- /dev/null +++ b/PhoneBook/PhoneBook.fs @@ -0,0 +1,62 @@ +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(json) + else + [] + + let writeContactsToFile (contacts: Contact list) phoneBaseFile = + if contacts = [] then () + else + let json = JsonConvert.SerializeObject((contacts @ (readContactsFromFile phoneBaseFile)), Formatting.Indented) + File.WriteAllText(phoneBaseFile, json) + + let isContactInFile nameForFind phoneForFind phoneBaseFile = + List.exists(fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) <| readContactsFromFile phoneBaseFile + + let isContactInBase nameForFind phoneForFind phoneBase = + List.exists(fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) phoneBase + + let writePhoneBaseToFile (phoneBase: Contact List) phoneBaseFile = + writeContactsToFile (List.filter(fun contact -> not (isContactInFile contact.Name contact.Phone phoneBaseFile)) phoneBase) phoneBaseFile + + let writePhoneBaseFromFile (phoneBase: Contact List) phoneBaseFile = + let baseInFile = readContactsFromFile phoneBaseFile + let newPhoneBase = phoneBase @ (List.filter (fun contact -> not (isContactInBase contact.Name contact.Phone phoneBase)) baseInFile) + newPhoneBase + + 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 + + + + + diff --git a/PhoneBook/PhoneBook.fsproj b/PhoneBook/PhoneBook.fsproj new file mode 100644 index 0000000..011a57e --- /dev/null +++ b/PhoneBook/PhoneBook.fsproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + + + + + + + + + + + + + \ No newline at end of file diff --git a/PhoneBook/PhoneBook.sln b/PhoneBook/PhoneBook.sln new file mode 100644 index 0000000..2916851 --- /dev/null +++ b/PhoneBook/PhoneBook.sln @@ -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 diff --git a/PhoneBook/Program.fs b/PhoneBook/Program.fs new file mode 100644 index 0000000..b9c4570 --- /dev/null +++ b/PhoneBook/Program.fs @@ -0,0 +1,110 @@ +open System +open PhoneBook.PhoneBook + +let printInstructions() = + printfn "It's a phone book!" + printfn "You can enter command number to:" + printfn "1. Add new record" + printfn "2. Find contact by phone" + printfn "3. Find contact by name" + printfn "4. Print current phone book" + printfn "5. Print contacts from file" + printfn "6. Add contacts from file to current phone book" + printfn "7. Add contacts from current phone book to file" + printfn "8. Exit" + +let phoneBaseFile = "PhoneBase.txt" + +let stringContact contact = sprintf "%s - %s" contact.Name contact.Phone + +let printContactList (contactList: Contact list) = + if List.isEmpty contactList then + printfn "There are no contacts\n" + else + printfn "n" + for contact in contactList do + printfn "%s" (stringContact contact) + +let rec enterCorrectPhone() = + printfn "Enter phone: " + let phone = Console.ReadLine() + if isCorrectPhone(phone) then phone + else + printfn "Phone was incorrect\n" + enterCorrectPhone() + +let addNewRecord (phoneBase: Contact list) = + printfn "To add new record enter name: " + let name = Console.ReadLine() + let phone = enterCorrectPhone() + match addContactToBase name phone phoneBase with + | (newPhoneBase, true) -> + printfn "Contact added successfully!\n" + newPhoneBase + | _ -> + printfn "Contact wasn't added: phone or name are already in phone book or\n" + phoneBase + +let printFindingRes (result: Contact option) = + match result with + | Some contact -> + printfn "Contact was found: %s" (stringContact contact) + | None -> + printfn "Contact wasn't found" + +let findPhoneByName (phoneBase: Contact list) = + let phone = enterCorrectPhone() + printFindingRes (findContactByPhoneInBase phone phoneBase) + +let findNameByPhone (phoneBase: Contact list) = + printfn "Enter name: " + let name = Console.ReadLine() + printFindingRes (findContactByNameInBase name phoneBase) + +let printCurrentPhoneBook (phoneBase: Contact list) = + printfn "Current phone book:\n" + printContactList phoneBase + +let printContactsFromFile () = + printfn "Contacts in file:" + printContactList (readContactsFromFile phoneBaseFile) + +let addContactsFromFile (phoneBase: Contact list) = + writePhoneBaseFromFile phoneBase phoneBaseFile + +let addContactsToFile (phoneBase: Contact list) = + writePhoneBaseToFile phoneBase phoneBaseFile + +let Run = + let rec internalRun (phoneBase: Contact list) = + printInstructions() + printfn "Enter command: " + let command = Console.ReadLine() + match command with + | "1" -> + let newPhoneBase = addNewRecord phoneBase + internalRun newPhoneBase + | "2" -> + findPhoneByName phoneBase + internalRun phoneBase + | "3" -> + findNameByPhone phoneBase + internalRun phoneBase + | "4" -> + printCurrentPhoneBook phoneBase + internalRun phoneBase + | "5" -> + printContactsFromFile() + internalRun phoneBase + | "6" -> + let newPhoneBase = addContactsFromFile phoneBase + internalRun newPhoneBase + | "7" -> + addContactsToFile phoneBase + internalRun phoneBase + | "8" -> + printfn "Exit" + | _ -> + printfn "Incorrect command!\n" + internalRun phoneBase + internalRun [] \ No newline at end of file From 6efabf554780c8033d272747bd8a0d5e2e79c96a Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Mon, 25 Nov 2024 05:32:59 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=9E=D1=82=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D1=8B,=20=D0=B2=20=D1=82=D0=B5=D0=BB=D0=B5=D1=84?= =?UTF-8?q?=D0=BE=D0=BD=D0=BD=D0=BE=D0=B9=20=D0=BA=D0=BD=D0=B8=D0=B3=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=BB=D0=B0=20=D0=BE=D0=BF?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D0=B0=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BracketSequence.Tests.fsproj | 3 - BracketSequence.Tests/BracketSequenceTest.fs | 17 +++--- BracketSequence.Tests/Program.fs | 1 - BracketSequence/BracketSequence.fs | 39 +++++++------ PhoneBook.Tests/PhoneBook.Tests.fsproj | 3 - PhoneBook.Tests/PhoneBookTests.fs | 55 +++++++++++-------- PhoneBook.Tests/Program.fs | 1 - PhoneBook/PhoneBook.fs | 48 ++++++++-------- PhoneBook/PhoneBook.fsproj | 3 + PhoneBook/Program.fs | 44 ++++++++------- PointFree.Tests/PointFreeTest.fs | 12 ++-- PointFree.Tests/Program.fs | 1 - PointFree/PointFree.fs | 2 +- 13 files changed, 124 insertions(+), 105 deletions(-) diff --git a/BracketSequence.Tests/BracketSequence.Tests.fsproj b/BracketSequence.Tests/BracketSequence.Tests.fsproj index dd6444a..4601aa3 100644 --- a/BracketSequence.Tests/BracketSequence.Tests.fsproj +++ b/BracketSequence.Tests/BracketSequence.Tests.fsproj @@ -23,9 +23,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - - - diff --git a/BracketSequence.Tests/BracketSequenceTest.fs b/BracketSequence.Tests/BracketSequenceTest.fs index b1dd3a5..ca40300 100644 --- a/BracketSequence.Tests/BracketSequenceTest.fs +++ b/BracketSequence.Tests/BracketSequenceTest.fs @@ -6,23 +6,24 @@ open BracketSequenceChecker [] let ``Function checks correctly sequence of brackets`` () = - let brSeqCorr = "(({})[])" - let brSeqIncorr = "((({})[])" + let brSeqCorr = "(({})[])" + let brSeqIncorr = "((({})[])" - (isCorrectBracketSequence brSeqCorr, isCorrectBracketSequence brSeqIncorr) |> should equal (true, false) + (isCorrectBracketSequence brSeqCorr, isCorrectBracketSequence brSeqIncorr) + |> should equal (true, false) [] let ``Function checks correctly sequence of brackets with other symbols`` () = - let brSeq = "(t({}d)[]g)" + let brSeq = "(t({}d)[]g)" - isCorrectBracketSequence brSeq |> should be True + isCorrectBracketSequence brSeq |> should be True [] let ``Function checks correctly empty string`` () = - isCorrectBracketSequence "" |> should be True + isCorrectBracketSequence "" |> should be True [] let ``Function checks correctly string with no braskets`` () = - let brSeq = "abc" + let brSeq = "abc" - isCorrectBracketSequence brSeq |> should be True \ No newline at end of file + isCorrectBracketSequence brSeq |> should be True diff --git a/BracketSequence.Tests/Program.fs b/BracketSequence.Tests/Program.fs index 5da7e48..5fa22d1 100644 --- a/BracketSequence.Tests/Program.fs +++ b/BracketSequence.Tests/Program.fs @@ -2,4 +2,3 @@ [] let main _ = 0 - diff --git a/BracketSequence/BracketSequence.fs b/BracketSequence/BracketSequence.fs index 7187173..d079742 100644 --- a/BracketSequence/BracketSequence.fs +++ b/BracketSequence/BracketSequence.fs @@ -1,26 +1,33 @@ namespace BracketSequence module BracketSequenceChecker = - let bracketList = [('(', ')'); ('[', ']'); ('{', '}')] + let bracketList = [ ('(', ')'); ('[', ']'); ('{', '}') ] - let isOpen (bracket: char) = bracket = '(' || bracket = '{' || bracket = '[' + let isOpen (bracket: char) = + bracket = '(' || bracket = '{' || bracket = '[' - let isClose (bracket: char) = bracket = ']' || bracket = ')' || bracket = '}' + let isClose (bracket: char) = + bracket = ']' || bracket = ')' || bracket = '}' - let relativeOpen (bracket: char) = List.find (fun x -> snd x = bracket) bracketList |> fst + let getRelativeOpen (bracket: char) = + List.find (fun x -> snd x = bracket) bracketList |> fst 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 = relativeOpen(fstChar) then internalBracketSequenceChecker tail brackets - else false - | _ -> false - else internalBracketSequenceChecker tail bracketsStack - | [] -> if bracketsStack = [] then true - else false - internalBracketSequenceChecker (bracketString |> Seq.toList) [] \ No newline at end of file + | 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 + | [] -> if bracketsStack = [] then true else false + + internalBracketSequenceChecker (bracketString |> Seq.toList) [] diff --git a/PhoneBook.Tests/PhoneBook.Tests.fsproj b/PhoneBook.Tests/PhoneBook.Tests.fsproj index 3c56873..aa42137 100644 --- a/PhoneBook.Tests/PhoneBook.Tests.fsproj +++ b/PhoneBook.Tests/PhoneBook.Tests.fsproj @@ -29,9 +29,6 @@ - - - diff --git a/PhoneBook.Tests/PhoneBookTests.fs b/PhoneBook.Tests/PhoneBookTests.fs index 2e39f92..87603a9 100644 --- a/PhoneBook.Tests/PhoneBookTests.fs +++ b/PhoneBook.Tests/PhoneBookTests.fs @@ -7,22 +7,27 @@ open PhoneBook let testFilePath = "testFileBase.txt" +let contactMax = ({ Name = "Max"; Phone = "123" }) +let contactVasya = ({ Name = "Vasya"; Phone = "124" }) + [] let ``Write and read test`` () = - File.WriteAllText(testFilePath,"") - let data = [({Name = "Max"; Phone = "123"}); ({Name = "Max"; Phone = "123"})] + File.WriteAllText(testFilePath, "") + let data = [ contactMax; contactMax ] writeContactsToFile data testFilePath readContactsFromFile testFilePath |> should equal data [] let ``Add new record test`` () = - let data = [({Name = "Max"; Phone = "123"})] - let expected = ([({Name = "Vasya"; Phone = "124"}); ({Name = "Max"; Phone = "123"})], true) + let data = [ contactMax ] + + let expected = ([ contactVasya; contactMax ], true) + addContactToBase "Vasya" "124" data |> should equal expected [] let ``Can not add record with existed in base phone or name`` () = - let data = [({Name = "Max"; Phone = "123"})] + let data = [ contactMax ] let (newData, _) = addContactToBase "Vasya" "123" data let (newNewData, _) = addContactToBase "Max" "345" newData (newNewData, newData) |> should equal (data, data) @@ -31,46 +36,50 @@ let ``Can not add record with existed in base phone or name`` () = let ``Finding ny name and phone test`` () = let name = "Max" let phone = "123" - let contact = ({Name = name; Phone = phone}) - let data = [contact] + let data = [ contactMax ] let res1 = findContactByNameInBase name data let res2 = findContactByPhoneInBase phone data - (res1, res2) |> should equal (Some(contact), Some(contact)) + (res1, res2) |> should equal (Some(contactMax), Some(contactMax)) [] let ``isContactInFile test`` () = let name = "Max" let phone = "123" - File.WriteAllText(testFilePath,"") - let data = [({Name = name; Phone = phone})] + File.WriteAllText(testFilePath, "") + let data = [ contactMax ] writeContactsToFile data testFilePath isContactInFile name phone testFilePath |> should be True [] let ``isContactInBase test`` () = - let data = [({Name = "Max"; Phone = "123"})] + let data = [ contactMax ] isContactInBase "Max" "123" data |> should be True - + [] -let ``Write contacts from base to file test`` () = +let ``Write contactMaxs from base to file test`` () = File.WriteAllText(testFilePath, "") - let data = [({Name = "Max"; Phone = "123"})] + let data = [ contactMax ] writeContactsToFile data testFilePath - let contacts = [({Name = "Vasya"; Phone = "124"}); ({Name = "Max"; Phone = "123"})] - writePhoneBaseToFile contacts testFilePath - readContactsFromFile testFilePath |> should equal contacts + + let contactMaxs = [ contactVasya; contactMax ] + + writePhoneBaseToFile contactMaxs testFilePath + readContactsFromFile testFilePath |> should equal contactMaxs [] -let ``Write contacts from file to base`` () = - File.WriteAllText(testFilePath,"") - let data = [({Name = "Max"; Phone = "123"}); ({Name = "Vasya"; Phone = "124"})] - let contacts = [({Name = "Max"; Phone = "123"})] +let ``Write contactMaxs from file to base`` () = + File.WriteAllText(testFilePath, "") + + let data = [ contactMax; contactVasya ] + + let contactMaxs = [ contactMax ] writeContactsToFile data testFilePath - writePhoneBaseFromFile contacts testFilePath |> should equal data + writePhoneBaseFromFile contactMaxs testFilePath |> should equal data [] let ``Correct phone test`` () = - (isCorrectPhone "88005555", isCorrectPhone "880055d5")|> should equal (true, false) \ No newline at end of file + (isCorrectPhone "88005555", isCorrectPhone "880055d5") + |> should equal (true, false) diff --git a/PhoneBook.Tests/Program.fs b/PhoneBook.Tests/Program.fs index 5da7e48..5fa22d1 100644 --- a/PhoneBook.Tests/Program.fs +++ b/PhoneBook.Tests/Program.fs @@ -2,4 +2,3 @@ [] let main _ = 0 - diff --git a/PhoneBook/PhoneBook.fs b/PhoneBook/PhoneBook.fs index 05431c7..d220225 100644 --- a/PhoneBook/PhoneBook.fs +++ b/PhoneBook/PhoneBook.fs @@ -5,14 +5,12 @@ open System.Text.RegularExpressions open Newtonsoft.Json module PhoneBook = - type Contact = { - Name: string; - Phone: string - } + type Contact = { Name: string; Phone: string } let readContactsFromFile phoneBaseFile = if File.Exists(phoneBaseFile) then let json = File.ReadAllText(phoneBaseFile) + if json = "" then [] else @@ -21,42 +19,48 @@ module PhoneBook = [] let writeContactsToFile (contacts: Contact list) phoneBaseFile = - if contacts = [] then () + if contacts = [] then + () else - let json = JsonConvert.SerializeObject((contacts @ (readContactsFromFile phoneBaseFile)), Formatting.Indented) + let json = + JsonConvert.SerializeObject((contacts @ (readContactsFromFile phoneBaseFile)), Formatting.Indented) + File.WriteAllText(phoneBaseFile, json) let isContactInFile nameForFind phoneForFind phoneBaseFile = - List.exists(fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) <| readContactsFromFile phoneBaseFile + List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) + <| readContactsFromFile phoneBaseFile let isContactInBase nameForFind phoneForFind phoneBase = - List.exists(fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) phoneBase + List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) phoneBase let writePhoneBaseToFile (phoneBase: Contact List) phoneBaseFile = - writeContactsToFile (List.filter(fun contact -> not (isContactInFile contact.Name contact.Phone phoneBaseFile)) phoneBase) phoneBaseFile + writeContactsToFile + (List.filter (fun contact -> not (isContactInFile contact.Name contact.Phone phoneBaseFile)) phoneBase) + phoneBaseFile let writePhoneBaseFromFile (phoneBase: Contact List) phoneBaseFile = let baseInFile = readContactsFromFile phoneBaseFile - let newPhoneBase = phoneBase @ (List.filter (fun contact -> not (isContactInBase contact.Name contact.Phone phoneBase)) baseInFile) + + let newPhoneBase = + phoneBase + @ (List.filter (fun contact -> not (isContactInBase contact.Name contact.Phone phoneBase)) baseInFile) + newPhoneBase 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 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 + List.tryFind (fun contact -> contact.Phone = phone) phoneBase let findContactByNameInBase name (phoneBase: Contact List) = - List.tryFind(fun contact -> contact.Name = name) phoneBase - - - - - + List.tryFind (fun contact -> contact.Name = name) phoneBase diff --git a/PhoneBook/PhoneBook.fsproj b/PhoneBook/PhoneBook.fsproj index 011a57e..a86bfeb 100644 --- a/PhoneBook/PhoneBook.fsproj +++ b/PhoneBook/PhoneBook.fsproj @@ -14,5 +14,8 @@ + + + \ No newline at end of file diff --git a/PhoneBook/Program.fs b/PhoneBook/Program.fs index b9c4570..e31dba9 100644 --- a/PhoneBook/Program.fs +++ b/PhoneBook/Program.fs @@ -1,7 +1,7 @@ open System open PhoneBook.PhoneBook -let printInstructions() = +let printInstructions () = printfn "It's a phone book!" printfn "You can enter command number to:" printfn "1. Add new record" @@ -15,45 +15,46 @@ let printInstructions() = let phoneBaseFile = "PhoneBase.txt" -let stringContact contact = sprintf "%s - %s" contact.Name contact.Phone +let stringContact contact = + sprintf "%s - %s" contact.Name contact.Phone let printContactList (contactList: Contact list) = - if List.isEmpty contactList then + if List.isEmpty contactList then printfn "There are no contacts\n" else - printfn "n" for contact in contactList do printfn "%s" (stringContact contact) -let rec enterCorrectPhone() = +let rec enterCorrectPhone () = printfn "Enter phone: " let phone = Console.ReadLine() - if isCorrectPhone(phone) then phone + + if isCorrectPhone (phone) then + phone else printfn "Phone was incorrect\n" - enterCorrectPhone() + enterCorrectPhone () let addNewRecord (phoneBase: Contact list) = printfn "To add new record enter name: " let name = Console.ReadLine() - let phone = enterCorrectPhone() + let phone = enterCorrectPhone () + match addContactToBase name phone phoneBase with | (newPhoneBase, true) -> printfn "Contact added successfully!\n" newPhoneBase | _ -> - printfn "Contact wasn't added: phone or name are already in phone book or\n" + printfn "Contact wasn't added: phone or name are already in phone book!\n" phoneBase let printFindingRes (result: Contact option) = match result with - | Some contact -> - printfn "Contact was found: %s" (stringContact contact) - | None -> - printfn "Contact wasn't found" + | Some contact -> printfn "Contact was found: %s" (stringContact contact) + | None -> printfn "Contact wasn't found!" let findPhoneByName (phoneBase: Contact list) = - let phone = enterCorrectPhone() + let phone = enterCorrectPhone () printFindingRes (findContactByPhoneInBase phone phoneBase) let findNameByPhone (phoneBase: Contact list) = @@ -69,17 +70,18 @@ let printContactsFromFile () = printfn "Contacts in file:" printContactList (readContactsFromFile phoneBaseFile) -let addContactsFromFile (phoneBase: Contact list) = +let addContactsFromFile (phoneBase: Contact list) = writePhoneBaseFromFile phoneBase phoneBaseFile -let addContactsToFile (phoneBase: Contact list) = +let addContactsToFile (phoneBase: Contact list) = writePhoneBaseToFile phoneBase phoneBaseFile let Run = let rec internalRun (phoneBase: Contact list) = - printInstructions() + printInstructions () printfn "Enter command: " let command = Console.ReadLine() + match command with | "1" -> let newPhoneBase = addNewRecord phoneBase @@ -94,7 +96,7 @@ let Run = printCurrentPhoneBook phoneBase internalRun phoneBase | "5" -> - printContactsFromFile() + printContactsFromFile () internalRun phoneBase | "6" -> let newPhoneBase = addContactsFromFile phoneBase @@ -102,9 +104,9 @@ let Run = | "7" -> addContactsToFile phoneBase internalRun phoneBase - | "8" -> - printfn "Exit" + | "8" -> printfn "Exit" | _ -> printfn "Incorrect command!\n" internalRun phoneBase - internalRun [] \ No newline at end of file + + internalRun [] diff --git a/PointFree.Tests/PointFreeTest.fs b/PointFree.Tests/PointFreeTest.fs index d846b60..30804d6 100644 --- a/PointFree.Tests/PointFreeTest.fs +++ b/PointFree.Tests/PointFreeTest.fs @@ -7,8 +7,10 @@ open PointFree [] let ``Functions are equal`` () = - let functionResAreEq (x: int) (l: int list) = (func'1 x l = func'2 x l) && - (func'2 x l = func'3 x l) && (func'3 x l = func'4 x l) && - (func'4 x l = func'5 x l) - - Check.Quick functionResAreEq \ No newline at end of file + let functionResAreEq (x: int) (l: int list) = + (func'1 x l = func'2 x l) + && (func'2 x l = func'3 x l) + && (func'3 x l = func'4 x l) + && (func'4 x l = func'5 x l) + + Check.Quick functionResAreEq diff --git a/PointFree.Tests/Program.fs b/PointFree.Tests/Program.fs index 5da7e48..5fa22d1 100644 --- a/PointFree.Tests/Program.fs +++ b/PointFree.Tests/Program.fs @@ -2,4 +2,3 @@ [] let main _ = 0 - diff --git a/PointFree/PointFree.fs b/PointFree/PointFree.fs index fe7a39d..9760a01 100644 --- a/PointFree/PointFree.fs +++ b/PointFree/PointFree.fs @@ -10,4 +10,4 @@ module PointFree = let func'4 x = List.map ((*) x) - let func'5 = List.map << (*) \ No newline at end of file + let func'5 = List.map << (*) From 48aef1e71527d04e507872b5e175acd2658360dc Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Tue, 26 Nov 2024 21:40:34 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BracketSequence.Tests.fsproj | 3 +- BracketSequence.Tests/Program.fs | 4 --- BracketSequence/BracketSequence.fs | 7 ++-- PhoneBook.Tests/PhoneBook.Tests.fsproj | 3 +- PhoneBook.Tests/PhoneBookTests.fs | 13 +++---- PhoneBook.Tests/Program.fs | 4 --- PhoneBook/PhoneBook.fs | 22 +++--------- PhoneBook/Program.fs | 35 ++++++++++--------- 8 files changed, 33 insertions(+), 58 deletions(-) delete mode 100644 BracketSequence.Tests/Program.fs delete mode 100644 PhoneBook.Tests/Program.fs diff --git a/BracketSequence.Tests/BracketSequence.Tests.fsproj b/BracketSequence.Tests/BracketSequence.Tests.fsproj index 4601aa3..2763555 100644 --- a/BracketSequence.Tests/BracketSequence.Tests.fsproj +++ b/BracketSequence.Tests/BracketSequence.Tests.fsproj @@ -4,13 +4,12 @@ net7.0 false - false + true true - diff --git a/BracketSequence.Tests/Program.fs b/BracketSequence.Tests/Program.fs deleted file mode 100644 index 5fa22d1..0000000 --- a/BracketSequence.Tests/Program.fs +++ /dev/null @@ -1,4 +0,0 @@ -module Program = - - [] - let main _ = 0 diff --git a/BracketSequence/BracketSequence.fs b/BracketSequence/BracketSequence.fs index d079742..b46a4c6 100644 --- a/BracketSequence/BracketSequence.fs +++ b/BracketSequence/BracketSequence.fs @@ -1,7 +1,7 @@ namespace BracketSequence module BracketSequenceChecker = - let bracketList = [ ('(', ')'); ('[', ']'); ('{', '}') ] + let bracketList = Map [(')', '('); ('}', '{'); (']', '[')] let isOpen (bracket: char) = bracket = '(' || bracket = '{' || bracket = '[' @@ -9,8 +9,7 @@ module BracketSequenceChecker = let isClose (bracket: char) = bracket = ']' || bracket = ')' || bracket = '}' - let getRelativeOpen (bracket: char) = - List.find (fun x -> snd x = bracket) bracketList |> fst + let getRelativeOpen (bracket: char) = bracketList.[bracket] let isCorrectBracketSequence (bracketString: string) = let rec internalBracketSequenceChecker charList bracketsStack = @@ -28,6 +27,6 @@ module BracketSequenceChecker = | _ -> false else internalBracketSequenceChecker tail bracketsStack - | [] -> if bracketsStack = [] then true else false + | [] -> bracketsStack = [] internalBracketSequenceChecker (bracketString |> Seq.toList) [] diff --git a/PhoneBook.Tests/PhoneBook.Tests.fsproj b/PhoneBook.Tests/PhoneBook.Tests.fsproj index aa42137..f97a0e9 100644 --- a/PhoneBook.Tests/PhoneBook.Tests.fsproj +++ b/PhoneBook.Tests/PhoneBook.Tests.fsproj @@ -4,13 +4,12 @@ net7.0 false - false + true true - diff --git a/PhoneBook.Tests/PhoneBookTests.fs b/PhoneBook.Tests/PhoneBookTests.fs index 87603a9..1ba906b 100644 --- a/PhoneBook.Tests/PhoneBookTests.fs +++ b/PhoneBook.Tests/PhoneBookTests.fs @@ -58,15 +58,13 @@ let ``isContactInBase test`` () = isContactInBase "Max" "123" data |> should be True [] -let ``Write contactMaxs from base to file test`` () = +let ``Write contacts from base to file test`` () = File.WriteAllText(testFilePath, "") - let data = [ contactMax ] - writeContactsToFile data testFilePath - let contactMaxs = [ contactVasya; contactMax ] + let phoneBase = [ contactVasya; contactMax ] - writePhoneBaseToFile contactMaxs testFilePath - readContactsFromFile testFilePath |> should equal contactMaxs + writeContactsToFile phoneBase testFilePath + readContactsFromFile testFilePath |> should equal phoneBase [] let ``Write contactMaxs from file to base`` () = @@ -74,10 +72,9 @@ let ``Write contactMaxs from file to base`` () = let data = [ contactMax; contactVasya ] - let contactMaxs = [ contactMax ] writeContactsToFile data testFilePath - writePhoneBaseFromFile contactMaxs testFilePath |> should equal data + writePhoneBaseFromFile testFilePath |> should equal data [] let ``Correct phone test`` () = diff --git a/PhoneBook.Tests/Program.fs b/PhoneBook.Tests/Program.fs deleted file mode 100644 index 5fa22d1..0000000 --- a/PhoneBook.Tests/Program.fs +++ /dev/null @@ -1,4 +0,0 @@ -module Program = - - [] - let main _ = 0 diff --git a/PhoneBook/PhoneBook.fs b/PhoneBook/PhoneBook.fs index d220225..cbec47b 100644 --- a/PhoneBook/PhoneBook.fs +++ b/PhoneBook/PhoneBook.fs @@ -27,26 +27,14 @@ module PhoneBook = File.WriteAllText(phoneBaseFile, json) - let isContactInFile nameForFind phoneForFind phoneBaseFile = - List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) + let isContactInFile nameForFind phoneToFind phoneBaseFile = + List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneToFind) <| readContactsFromFile phoneBaseFile - let isContactInBase nameForFind phoneForFind phoneBase = - List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneForFind) phoneBase + let isContactInBase nameForFind phoneToFind phoneBase = + List.exists (fun contact -> contact.Name = nameForFind || contact.Phone = phoneToFind) phoneBase - let writePhoneBaseToFile (phoneBase: Contact List) phoneBaseFile = - writeContactsToFile - (List.filter (fun contact -> not (isContactInFile contact.Name contact.Phone phoneBaseFile)) phoneBase) - phoneBaseFile - - let writePhoneBaseFromFile (phoneBase: Contact List) phoneBaseFile = - let baseInFile = readContactsFromFile phoneBaseFile - - let newPhoneBase = - phoneBase - @ (List.filter (fun contact -> not (isContactInBase contact.Name contact.Phone phoneBase)) baseInFile) - - newPhoneBase + let writePhoneBaseFromFile phoneBaseFile = readContactsFromFile phoneBaseFile let isCorrectPhone (phone: string) = let pattern = @"^[0-9]+$" diff --git a/PhoneBook/Program.fs b/PhoneBook/Program.fs index e31dba9..767281a 100644 --- a/PhoneBook/Program.fs +++ b/PhoneBook/Program.fs @@ -9,21 +9,26 @@ let printInstructions () = printfn "3. Find contact by name" printfn "4. Print current phone book" printfn "5. Print contacts from file" - printfn "6. Add contacts from file to current phone book" - printfn "7. Add contacts from current phone book to file" - printfn "8. Exit" + printfn "6. Add contacts from current phone book to file" + printfn "7. Exit" let phoneBaseFile = "PhoneBase.txt" let stringContact contact = - sprintf "%s - %s" contact.Name contact.Phone + printfn "%s - %s" contact.Name contact.Phone let printContactList (contactList: Contact list) = if List.isEmpty contactList then printfn "There are no contacts\n" else - for contact in contactList do - printfn "%s" (stringContact contact) + let rec printContactListInternal ls = + match ls with + | h :: tail -> + stringContact h + printContactListInternal tail + | _ -> () + + printContactListInternal contactList let rec enterCorrectPhone () = printfn "Enter phone: " @@ -50,7 +55,9 @@ let addNewRecord (phoneBase: Contact list) = let printFindingRes (result: Contact option) = match result with - | Some contact -> printfn "Contact was found: %s" (stringContact contact) + | Some contact -> + printfn "Contact was found: " + stringContact contact | None -> printfn "Contact wasn't found!" let findPhoneByName (phoneBase: Contact list) = @@ -70,13 +77,10 @@ let printContactsFromFile () = printfn "Contacts in file:" printContactList (readContactsFromFile phoneBaseFile) -let addContactsFromFile (phoneBase: Contact list) = - writePhoneBaseFromFile phoneBase phoneBaseFile - let addContactsToFile (phoneBase: Contact list) = - writePhoneBaseToFile phoneBase phoneBaseFile + writeContactsToFile phoneBase phoneBaseFile -let Run = +let run = let rec internalRun (phoneBase: Contact list) = printInstructions () printfn "Enter command: " @@ -99,14 +103,11 @@ let Run = printContactsFromFile () internalRun phoneBase | "6" -> - let newPhoneBase = addContactsFromFile phoneBase - internalRun newPhoneBase - | "7" -> addContactsToFile phoneBase internalRun phoneBase - | "8" -> printfn "Exit" + | "7" -> printfn "Exit" | _ -> printfn "Incorrect command!\n" internalRun phoneBase - internalRun [] + internalRun (writePhoneBaseFromFile phoneBaseFile) From 5c92b2288231d22ca97e15ae1a0b07f76261e88d Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Tue, 26 Nov 2024 21:42:08 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PhoneBook.Tests/.DS_Store | Bin 6148 -> 0 bytes PhoneBook/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 PhoneBook.Tests/.DS_Store delete mode 100644 PhoneBook/.DS_Store diff --git a/PhoneBook.Tests/.DS_Store b/PhoneBook.Tests/.DS_Store deleted file mode 100644 index 99a7005889933a9e5114c53510f14e7a9f53d655..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKL2uJA6n<_CO*bJGX`o#uMdDhOj!l)OT|zbvyG>+*!$2jWWedyfs!7*FRVioq zF`e$w-2Kfm{Ee~#>!h(tIYZWFbM$b&N0S}0Zs_p{!Snzb~6 zf^(>kNv0CCIGvM!fdSsTIwh2nJj#~#?O+8NDq@&rOy>Xyq5vOuA(E;gj1ebSjQ#n+# zP)+49*Rhf5@LFDL(B7QSxBI&tf2Y@9bo}|=i(bdy-RUnDE$_**=lib*7uls$UkvdA z-=vmD7H{AyDt0wj(L`oSp25Q2byDEsbA^d-=()|rQ#z(|cuE>k2vl@IT{=N4rvlog zPbkMUK~E)@*sA1uydqa6)ADQt&;Iso){ucmIzt)Lr5U{v^P>`jrW412W8hvhV9!Ty z{a&-+7Vj8v46I^+&j%mM7+5R~>Z1dNegXg+Fl#|w|NH}eHUI;Qr9oIA!h`}%sIX5A zVc!nIgrh&O{-r?^PC}22Ja%MZUns&pJ%q2_NdyL6=@@VfEHh9y%{Jfvum1f0Uk-9D z$ADwtzhXc%4x_^mOR{(CoyGB8>q9?4S=g^M_&Wp@eHFu(ui{Op7TA5R00WDqL0BN} OM?lly3dg`-W#AY7a*qH2 diff --git a/PhoneBook/.DS_Store b/PhoneBook/.DS_Store deleted file mode 100644 index cb5201d788987ec8cccf92cd09a87f810e9b2ddd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKK~LK-6n<{Ik;0I27!sFBk#?x6=_iQ7zpj~2Ctd&fp^~E{ll`G4WW7F8 zPCsX|rzVja%U+?mu?cuhui5ptr_tON>=O`1pTK2Rx)?j`%G@Y%q z0(O6R8!Np^w>YPOQ{XWQ@b@8rGlmvRgZk5fLSF%Z4RmWmUH|+81MUEZ7E6QZfeAwe z8mh2Y3}NpM!q5>9ZGLIc&`Ic-F^-*C*c*zlR}T^BbP}OKS2_ip0*eaNP1EP||M=ni z|02n?oB~dPl~O=7j^d*TOR{I{y~Xia8^FK7**LE>s3|DyajYwR6mP?|Ar^227+Ne1 Sq6g-F1hfpUa0>iW1^xnoVwrmY From e51385719c2b61cd2eb77d085f7f17436d159c74 Mon Sep 17 00:00:00 2001 From: Ksenia Nigmatulina Date: Tue, 26 Nov 2024 21:49:04 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20point=20free?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BracketSequence.Tests.sln | 25 +++++++++++++++++++ PointFree.Tests/PointFreeTest.fs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 BracketSequence.Tests/BracketSequence.Tests.sln diff --git a/BracketSequence.Tests/BracketSequence.Tests.sln b/BracketSequence.Tests/BracketSequence.Tests.sln new file mode 100644 index 0000000..0dde088 --- /dev/null +++ b/BracketSequence.Tests/BracketSequence.Tests.sln @@ -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 diff --git a/PointFree.Tests/PointFreeTest.fs b/PointFree.Tests/PointFreeTest.fs index 30804d6..5bab7cf 100644 --- a/PointFree.Tests/PointFreeTest.fs +++ b/PointFree.Tests/PointFreeTest.fs @@ -13,4 +13,4 @@ let ``Functions are equal`` () = && (func'3 x l = func'4 x l) && (func'4 x l = func'5 x l) - Check.Quick functionResAreEq + Check.QuickThrowOnFailure functionResAreEq