-
Notifications
You must be signed in to change notification settings - Fork 0
ProgressCheck1 #6
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
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,5 @@ | ||
| module Program = | ||
|
|
||
| [<EntryPoint>] | ||
| let main _ = 0 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="UnitTests.fs" /> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
| <PackageReference Include="NUnit" Version="4.1.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="4.2.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.0"> | ||
| <GeneratePathProperty></GeneratePathProperty> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\ProgressCheck1\ProgressCheck1.fsproj" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||
| module ProgressCheck1.Tests | ||||||
|
|
||||||
| open NUnit.Framework | ||||||
| open FsUnit | ||||||
| open ProgressCheck1.Fibonachi | ||||||
| open ProgressCheck1.PrintSquare | ||||||
| open ProgressCheck1.PriorityQueue | ||||||
|
|
||||||
| [<Test>] | ||||||
| let TestFibonachi () = | ||||||
| countSumFibEven() |> should equal 1089154 | ||||||
|
|
||||||
| let squaresCases = | ||||||
| seq{ | ||||||
|
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.
Suggested change
|
||||||
| TestCaseData(4, "****\n* *\n* *\n****") | ||||||
| TestCaseData(5, "*****\n* *\n* *\n* *\n*****") | ||||||
| } | ||||||
|
|
||||||
| [<TestCaseSource("squaresCases")>] | ||||||
| let TestSquare (n : int, answer : string) = | ||||||
| PrintSquare n |> should equal answer | ||||||
|
|
||||||
| let queue = new PriorityQueue<int>(fun n1 n2 -> compare n1 n2) | ||||||
|
|
||||||
| [<Test>] | ||||||
| let TestEnqueue () = | ||||||
| queue.Enqueue(1) | ||||||
| assert not queue.IsEmpty | ||||||
|
|
||||||
| [<Test>] | ||||||
| let ``dequeue from empty queue throws exception`` () = | ||||||
| queue.Clear() | ||||||
| (fun () -> queue.Dequeue() |> ignore) |> should throw typeof<System.Exception> | ||||||
|
|
||||||
| [<Test>] | ||||||
| let ``peek with empty queue throws exception`` () = | ||||||
| queue.Clear() | ||||||
| (fun () -> queue.Peek() |> ignore) |> should throw typeof<System.Exception> | ||||||
|
|
||||||
| [<SetUp>] | ||||||
| let fillQueue () = | ||||||
| queue.Clear() | ||||||
| queue.Enqueue(1) | ||||||
| queue.Enqueue(-1) | ||||||
| queue.Enqueue(2) | ||||||
|
|
||||||
| [<Test>] | ||||||
| let TestDequeue () = | ||||||
| queue.Dequeue() |> should equal 2 | ||||||
|
|
||||||
| [<Test>] | ||||||
| let TestPeek () = | ||||||
| queue.Peek() |> should equal 2 | ||||||
| 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.10 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ProgressCheck1", "ProgressCheck1\ProgressCheck1.fsproj", "{47233943-791F-4600-B5F7-872EAF50CB35}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ProgressCheck1.Tests", "ProgressCheck1.Tests\ProgressCheck1.Tests.fsproj", "{D5C87B10-390D-4421-A91E-19BA38B2439B}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {47233943-791F-4600-B5F7-872EAF50CB35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {47233943-791F-4600-B5F7-872EAF50CB35}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {47233943-791F-4600-B5F7-872EAF50CB35}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {47233943-791F-4600-B5F7-872EAF50CB35}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {D5C87B10-390D-4421-A91E-19BA38B2439B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {D5C87B10-390D-4421-A91E-19BA38B2439B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {D5C87B10-390D-4421-A91E-19BA38B2439B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {D5C87B10-390D-4421-A91E-19BA38B2439B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {94AE1E0B-85E1-46BC-8F1F-D7EA6DDE84D2} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace ProgressCheck1 | ||
|
|
||
| module Fibonachi = | ||
|
|
||
| let countSumFibEven () = | ||
| let rec countSum n1 n2 sum = | ||
| match n1 with | ||
| | (n : int) when (n >= 1000000) -> sum | ||
|
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. тут необязательно указывать типы |
||
| | (n : int) when (n % 2 = 0) -> countSum n2 (n2 + n1) sum | ||
| | _ -> countSum n2 (n2 + n1) (sum + n1) | ||
|
Comment on lines
+9
to
+10
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. можно объединить ветки, разделив только логику вычисления аккумулятора |
||
| countSum 1 1 0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| namespace ProgressCheck1 | ||
|
|
||
| module PriorityQueue = | ||
|
|
||
| type PriorityQueue<'T>(comparer : 'T -> 'T -> int) = | ||
|
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 mutable items = [] | ||
|
|
||
| let lockObject = obj | ||
|
|
||
| member this.Clear() = | ||
| items <- [] | ||
|
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. Так же нужно обернуть в lock |
||
|
|
||
| member this.Enqueue(item : 'T) = | ||
| let rec insert items x = | ||
| match items with | ||
| | [] -> [x] | ||
| | y::tail when comparer x y > 0 -> x::y::tail | ||
| | y::tail -> y::insert tail x | ||
| lock lockObject (fun () -> (items <- insert items item)) | ||
|
|
||
| member this.Dequeue() = | ||
| lock lockObject (fun () -> | ||
| match items with | ||
| | [] -> failwith "Queue is empty" | ||
| | head::tail -> | ||
| items <- tail | ||
| head) | ||
|
|
||
| member this.Peek() = | ||
| lock lockObject (fun () -> | ||
| match items with | ||
| | [] -> failwith "Queue is empty" | ||
| | head::_ -> head | ||
| ) | ||
|
|
||
| member this.IsEmpty = | ||
| lock lockObject (fun () -> (List.length items = 0)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Fibonachi.fs" /> | ||
| <Compile Include="PriorityQueue.fs" /> | ||
| <Compile Include="SquarePrinter.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace ProgressCheck1 | ||
|
|
||
| module PrintSquare = | ||
|
|
||
| let PrintSquare n = | ||
|
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 rec makeInnerString str iter n = | ||
| match iter with | ||
| | (p : int) when (p = n) -> str | ||
|
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 излишний, тут структурного сопоставления нигде не делается. Это лучше было бы записать как цепочку if-ов, хотя дело вкуса |
||
| | (p : int) when (p = 0 || p = n - 1) -> | ||
| makeInnerString (String.concat "" [str; "*"]) (iter + 1) n | ||
| | _ -> makeInnerString (String.concat "" [str; " "]) (iter + 1) n | ||
|
|
||
| let rec makeBoundaries str iter n = | ||
| match iter with | ||
| | (p : int) when (p = n) -> str | ||
| | _ -> makeBoundaries (String.concat "" [str; "*"]) (iter + 1) n | ||
|
|
||
| let boundary = makeBoundaries "" 0 n | ||
| let inner = makeInnerString "" 0 n | ||
|
|
||
| let rec makeSquare square iter n = | ||
| match iter with | ||
| | (p : int) when (p = n - 1) -> (String.concat "\n" [square; boundary]) | ||
| | _ -> makeSquare (String.concat "\n" [square; inner]) (iter + 1) n | ||
|
|
||
| makeSquare boundary 1 n | ||
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.
Он Fibonacci на самом деле