-
Notifications
You must be signed in to change notification settings - Fork 0
Final test is partly ready #15
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?
Changes from all commits
6fefd78
e882792
e878805
f2feec4
4e6e66e
d742503
7132046
99b9f14
88a015a
7d39cb0
038c013
90adc97
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,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.30128.74 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FinalTest", "FinalTest\FinalTest.fsproj", "{6352CF47-E6E1-4AE3-8707-5CDFA00A5AFC}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FinalTestTests", "FinalTestTests\FinalTestTests.fsproj", "{C22E1080-C329-46B1-938F-0EAA891AEC7B}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {6352CF47-E6E1-4AE3-8707-5CDFA00A5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {6352CF47-E6E1-4AE3-8707-5CDFA00A5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {6352CF47-E6E1-4AE3-8707-5CDFA00A5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {6352CF47-E6E1-4AE3-8707-5CDFA00A5AFC}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {C22E1080-C329-46B1-938F-0EAA891AEC7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {C22E1080-C329-46B1-938F-0EAA891AEC7B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {C22E1080-C329-46B1-938F-0EAA891AEC7B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {C22E1080-C329-46B1-938F-0EAA891AEC7B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {DE94B474-4BEA-4E30-9C38-46640313C9A5} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module EvenFibonacci | ||
|
|
||
| /// Sum of even fibonacci numbers that are lower than a million. | ||
| let evenFibonacci () = | ||
| let rec fibonacci prev cur acc = | ||
| if cur < 1000000 && cur % 2 = 0 then fibonacci cur (prev + cur) (acc + cur) | ||
| elif cur < 1000000 then fibonacci cur (prev + cur) acc | ||
| else acc | ||
| fibonacci 1 1 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="EvenFibonacci.fs" /> | ||
| <Compile Include="Queue.fs" /> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Learn more about F# at http://fsharp.org | ||
|
|
||
| open System | ||
| open Queue | ||
|
|
||
| [<EntryPoint>] | ||
| let main argv = | ||
|
|
||
| 0 // return an integer exit code |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| module Queue | ||
|
|
||
| /// Type implements queue data structure. | ||
| type Queue<'a> () = | ||
| let mutable maxSize = 100 | ||
|
|
||
| let mutable elements = Array.zeroCreate<'a> (maxSize) | ||
|
|
||
| let mutable count = 0 | ||
|
|
||
| /// Resizes the queue. | ||
| let resize () = | ||
| maxSize <- maxSize + 50 | ||
| let mutable temp = elements | ||
| elements <- Array.zeroCreate<'a> (maxSize) | ||
|
|
||
| let copy (whatCopy : 'a[]) = | ||
| for i in 0..(maxSize - 1 - 50) do | ||
| elements.[i] <- whatCopy.[i] | ||
|
|
||
| copy temp | ||
|
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. А Вы уверены, что массив --- хорошая структура данных для этой задачи? :) Или Вы хотели очередь на кольцевом массиве, но что-то пошло не так? |
||
|
|
||
| /// Makes shift in array after dequeue. | ||
| let shift () = | ||
| for i in 1..(maxSize - 1) do | ||
| elements.[i - 1] <- elements.[i] | ||
|
|
||
| /// Peeks the first element in the queue. | ||
| member this.Peek () = | ||
| if count = 0 then failwith "The queue is empty!" | ||
| else elements.[0] | ||
|
|
||
| /// Enqueue an element. | ||
| member this.Enqueue value = | ||
| if count = maxSize then | ||
| resize() | ||
| else | ||
| elements.[count] <- value | ||
| count <- count + 1 | ||
|
|
||
| /// Dequeue the first element. | ||
| member this.Dequeue () = | ||
| if count = 0 then failwith "The queue is empty!" | ||
| else | ||
| shift () | ||
| count <- count - 1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| module EvenFibonacciTests | ||
|
|
||
| open EvenFibonacci | ||
| open FsUnit | ||
| open NUnit.Framework | ||
|
|
||
| let checkSumEvenFibonacci () = | ||
| let fibonacci n = | ||
| let rec fibonacciRec n prev prevPrev count = | ||
| if n = 1 || n = 2 then 1 | ||
| elif n = count then prev + prevPrev | ||
| else fibonacciRec n (prev + prevPrev) prev (count + 1) | ||
| fibonacciRec n 1 1 3 | ||
|
|
||
| let mutable i = 1 | ||
| let mutable sum = 0 | ||
|
|
||
| while (fibonacci i) < 1000000 do | ||
|
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. Это каждое число Фибоначчи заново с нуля считается, хотя они все легко вычисляются через друг друга. Можно через seq и yield :) |
||
| if (fibonacci i) % 2 = 0 then | ||
| sum <- sum + fibonacci i | ||
| i <- i + 1 | ||
| sum | ||
|
|
||
| [<Test>] | ||
| let ``Sum of even fibonacci lower than 1 000 000 test`` () = | ||
| evenFibonacci () |> should equal (checkSumEvenFibonacci ()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>true</GenerateProgramFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsUnit" Version="3.8.1" /> | ||
| <PackageReference Include="nunit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.16.1"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="EvenFibonacciTests.fs" /> | ||
| <Compile Include="QueueTests.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\FinalTest\FinalTest.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Update="FSharp.Core" Version="4.7.2" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| module QueueTests | ||
|
|
||
| open NUnit.Framework | ||
| open FsUnit | ||
| open Queue | ||
| open System | ||
|
|
||
| [<Test>] | ||
| let ``Enqueue and peek test 1`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(1) | ||
| q.Peek () |> should equal 1 | ||
|
|
||
| [<Test>] | ||
| let ``Enqueue and peek test 2`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(1) | ||
| q.Enqueue(1) | ||
| q.Peek () |> should equal 1 | ||
|
|
||
| [<Test>] | ||
| let ``Enqueue and peek test 3`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(1) | ||
| q.Enqueue(2) | ||
| q.Peek () |> should equal 1 | ||
|
|
||
| [<Test>] | ||
| let ``Enqueue and peek test 4`` () = | ||
| let q = new Queue<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. Это можно было бы вынести в SetUp |
||
| q.Enqueue(5) | ||
| q.Enqueue(-7) | ||
| q.Enqueue(0) | ||
| q.Peek() |> should equal 5 | ||
|
|
||
| [<Test>] | ||
| let ``Dequeue and peek test 1`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(1) | ||
| q.Dequeue () | ||
| q.Enqueue(1) | ||
| q.Peek () |> should equal 1 | ||
|
|
||
| [<Test>] | ||
| let ``Dequeue from empty queue test 1`` () = | ||
| let q = new Queue<int>() | ||
| (fun () -> q.Dequeue () |> ignore) |> should throw typeof<Exception> | ||
|
|
||
| [<Test>] | ||
| let ``Dequeue from empty queue test 2`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(-9) | ||
| q.Dequeue () |> ignore | ||
| (fun () -> q.Dequeue () |> ignore) |> should throw typeof<Exception> | ||
|
|
||
| [<Test>] | ||
| let ``Peek with empty queue test 1`` () = | ||
| let q = new Queue<int>() | ||
| (fun () -> q.Peek () |> ignore) |> should throw typeof<Exception> | ||
|
|
||
| [<Test>] | ||
| let ``Peek with empty queue test 2`` () = | ||
| let q = new Queue<int>() | ||
| q.Enqueue(-9) | ||
| q.Dequeue () |> ignore | ||
| (fun () -> q.Peek() |> ignore) |> should throw typeof<Exception> | ||
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.
Если уж делать resize, то вдвое (это влияет на асимптотику --- есть большая разница, прибавлять 50 или умножать на 2)