From 4538b95ee864bcd2c07de34b67d7ccdef21eb4fb Mon Sep 17 00:00:00 2001 From: bygu4 Date: Thu, 10 Apr 2025 16:55:28 +0300 Subject: [PATCH 1/4] add first two tasks solutions --- Tasks/Test/Test.Tests/BinaryTreeTests.fs | 31 +++++++++++++++++++ .../Test/Test.Tests/InfiniteSequenceTests.fs | 20 ++++++++++++ Tasks/Test/Test.Tests/Program.fs | 4 +++ Tasks/Test/Test.Tests/Test.Tests.fsproj | 30 ++++++++++++++++++ Tasks/Test/Test.sln | 28 +++++++++++++++++ Tasks/Test/Test/BinaryTree.fs | 21 +++++++++++++ Tasks/Test/Test/InfiniteSequence.fs | 14 +++++++++ Tasks/Test/Test/PriorityQueue.fs | 12 +++++++ Tasks/Test/Test/Test.fsproj | 14 +++++++++ 9 files changed, 174 insertions(+) create mode 100644 Tasks/Test/Test.Tests/BinaryTreeTests.fs create mode 100644 Tasks/Test/Test.Tests/InfiniteSequenceTests.fs create mode 100644 Tasks/Test/Test.Tests/Program.fs create mode 100644 Tasks/Test/Test.Tests/Test.Tests.fsproj create mode 100644 Tasks/Test/Test.sln create mode 100644 Tasks/Test/Test/BinaryTree.fs create mode 100644 Tasks/Test/Test/InfiniteSequence.fs create mode 100644 Tasks/Test/Test/PriorityQueue.fs create mode 100644 Tasks/Test/Test/Test.fsproj diff --git a/Tasks/Test/Test.Tests/BinaryTreeTests.fs b/Tasks/Test/Test.Tests/BinaryTreeTests.fs new file mode 100644 index 0000000..b91fe4b --- /dev/null +++ b/Tasks/Test/Test.Tests/BinaryTreeTests.fs @@ -0,0 +1,31 @@ +module BinaryTree.Tests + +open NUnit.Framework +open FsUnit + +[] +let testFilter_EmptyTree_GetEmptyList () = + filter None (fun x -> x > 0) |> _.IsEmpty |> should be True + +[] +let testFilter_SingleElementTree_GetTheTip () = + filter (Tip ("abc", None, None)) (fun s -> s.StartsWith "a") + |> should equal ["abc"] + +[] +let testFilter_NestedTree_GetEvenNumbers () = + let tree = Tip (100, + Tip (0, + Tip (-1, + None, + Tip (15, None, None) + ), + Tip (-124, None, None) + ), + Tip (1000, + Tip (-6, None, None), + None + ) + ) + filter tree (fun x -> x % 2 = 0) + |> should equal [0; -124; 100; -6; 1000] diff --git a/Tasks/Test/Test.Tests/InfiniteSequenceTests.fs b/Tasks/Test/Test.Tests/InfiniteSequenceTests.fs new file mode 100644 index 0000000..2a6c889 --- /dev/null +++ b/Tasks/Test/Test.Tests/InfiniteSequenceTests.fs @@ -0,0 +1,20 @@ +module InfiniteSequence.Tests + +open NUnit.Framework +open FsUnit +open FsCheck +open FsCheck.NUnit + +[] +let anyEvenIndexOfSequenceIsPositive (index: NonNegativeInt) = + match index with + | x when int x % 2 = 0 -> Seq.item (int x) signAlternatingSequence > 0 + | x -> Seq.item (int x) signAlternatingSequence < 0 + +[] +let testSequence_GetFirstNItems () = + signAlternatingSequence + |> Seq.take 12 + |> should equal (seq [1; -2; 3; -4; 5; -6; 7; -8; 9; -10; 11; -12]) + +Check.QuickThrowOnFailure anyEvenIndexOfSequenceIsPositive diff --git a/Tasks/Test/Test.Tests/Program.fs b/Tasks/Test/Test.Tests/Program.fs new file mode 100644 index 0000000..96b133f --- /dev/null +++ b/Tasks/Test/Test.Tests/Program.fs @@ -0,0 +1,4 @@ +module Program + +[] +let main _ = 0 diff --git a/Tasks/Test/Test.Tests/Test.Tests.fsproj b/Tasks/Test/Test.Tests/Test.Tests.fsproj new file mode 100644 index 0000000..eb4dcae --- /dev/null +++ b/Tasks/Test/Test.Tests/Test.Tests.fsproj @@ -0,0 +1,30 @@ + + + + net9.0 + latest + false + false + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tasks/Test/Test.sln b/Tasks/Test/Test.sln new file mode 100644 index 0000000..9edec8e --- /dev/null +++ b/Tasks/Test/Test.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test\Test.fsproj", "{A1E410C5-287A-40C7-833F-34021FFB093A}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test.Tests", "Test.Tests\Test.Tests.fsproj", "{0BA19CC2-24D0-45D5-A373-30698C147C27}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A1E410C5-287A-40C7-833F-34021FFB093A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1E410C5-287A-40C7-833F-34021FFB093A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1E410C5-287A-40C7-833F-34021FFB093A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1E410C5-287A-40C7-833F-34021FFB093A}.Release|Any CPU.Build.0 = Release|Any CPU + {0BA19CC2-24D0-45D5-A373-30698C147C27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BA19CC2-24D0-45D5-A373-30698C147C27}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BA19CC2-24D0-45D5-A373-30698C147C27}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BA19CC2-24D0-45D5-A373-30698C147C27}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Tasks/Test/Test/BinaryTree.fs b/Tasks/Test/Test/BinaryTree.fs new file mode 100644 index 0000000..ffbdfd9 --- /dev/null +++ b/Tasks/Test/Test/BinaryTree.fs @@ -0,0 +1,21 @@ +module BinaryTree + +type Tree<'a> = + | None + | Tip of 'a * Tree<'a> * Tree<'a> + +let filter (tree: Tree<'a>) (condition: 'a -> bool) = + let rec filterInternal (tree: Tree<'a>) (condition: 'a -> bool) (cont: unit -> 'a list) = + match tree with + | None -> cont () + | Tip (x, left, right) -> + if condition x then + filterInternal left condition (fun () -> + x :: filterInternal right condition cont + ) + else + filterInternal left condition (fun () -> + filterInternal right condition cont + ) + + filterInternal tree condition (fun () -> []) diff --git a/Tasks/Test/Test/InfiniteSequence.fs b/Tasks/Test/Test/InfiniteSequence.fs new file mode 100644 index 0000000..fc6ae3f --- /dev/null +++ b/Tasks/Test/Test/InfiniteSequence.fs @@ -0,0 +1,14 @@ +module InfiniteSequence + +let signAlternatingOnes = + Seq.initInfinite (( + ) 1) + |> Seq.map (fun x -> + match x with + | x when x % 2 = 0 -> -1 + | _ -> 1 + ) + +let signAlternatingSequence = + Seq.initInfinite (( + ) 1) + |> Seq.zip signAlternatingOnes + |> Seq.map (fun (x, y) -> x * y) diff --git a/Tasks/Test/Test/PriorityQueue.fs b/Tasks/Test/Test/PriorityQueue.fs new file mode 100644 index 0000000..250a797 --- /dev/null +++ b/Tasks/Test/Test/PriorityQueue.fs @@ -0,0 +1,12 @@ +module PriorityQueue + +open System + +type Queue<'a>() = + let mutable elements: ('a * int) list = [] + + member _.Enqueue (element: 'a, priority: int) = + elements <- (element, priority) :: elements + + member _.Dequeue () = + if elements.IsEmpty then raise (new InvalidOperationException "queue was empty") diff --git a/Tasks/Test/Test/Test.fsproj b/Tasks/Test/Test/Test.fsproj new file mode 100644 index 0000000..43d0645 --- /dev/null +++ b/Tasks/Test/Test/Test.fsproj @@ -0,0 +1,14 @@ + + + + net9.0 + true + + + + + + + + + From 4335cc47b19fda69eab05e32b268f8ba19baf61b Mon Sep 17 00:00:00 2001 From: bygu4 Date: Thu, 10 Apr 2025 17:00:29 +0300 Subject: [PATCH 2/4] add comments --- Tasks/Test/Test/BinaryTree.fs | 4 ++++ Tasks/Test/Test/InfiniteSequence.fs | 4 ++++ Tasks/Test/Test/PriorityQueue.fs | 12 ------------ Tasks/Test/Test/Test.fsproj | 1 - 4 files changed, 8 insertions(+), 13 deletions(-) delete mode 100644 Tasks/Test/Test/PriorityQueue.fs diff --git a/Tasks/Test/Test/BinaryTree.fs b/Tasks/Test/Test/BinaryTree.fs index ffbdfd9..f110914 100644 --- a/Tasks/Test/Test/BinaryTree.fs +++ b/Tasks/Test/Test/BinaryTree.fs @@ -1,10 +1,14 @@ module BinaryTree +/// Definition of a binary tree. type Tree<'a> = | None | Tip of 'a * Tree<'a> * Tree<'a> +/// Get of the elements of the given `tree` for which the `condition` is true. let filter (tree: Tree<'a>) (condition: 'a -> bool) = + + /// Traverse the `tree` in CPS style. let rec filterInternal (tree: Tree<'a>) (condition: 'a -> bool) (cont: unit -> 'a list) = match tree with | None -> cont () diff --git a/Tasks/Test/Test/InfiniteSequence.fs b/Tasks/Test/Test/InfiniteSequence.fs index fc6ae3f..058bb36 100644 --- a/Tasks/Test/Test/InfiniteSequence.fs +++ b/Tasks/Test/Test/InfiniteSequence.fs @@ -1,5 +1,7 @@ module InfiniteSequence +/// A sign alternating sequence of ones. +/// (1, -1, 1, -1, ...) let signAlternatingOnes = Seq.initInfinite (( + ) 1) |> Seq.map (fun x -> @@ -8,6 +10,8 @@ let signAlternatingOnes = | _ -> 1 ) +/// A sign alternating sequence with ascending module. +/// (1, -2, 3, -4, ...) let signAlternatingSequence = Seq.initInfinite (( + ) 1) |> Seq.zip signAlternatingOnes diff --git a/Tasks/Test/Test/PriorityQueue.fs b/Tasks/Test/Test/PriorityQueue.fs deleted file mode 100644 index 250a797..0000000 --- a/Tasks/Test/Test/PriorityQueue.fs +++ /dev/null @@ -1,12 +0,0 @@ -module PriorityQueue - -open System - -type Queue<'a>() = - let mutable elements: ('a * int) list = [] - - member _.Enqueue (element: 'a, priority: int) = - elements <- (element, priority) :: elements - - member _.Dequeue () = - if elements.IsEmpty then raise (new InvalidOperationException "queue was empty") diff --git a/Tasks/Test/Test/Test.fsproj b/Tasks/Test/Test/Test.fsproj index 43d0645..f47966e 100644 --- a/Tasks/Test/Test/Test.fsproj +++ b/Tasks/Test/Test/Test.fsproj @@ -8,7 +8,6 @@ - From 891931ae8b6ccae182421e785c38a65041337db2 Mon Sep 17 00:00:00 2001 From: bygu4 Date: Thu, 10 Apr 2025 17:00:59 +0300 Subject: [PATCH 3/4] correct comment --- Tasks/Test/Test/BinaryTree.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tasks/Test/Test/BinaryTree.fs b/Tasks/Test/Test/BinaryTree.fs index f110914..98b65a4 100644 --- a/Tasks/Test/Test/BinaryTree.fs +++ b/Tasks/Test/Test/BinaryTree.fs @@ -8,7 +8,7 @@ type Tree<'a> = /// Get of the elements of the given `tree` for which the `condition` is true. let filter (tree: Tree<'a>) (condition: 'a -> bool) = - /// Traverse the `tree` in CPS style. + /// Traverse the `tree` in CPS. let rec filterInternal (tree: Tree<'a>) (condition: 'a -> bool) (cont: unit -> 'a list) = match tree with | None -> cont () From 370bdc0862da53ee57b66454ad0f4912f8d561e6 Mon Sep 17 00:00:00 2001 From: bygu4 Date: Thu, 10 Apr 2025 17:23:26 +0300 Subject: [PATCH 4/4] correct typo --- Tasks/Test/Test/BinaryTree.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tasks/Test/Test/BinaryTree.fs b/Tasks/Test/Test/BinaryTree.fs index 98b65a4..b662979 100644 --- a/Tasks/Test/Test/BinaryTree.fs +++ b/Tasks/Test/Test/BinaryTree.fs @@ -5,7 +5,7 @@ type Tree<'a> = | None | Tip of 'a * Tree<'a> * Tree<'a> -/// Get of the elements of the given `tree` for which the `condition` is true. +/// Get all of the elements of the given `tree` for which the `condition` is true. let filter (tree: Tree<'a>) (condition: 'a -> bool) = /// Traverse the `tree` in CPS.