Skip to content
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

Hw3 #12

Merged
merged 7 commits into from
Mar 3, 2025
Merged

Hw3 #12

Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions FsharpSln.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LibHw2", "src\LibHw2\LibHw2
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test\Test.fsproj", "{363D4F3B-6E01-4E01-9616-7C95DFB0F5B6}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LibHw3", "src\LibHw3\LibHw3.fsproj", "{E63C1F63-167B-47D5-A94C-6C00CDAC9104}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,8 +28,13 @@ Global
{363D4F3B-6E01-4E01-9616-7C95DFB0F5B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{363D4F3B-6E01-4E01-9616-7C95DFB0F5B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{363D4F3B-6E01-4E01-9616-7C95DFB0F5B6}.Release|Any CPU.Build.0 = Release|Any CPU
{E63C1F63-167B-47D5-A94C-6C00CDAC9104}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E63C1F63-167B-47D5-A94C-6C00CDAC9104}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E63C1F63-167B-47D5-A94C-6C00CDAC9104}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E63C1F63-167B-47D5-A94C-6C00CDAC9104}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{07D1365E-068B-4B9D-999D-4F9C3B544697} = {C3F2792A-709B-4169-ADE4-3D55DC717452}
{E63C1F63-167B-47D5-A94C-6C00CDAC9104} = {C3F2792A-709B-4169-ADE4-3D55DC717452}
EndGlobalSection
EndGlobal
13 changes: 7 additions & 6 deletions Test/Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Tests.fs" />
<Compile Include="TestsHw3.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand All @@ -21,14 +21,15 @@
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\LibHw2\LibHw2.fsproj" />
<ItemGroup>
<ProjectReference Include="..\src\LibHw2\LibHw2.fsproj" />
<ProjectReference Include="..\src\LibHw3\LibHw3.fsproj" />
</ItemGroup>

</Project>
113 changes: 113 additions & 0 deletions Test/TestsHw3.fs
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)
12 changes: 12 additions & 0 deletions src/LibHw3/LibHw3.fsproj
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>
83 changes: 83 additions & 0 deletions src/LibHw3/Library.fs
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 func elem = false -> Empty
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Реально надо писать прям = false? Просто логически выражением не обойтись?

| Cons (elem1, Cons(elem2, tl)) when func elem1 = false -> 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))
Loading