-
Notifications
You must be signed in to change notification settings - Fork 0
Hw3 #12
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
Merged
Merged
Hw3 #12
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85f08c5
Create Hw3
GrigorySmirnovSpb 943086e
Upd code
GrigorySmirnovSpb 7b94bee
Fix Test.fsproj
GrigorySmirnovSpb f7e56b9
Fix code
GrigorySmirnovSpb 202018a
Fix code
GrigorySmirnovSpb d1e0af4
Merge branch 'main' into HW3
GrigorySmirnovSpb 373391b
Fix conflicts
GrigorySmirnovSpb 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
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
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,113 @@ | ||
namespace tests | ||
|
||
open Xunit | ||
open FsCheck | ||
open FsCheck.Xunit | ||
open ListLib | ||
|
||
module UnitTests = | ||
|
||
[<Fact>] | ||
let MergeTest () = | ||
let expected = List.sort [] | ||
let lst = MyList.fromList [] | ||
let lst2 = MyList.mergeSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
[<Fact>] | ||
let BubbleTest () = | ||
let expected = List.sort [] | ||
let lst = MyList.fromList [] | ||
let lst2 = MyList.bubbleSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
[<Fact>] | ||
let QuickTest () = | ||
let expected = List.sort [] | ||
let lst = MyList.fromList [] | ||
let lst2 = MyList.quickSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
module PropertyTests = | ||
|
||
[<Properties(MaxTest = 100)>] | ||
type MergeTest() = | ||
|
||
[<Property>] | ||
member _.intTest(testList: int list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.mergeSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
[<Property>] | ||
member _.charTest(testList: char list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.mergeSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<char>(expected, actual) | ||
|
||
[<Property>] | ||
member _.floatTest(testList: float list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.mergeSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<float>(expected, actual) | ||
|
||
type QuickTest() = | ||
|
||
[<Property>] | ||
member _.``intTest``(testList: int list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.quickSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
[<Property>] | ||
member _.charTest(testList: char list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.quickSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<char>(expected, actual) | ||
|
||
[<Property>] | ||
member _.floatTest(testList: float list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.quickSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<float>(expected, actual) | ||
|
||
type BubbleTest() = | ||
|
||
[<Property>] | ||
member _.intTest(testList: int list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.bubbleSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<int>(expected, actual) | ||
|
||
[<Property>] | ||
member _.charTest(testList: char list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.bubbleSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<char>(expected, actual) | ||
|
||
[<Property>] | ||
member _.floatTest(testList: float list) = | ||
let expected = List.sort testList | ||
let lst = MyList.fromList testList | ||
let lst2 = MyList.bubbleSort lst compare | ||
let actual = MyList.toList lst2 | ||
Assert.Equal<float>(expected, actual) |
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Library.fs" /> | ||
</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,83 @@ | ||
namespace ListLib | ||
|
||
module MyList = | ||
|
||
type MyList<'elem> = | ||
| Empty | ||
| Cons of 'elem * MyList<'elem> | ||
|
||
let rec toList myList = | ||
match myList with | ||
| Empty -> [] | ||
| Cons (head, tail) -> head :: toList tail | ||
|
||
let rec fromList lst = | ||
match lst with | ||
| [] -> Empty | ||
| hd :: tl -> Cons(hd, fromList tl) | ||
|
||
// Сортировка слиянием | ||
let rec merge left right compare = | ||
match left, right with | ||
| Empty, _ -> right | ||
| _, Empty -> left | ||
| Cons (x1, tail1), Cons (x2, tail2) -> | ||
if compare x1 x2 <= 0 then | ||
Cons (x1, merge tail1 right compare) | ||
else | ||
Cons (x2, merge left tail2 compare) | ||
|
||
let rec split list = | ||
match list with | ||
| Empty -> (Empty, Empty) | ||
| Cons (x, Empty) -> (Cons (x, Empty), Empty) | ||
| Cons (x1, Cons (x2, tail)) -> | ||
let left, right = split tail | ||
(Cons (x1, left), Cons (x2, right)) | ||
|
||
let rec mergeSort list compare = | ||
match list with | ||
| Empty -> Empty | ||
| Cons (x, Empty) -> Cons (x, Empty) | ||
| _ -> | ||
let left, right = split list | ||
merge (mergeSort left compare) (mergeSort right compare) compare | ||
|
||
// Сортировка пузырьком | ||
let rec bubbleSort list compare = | ||
let rec swap list notSorted = | ||
match list with | ||
| Empty -> (Empty, notSorted) | ||
| Cons (x, Empty) -> (Cons (x, Empty), notSorted) | ||
| Cons (x1, Cons (x2, tail)) -> | ||
if compare x1 x2 > 0 then | ||
let newTail, _ = swap (Cons (x1, tail)) true | ||
(Cons (x2, newTail), true) | ||
else | ||
let newTail, wasChanged = swap (Cons (x2, tail)) notSorted | ||
(Cons (x1, newTail), wasChanged) | ||
|
||
let sortedList, wasChanged = swap list false | ||
if wasChanged then bubbleSort sortedList compare else sortedList | ||
|
||
// QuickSort | ||
let rec append list1 list2 = | ||
match list1 with | ||
| Empty -> list2 | ||
| Cons (head, tail) -> Cons (head, append tail list2) | ||
|
||
let rec filter func list = | ||
match list with | ||
| Empty -> Empty | ||
| Cons (elem, tl) when func elem -> Cons (elem, filter func tl) | ||
| Cons (elem, Empty) when not (func elem) -> Empty | ||
| Cons (elem1, Cons(elem2, tl)) when not (func elem1) -> filter func (Cons(elem2, tl)) | ||
| _ -> Empty | ||
|
||
let rec quickSort list compare = | ||
match list with | ||
| Empty -> Empty | ||
| Cons (pivot, tail) -> | ||
let less = filter (fun x -> compare x pivot < 0) tail | ||
let greater = filter (fun x -> compare x pivot >= 0) tail | ||
append (quickSort less compare) (Cons(pivot, quickSort greater compare)) |
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.
Uh oh!
There was an error while loading. Please reload this page.