-
Notifications
You must be signed in to change notification settings - Fork 0
hw 2 #2
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
Open
MikePuzanov
wants to merge
4
commits into
master
Choose a base branch
from
hw2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hw 2 #2
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| module hw2.Test | ||
|
|
||
| open NUnit.Framework | ||
| open FsCheck | ||
| open FsUnit | ||
| open Program | ||
|
|
||
| [<Test>] | ||
| let countEvenNumbersCheckWithNumbers () = | ||
| let result = countEvenNumbersFilter [1; 2; 3; 4; 5; 6] | ||
|
|
||
| result |> should equal 3 | ||
|
|
||
| [<Test>] | ||
| let countEvenNumbersCheckEqual () = | ||
| let check x = | ||
| let mapFilter = countEvenNumbersMap x = countEvenNumbersFilter x | ||
| let mapFold = countEvenNumbersMap x = countEvenNumbersFold x | ||
| let foldFilter = countEvenNumbersFold x = countEvenNumbersFilter x | ||
| match mapFilter, mapFold, foldFilter with | ||
| | true, true, true -> true | ||
| | _ -> false | ||
|
|
||
| Check.QuickThrowOnFailure check | ||
|
|
||
| [<Test>] | ||
| let funcToTreeWhenRightSubtreeIsNotEmpty () = | ||
| let tree = Tree.Tree(2, Tree.Tip(4), Tree.Tree(2, Tree.Tree(4, Tree.Tip(4), Tree.Tip(4)), Tree.Tip(4))) | ||
|
|
||
| let newTree = funcToTree tree (fun x -> x * 2) | ||
|
|
||
| newTree |> should equal (Tree.Tree(4, Tree.Tip(8), Tree.Tree(4, Tree.Tree(8, Tree.Tip(8), Tree.Tip(8)), Tree.Tip(8)))) | ||
|
|
||
| [<Test>] | ||
| let funcToTreeTest () = | ||
| let tree = Tree.Tree(2, Tree.Tip(4), Tree.Tip(6)) | ||
|
|
||
| let newTree = funcToTree tree (fun x -> x * 2) | ||
|
|
||
| newTree |> should equal (Tree(4, Tree.Tip(8), Tree.Tip(12))) | ||
|
|
||
| [<Test>] | ||
| let countTreeTestSum () = | ||
| let tree1 = TreeArithmetic.Node(Summation, Leaf 2, Node(Summation, Leaf 1, Leaf -1)) | ||
|
|
||
| let result = count tree1 | ||
|
|
||
| result |> should equal 2 | ||
|
|
||
| [<Test>] | ||
| let countTreeTestSub () = | ||
| let tree1 = TreeArithmetic.Node(Subtracting, Leaf 15, Node(Subtracting, Leaf 10, Leaf -5)) | ||
|
|
||
| let result = count tree1 | ||
|
|
||
| result |> should equal 0 | ||
|
|
||
| [<Test>] | ||
| let countTreeTestMult () = | ||
| let tree1 = TreeArithmetic.Node(Multiplication, Leaf -2, Node(Multiplication, Leaf 2, Leaf 4)) | ||
|
|
||
| let result = count tree1 | ||
|
|
||
| result |> should equal -16 | ||
|
|
||
| [<Test>] | ||
| let countTreeTestDiv () = | ||
| let tree1 = TreeArithmetic.Node(Division, Leaf -10, Node(Division, Leaf 10, Leaf 5)) | ||
|
|
||
| let result = count tree1 | ||
|
|
||
| result |> should equal -5 | ||
|
|
||
| [<Test>] | ||
| let countTreeTestWithAll () = | ||
| let tree1 = TreeArithmetic.Node(Division, | ||
| Node(Summation, Node(Subtracting, Leaf 35, Leaf -15), Leaf 50), | ||
| Node(Multiplication, Leaf 10, Leaf 5)) | ||
|
|
||
| let result = count tree1 | ||
|
|
||
| result |> should equal 2 | ||
|
|
||
|
|
||
| [<Test>] | ||
| let primeNumbersTest () = | ||
| let first7PrimeNums = [ for i in 0 .. 7 -> Seq.item i (primeNumbers()) ] | ||
|
|
||
| first7PrimeNums |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <GenerateProgramFile>false</GenerateProgramFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="FunctionsTest.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FsCheck" Version="3.0.0-beta2" /> | ||
| <PackageReference Include="FsUnit" Version="4.2.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
| <PackageReference Include="NUnit" Version="3.13.2" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\hw2\hw2.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "hw2", "hw2\hw2.fsproj", "{E29CE84F-A450-4B11-B9D1-195F5D0C46B6}" | ||
| EndProject | ||
| Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "hw2.Test", "hw2.Test\hw2.Test.fsproj", "{A878DF09-0A2D-4982-AECE-235EFEFEB545}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E29CE84F-A450-4B11-B9D1-195F5D0C46B6}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {A878DF09-0A2D-4982-AECE-235EFEFEB545}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A878DF09-0A2D-4982-AECE-235EFEFEB545}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A878DF09-0A2D-4982-AECE-235EFEFEB545}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A878DF09-0A2D-4982-AECE-235EFEFEB545}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| open System | ||
|
|
||
| // подсчитывает количество чётных чисел в списке c filter | ||
| let countEvenNumbersFilter list = | ||
| list |> List.filter (fun x -> x % 2 = 0) |> List.length | ||
|
|
||
| // подсчитывает количество чётных чисел в списке c map | ||
| let countEvenNumbersMap list = | ||
| list |> List.map (fun x -> (abs x + 1) % 2) |> List.sum | ||
|
|
||
| // подсчитывает количество чётных чисел в списке c fold | ||
| let countEvenNumbersFold list = | ||
| (0, list) ||> List.fold (fun acc x -> acc + (abs x + 1) % 2) | ||
|
|
||
| // бинарное дерево | ||
| type Tree<'a> = | ||
| | Tree of 'a * Tree<'a> * Tree<'a> | ||
| | Tip of 'a | ||
|
|
||
| // функция, которая применяет функцию к каждому элементу дерева | ||
| let rec funcToTree tree func = | ||
| match tree with | ||
| | Tip (value) -> Tip(func value) | ||
| | Tree (value, left, right) -> Tree(func value, funcToTree left func, funcToTree right func) | ||
|
|
||
| // все операции | ||
| type Operation = | ||
| | Summation | ||
| | Subtracting | ||
| | Multiplication | ||
| | Division | ||
|
|
||
| // арифметическое дерево | ||
| type TreeArithmetic<'t> = | ||
| | Leaf of 't | ||
| | Node of Operation * TreeArithmetic<'t> * TreeArithmetic<'t> | ||
|
|
||
| // функция, реализующая подсчет арифмитического дерева | ||
| let rec count tree = | ||
| match tree with | ||
| | Leaf x -> x | ||
| | Node(operation, left, right) -> | ||
| let left = count left | ||
| let right = count right | ||
| match operation with | ||
| | Summation -> left + right | ||
| | Subtracting -> left - right | ||
| | Multiplication -> left * right | ||
| | Division -> left / right | ||
|
|
||
| // выводит бесконечную последовательность простых чисел | ||
| let primeNumbers () = | ||
| let rec testOnPrime element list = | ||
| match list with | ||
| | [] -> true | ||
| | h :: tail -> | ||
| if element % h = 0 then false else testOnPrime element tail | ||
| let rec sequence element list = | ||
| seq { | ||
| if testOnPrime element list then | ||
| yield element | ||
| yield! sequence (element + 1) (element :: list) | ||
| else | ||
| yield! sequence (element + 1) list | ||
| } | ||
| sequence 2 [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Program.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
было бы так же неплохо проверить на корректность одну из функций. А то окажется, что все функции эквивалентны, но некорректны.