From 91a382aaf15c6bfbc4139a8c74a0c74738e5dcff Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sun, 6 Mar 2022 23:15:51 +0300 Subject: [PATCH 1/4] tests not ready --- hw2/hw2.Test/Program.fs | 4 +++ hw2/hw2.Test/UnitTest1.fs | 29 ++++++++++++++++ hw2/hw2.Test/hw2.Test.fsproj | 28 +++++++++++++++ hw2/hw2.sln | 22 ++++++++++++ hw2/hw2/Program.fs | 66 ++++++++++++++++++++++++++++++++++++ hw2/hw2/hw2.fsproj | 13 +++++++ 6 files changed, 162 insertions(+) create mode 100644 hw2/hw2.Test/Program.fs create mode 100644 hw2/hw2.Test/UnitTest1.fs create mode 100644 hw2/hw2.Test/hw2.Test.fsproj create mode 100644 hw2/hw2.sln create mode 100644 hw2/hw2/Program.fs create mode 100644 hw2/hw2/hw2.fsproj diff --git a/hw2/hw2.Test/Program.fs b/hw2/hw2.Test/Program.fs new file mode 100644 index 0000000..176a7b6 --- /dev/null +++ b/hw2/hw2.Test/Program.fs @@ -0,0 +1,4 @@ +module Program = + + [] + let main _ = 0 diff --git a/hw2/hw2.Test/UnitTest1.fs b/hw2/hw2.Test/UnitTest1.fs new file mode 100644 index 0000000..7154e32 --- /dev/null +++ b/hw2/hw2.Test/UnitTest1.fs @@ -0,0 +1,29 @@ +module hw2.Test + +open NUnit.Framework +open FsCheck +open FsUnit +open Program +open Program + +[] +let funcToTreeTest () = + let tree1 = Tree.Tree(2, Tree.Tree(4, Tree.Empty, Tree.Empty), Tree.Tree(6, Tree.Empty, Tree.Empty)) + + let newTree = funcToTree tree1 (fun x -> x * 2) + + newTree |> should equal (Tree.Tree(4, Tree.Tree(8, Tree.Empty, Tree.Empty), Tree.Tree(12, Tree.Empty, Tree.Empty))) + +[] +let countTreeTest () = + let tree1 = ArithmeticTree.Node(Multi, Leaf 2, Node(Sum, Leaf 1, Leaf 1)) + + let result = count tree1 + + result |> should equal 4 + +[] +let primeNumbersTest () = + let primeNums10 = [ for i in 0 .. 9 -> Seq.item i primeNumbers ] + + primeNums10 |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19; 23; 29 ] \ 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..bdc9daa --- /dev/null +++ b/hw2/hw2.Test/hw2.Test.fsproj @@ -0,0 +1,28 @@ + + + + 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..043c4b7 --- /dev/null +++ b/hw2/hw2/Program.fs @@ -0,0 +1,66 @@ +open System + +let countEvenNumbersFilter list = + (List.filter (fun x -> x % 2 = 0) list).Length + +let countEvenNumbersMap list = + list |> List.map (fun x -> (x + 1) % 2) |> List.sum + +let countEvenNumbersFold list = + List.fold (fun x acc -> (x + 1) % 2 + acc) 0 list + +printfn "%d" (countEvenNumbersFilter [1; 3; 5; 7; 9]) +printfn "%A" (countEvenNumbersFilter [1; 3; 5; 7; 9]) +printfn "%A" (countEvenNumbersFilter [1; 3; 5; 7; 9]) + +type Tree<'a> = +| Tree of 'a * Tree<'a> * Tree<'a> +| Empty + +let rec funcToTree tree func = + match tree with + | Empty -> Empty + | Tree (value, left, right) -> Tree(func value, funcToTree left func, funcToTree right func) + +type Operation = + | Sum + | Sub + | Multi + | Div + +type ArithmeticTree<'t> = + | Leaf of 't + | Node of Operation * ArithmeticTree<'t> * ArithmeticTree<'t> + +let reс count tree = + match tree with + | Leaf x -> x + | Node(operation, left, right) -> + let left = count left + let right = count right + match operation with + | Sum -> left + right + | Sub -> left - right + | Multi -> left * right + | Div when right <> 0 -> left / right + | Div when right = 0 -> raise(ArgumentNullException "На ноль делить нельзя") + +let primeNumbers = + let rec test element list = + match list with + | [] -> true + | h :: tail when (float h <= sqrt(element |> float) && element % h = 0) -> false + | h :: tail when element % h <> 0 -> test element tail + | _ -> false + + let rec sequence element list = + seq { + if test element list then + yield element + yield! sequence (element + 1) (element :: list) + else + yield! sequence (element + 1) list + } + sequence 2 [] + +printfn "%A" primeNumbers \ 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 + + + + + + + From 6ed216c136992f29746c8f06c2c8255bfa235af8 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Mon, 7 Mar 2022 23:30:10 +0300 Subject: [PATCH 2/4] added one test --- hw2/hw2.Test/FunctionsTest.fs | 42 +++++++++++++++++++++++++++++++++++ hw2/hw2.Test/Program.fs | 4 ---- hw2/hw2.Test/UnitTest1.fs | 29 ------------------------ hw2/hw2.Test/hw2.Test.fsproj | 3 +-- hw2/hw2/Program.fs | 35 ++++++++++++++++------------- 5 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 hw2/hw2.Test/FunctionsTest.fs delete mode 100644 hw2/hw2.Test/Program.fs delete mode 100644 hw2/hw2.Test/UnitTest1.fs diff --git a/hw2/hw2.Test/FunctionsTest.fs b/hw2/hw2.Test/FunctionsTest.fs new file mode 100644 index 0000000..5c7a5d3 --- /dev/null +++ b/hw2/hw2.Test/FunctionsTest.fs @@ -0,0 +1,42 @@ +module hw2.Test + +open NUnit.Framework +open FsCheck +open FsUnit +open Program + +[] +let countEvenNumbersCheck () = + 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 funcToTreeTest () = + let tree1 = Tree.Tree(2, Tree.Tree(4, Tree.Empty, Tree.Empty), Tree.Tree(6, Tree.Empty, Tree.Empty)) + + let newTree = funcToTree tree1 (fun x -> x * 2) + + newTree |> should equal (Tree.Tree(4, Tree.Tree(8, Tree.Empty, Tree.Empty), Tree.Tree(12, Tree.Empty, Tree.Empty))) + +[] +let countTreeTest () = + let tree1 = TreeArithmetic.Node(Multi, Leaf 2, Node(Sum, Leaf 1, Leaf 1)) + + let result = count tree1 + + result |> should equal 4 + +[] +let primeNumbersTest () = + + let number13 = primeNumbers |> Seq.item 5 + + number13 |> should equal 13 + \ No newline at end of file diff --git a/hw2/hw2.Test/Program.fs b/hw2/hw2.Test/Program.fs deleted file mode 100644 index 176a7b6..0000000 --- a/hw2/hw2.Test/Program.fs +++ /dev/null @@ -1,4 +0,0 @@ -module Program = - - [] - let main _ = 0 diff --git a/hw2/hw2.Test/UnitTest1.fs b/hw2/hw2.Test/UnitTest1.fs deleted file mode 100644 index 7154e32..0000000 --- a/hw2/hw2.Test/UnitTest1.fs +++ /dev/null @@ -1,29 +0,0 @@ -module hw2.Test - -open NUnit.Framework -open FsCheck -open FsUnit -open Program -open Program - -[] -let funcToTreeTest () = - let tree1 = Tree.Tree(2, Tree.Tree(4, Tree.Empty, Tree.Empty), Tree.Tree(6, Tree.Empty, Tree.Empty)) - - let newTree = funcToTree tree1 (fun x -> x * 2) - - newTree |> should equal (Tree.Tree(4, Tree.Tree(8, Tree.Empty, Tree.Empty), Tree.Tree(12, Tree.Empty, Tree.Empty))) - -[] -let countTreeTest () = - let tree1 = ArithmeticTree.Node(Multi, Leaf 2, Node(Sum, Leaf 1, Leaf 1)) - - let result = count tree1 - - result |> should equal 4 - -[] -let primeNumbersTest () = - let primeNums10 = [ for i in 0 .. 9 -> Seq.item i primeNumbers ] - - primeNums10 |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19; 23; 29 ] \ No newline at end of file diff --git a/hw2/hw2.Test/hw2.Test.fsproj b/hw2/hw2.Test/hw2.Test.fsproj index bdc9daa..ff61b4a 100644 --- a/hw2/hw2.Test/hw2.Test.fsproj +++ b/hw2/hw2.Test/hw2.Test.fsproj @@ -8,8 +8,7 @@ - - + diff --git a/hw2/hw2/Program.fs b/hw2/hw2/Program.fs index 043c4b7..8fb8f4d 100644 --- a/hw2/hw2/Program.fs +++ b/hw2/hw2/Program.fs @@ -1,38 +1,42 @@ open System +// подсчитывает количество чётных чисел в списке c filter let countEvenNumbersFilter list = - (List.filter (fun x -> x % 2 = 0) list).Length + list |> List.filter (fun x -> x % 2 = 0) |> List.length +// подсчитывает количество чётных чисел в списке c map let countEvenNumbersMap list = - list |> List.map (fun x -> (x + 1) % 2) |> List.sum + list |> List.map (fun x -> (abs x + 1) % 2) |> List.sum +// подсчитывает количество чётных чисел в списке c fold let countEvenNumbersFold list = - List.fold (fun x acc -> (x + 1) % 2 + acc) 0 list + (0, list) ||> List.fold (fun acc x -> acc + (abs x + 1) % 2) -printfn "%d" (countEvenNumbersFilter [1; 3; 5; 7; 9]) -printfn "%A" (countEvenNumbersFilter [1; 3; 5; 7; 9]) -printfn "%A" (countEvenNumbersFilter [1; 3; 5; 7; 9]) - +// бинарное дерево type Tree<'a> = | Tree of 'a * Tree<'a> * Tree<'a> | Empty +// функция, которая применяет функцию к каждому элементу дерева let rec funcToTree tree func = match tree with | Empty -> Empty | Tree (value, left, right) -> Tree(func value, funcToTree left func, funcToTree right func) +// все операции type Operation = | Sum | Sub | Multi | Div - -type ArithmeticTree<'t> = + +// арифметическое дерево +type TreeArithmetic<'t> = | Leaf of 't - | Node of Operation * ArithmeticTree<'t> * ArithmeticTree<'t> + | Node of Operation * TreeArithmetic<'t> * TreeArithmetic<'t> -let reс count tree = +// функция, реализующая подсчет арифмитического дерева +let rec count tree = match tree with | Leaf x -> x | Node(operation, left, right) -> @@ -44,15 +48,16 @@ let reс count tree = | Multi -> left * right | Div when right <> 0 -> left / right | Div when right = 0 -> raise(ArgumentNullException "На ноль делить нельзя") + | _ -> raise(InvalidOperationException "Такой операции не существует!") +// выводит бесконечную последовательность простых чисел let primeNumbers = let rec test element list = match list with | [] -> true - | h :: tail when (float h <= sqrt(element |> float) && element % h = 0) -> false + | h :: tail when element % h = 0 -> false | h :: tail when element % h <> 0 -> test element tail - | _ -> false - + | _ -> false let rec sequence element list = seq { if test element list then @@ -63,4 +68,4 @@ let primeNumbers = } sequence 2 [] -printfn "%A" primeNumbers \ No newline at end of file +printfn "%d" (Seq.item 5 primeNumbers) \ No newline at end of file From 65bf42119f78af71dc7c46f4a9acf406f33056a1 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Thu, 10 Mar 2022 21:24:00 +0300 Subject: [PATCH 3/4] fix last test --- hw2/hw2.Test/FunctionsTest.fs | 11 +++++------ hw2/hw2/Program.fs | 35 +++++++++++++++-------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/hw2/hw2.Test/FunctionsTest.fs b/hw2/hw2.Test/FunctionsTest.fs index 5c7a5d3..9f39d86 100644 --- a/hw2/hw2.Test/FunctionsTest.fs +++ b/hw2/hw2.Test/FunctionsTest.fs @@ -19,15 +19,15 @@ let countEvenNumbersCheck () = [] let funcToTreeTest () = - let tree1 = Tree.Tree(2, Tree.Tree(4, Tree.Empty, Tree.Empty), Tree.Tree(6, Tree.Empty, Tree.Empty)) + let tree1 = Tree.Tree(2, Tree.Tip(4), Tree.Tip(6)) let newTree = funcToTree tree1 (fun x -> x * 2) - newTree |> should equal (Tree.Tree(4, Tree.Tree(8, Tree.Empty, Tree.Empty), Tree.Tree(12, Tree.Empty, Tree.Empty))) + newTree |> should equal (Tree(4, Tree.Tip(8), Tree.Tip(12))) [] let countTreeTest () = - let tree1 = TreeArithmetic.Node(Multi, Leaf 2, Node(Sum, Leaf 1, Leaf 1)) + let tree1 = TreeArithmetic.Node(Multiplication, Leaf 2, Node(Summation, Leaf 1, Leaf 1)) let result = count tree1 @@ -35,8 +35,7 @@ let countTreeTest () = [] let primeNumbersTest () = - - let number13 = primeNumbers |> Seq.item 5 + let first7PrimeNums = [ for i in 0 .. 7 -> Seq.item i (primeNumbers()) ] - number13 |> should equal 13 + first7PrimeNums |> should equal [ 2; 3; 5; 7; 11; 13; 17; 19] \ No newline at end of file diff --git a/hw2/hw2/Program.fs b/hw2/hw2/Program.fs index 8fb8f4d..f94523c 100644 --- a/hw2/hw2/Program.fs +++ b/hw2/hw2/Program.fs @@ -15,20 +15,20 @@ let countEvenNumbersFold list = // бинарное дерево type Tree<'a> = | Tree of 'a * Tree<'a> * Tree<'a> -| Empty +| Tip of 'a // функция, которая применяет функцию к каждому элементу дерева let rec funcToTree tree func = match tree with - | Empty -> Empty + | Tip (value) -> Tip(func value) | Tree (value, left, right) -> Tree(func value, funcToTree left func, funcToTree right func) // все операции type Operation = - | Sum - | Sub - | Multi - | Div + | Summation + | Subtracting + | Multiplication + | Division // арифметическое дерево type TreeArithmetic<'t> = @@ -43,29 +43,24 @@ let rec count tree = let left = count left let right = count right match operation with - | Sum -> left + right - | Sub -> left - right - | Multi -> left * right - | Div when right <> 0 -> left / right - | Div when right = 0 -> raise(ArgumentNullException "На ноль делить нельзя") - | _ -> raise(InvalidOperationException "Такой операции не существует!") + | Summation -> left + right + | Subtracting -> left - right + | Multiplication -> left * right + | Division -> left / right // выводит бесконечную последовательность простых чисел -let primeNumbers = - let rec test element list = +let primeNumbers () = + let rec testOnPrime element list = match list with | [] -> true | h :: tail when element % h = 0 -> false - | h :: tail when element % h <> 0 -> test element tail - | _ -> false + | h :: tail when element % h <> 0 -> testOnPrime element tail let rec sequence element list = seq { - if test element list then + if testOnPrime element list then yield element yield! sequence (element + 1) (element :: list) else yield! sequence (element + 1) list } - sequence 2 [] - -printfn "%d" (Seq.item 5 primeNumbers) \ No newline at end of file + sequence 2 [] \ No newline at end of file From d8cb4ab77cec89c1b01f9e03049f5605e1f132ce Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Thu, 24 Mar 2022 21:24:54 +0300 Subject: [PATCH 4/4] add new test --- hw2/hw2.Test/FunctionsTest.fs | 61 +++++++++++++++++++++++++++++++---- hw2/hw2/Program.fs | 4 +-- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/hw2/hw2.Test/FunctionsTest.fs b/hw2/hw2.Test/FunctionsTest.fs index 9f39d86..0563a39 100644 --- a/hw2/hw2.Test/FunctionsTest.fs +++ b/hw2/hw2.Test/FunctionsTest.fs @@ -6,7 +6,13 @@ open FsUnit open Program [] -let countEvenNumbersCheck () = +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 @@ -16,22 +22,65 @@ let countEvenNumbersCheck () = | _ -> 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 tree1 = Tree.Tree(2, Tree.Tip(4), Tree.Tip(6)) + let tree = Tree.Tree(2, Tree.Tip(4), Tree.Tip(6)) - let newTree = funcToTree tree1 (fun x -> x * 2) + let newTree = funcToTree tree (fun x -> x * 2) newTree |> should equal (Tree(4, Tree.Tip(8), Tree.Tip(12))) [] -let countTreeTest () = - let tree1 = TreeArithmetic.Node(Multiplication, Leaf 2, Node(Summation, Leaf 1, Leaf 1)) +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 4 + 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 () = diff --git a/hw2/hw2/Program.fs b/hw2/hw2/Program.fs index f94523c..d55dcfe 100644 --- a/hw2/hw2/Program.fs +++ b/hw2/hw2/Program.fs @@ -53,8 +53,8 @@ let primeNumbers () = let rec testOnPrime element list = match list with | [] -> true - | h :: tail when element % h = 0 -> false - | h :: tail when element % h <> 0 -> testOnPrime element tail + | h :: tail -> + if element % h = 0 then false else testOnPrime element tail let rec sequence element list = seq { if testOnPrime element list then