diff --git a/hw2/hw2.Test/FunctionsTest.fs b/hw2/hw2.Test/FunctionsTest.fs new file mode 100644 index 0000000..0563a39 --- /dev/null +++ b/hw2/hw2.Test/FunctionsTest.fs @@ -0,0 +1,90 @@ +module hw2.Test + +open NUnit.Framework +open FsCheck +open FsUnit +open Program + +[] +let countEvenNumbersCheckWithNumbers () = + let result = countEvenNumbersFilter [1; 2; 3; 4; 5; 6] + + result |> should equal 3 + +[] +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 + +[] +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)))) + +[] +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))) + +[] +let countTreeTestSum () = + let tree1 = TreeArithmetic.Node(Summation, Leaf 2, Node(Summation, Leaf 1, Leaf -1)) + + let result = count tree1 + + result |> should equal 2 + +[] +let countTreeTestSub () = + let tree1 = TreeArithmetic.Node(Subtracting, Leaf 15, Node(Subtracting, Leaf 10, Leaf -5)) + + let result = count tree1 + + result |> should equal 0 + +[] +let countTreeTestMult () = + let tree1 = TreeArithmetic.Node(Multiplication, Leaf -2, Node(Multiplication, Leaf 2, Leaf 4)) + + let result = count tree1 + + result |> should equal -16 + +[] +let countTreeTestDiv () = + let tree1 = TreeArithmetic.Node(Division, Leaf -10, Node(Division, Leaf 10, Leaf 5)) + + let result = count tree1 + + result |> should equal -5 + +[] +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 + + +[] +let primeNumbersTest () = + let first7PrimeNums = [ for i in 0 .. 7 -> Seq.item i (primeNumbers()) ] + + first7PrimeNums |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19] + \ No newline at end of file diff --git a/hw2/hw2.Test/hw2.Test.fsproj b/hw2/hw2.Test/hw2.Test.fsproj new file mode 100644 index 0000000..ff61b4a --- /dev/null +++ b/hw2/hw2.Test/hw2.Test.fsproj @@ -0,0 +1,27 @@ + + + + net6.0 + + false + false + + + + + + + + + + + + + + + + + + + + diff --git a/hw2/hw2.sln b/hw2/hw2.sln new file mode 100644 index 0000000..4bddd07 --- /dev/null +++ b/hw2/hw2.sln @@ -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 diff --git a/hw2/hw2/Program.fs b/hw2/hw2/Program.fs new file mode 100644 index 0000000..d55dcfe --- /dev/null +++ b/hw2/hw2/Program.fs @@ -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 [] \ No newline at end of file diff --git a/hw2/hw2/hw2.fsproj b/hw2/hw2/hw2.fsproj new file mode 100644 index 0000000..761405f --- /dev/null +++ b/hw2/hw2/hw2.fsproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0 + Windows + + + + + + +