Skip to content

Commit 0a2c904

Browse files
committed
[draft] Add MergeSort exercise and docs
1 parent 8a07f46 commit 0a2c904

7 files changed

Lines changed: 69 additions & 14 deletions

File tree

C-Sharp-Data-Structures-and-Algorithms.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
4444
.gitignore = .gitignore
4545
EndProjectSection
4646
EndProject
47+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Arrays", "Arrays", "{517E461B-F61E-4598-A4D9-D1ABAE070CE5}"
48+
EndProject
49+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sort", "Sort", "{3D3FEA4A-376A-45C5-BED1-517954BA5782}"
50+
ProjectSection(SolutionItems) = preProject
51+
Docs\Exercises\Arrays\Sort\MergeSort.md = Docs\Exercises\Arrays\Sort\MergeSort.md
52+
EndProjectSection
53+
EndProject
4754
Global
4855
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4956
Debug|Any CPU = Debug|Any CPU
@@ -96,5 +103,7 @@ Global
96103
{DF8A6048-EC35-4A12-9E18-DFE8AAEE1315} = {A66DB0A9-94B4-4591-84D7-1B4C64344EFD}
97104
{52019907-5924-4F1A-86D7-1B0C1FC9CDD7} = {8F7EDDF0-E9FC-4D7D-8DBE-3C898C8798B9}
98105
{D523E1B0-B30E-44C9-936C-D45A6F34191E} = {8F7EDDF0-E9FC-4D7D-8DBE-3C898C8798B9}
106+
{517E461B-F61E-4598-A4D9-D1ABAE070CE5} = {D523E1B0-B30E-44C9-936C-D45A6F34191E}
107+
{3D3FEA4A-376A-45C5-BED1-517954BA5782} = {517E461B-F61E-4598-A4D9-D1ABAE070CE5}
99108
EndGlobalSection
100109
EndGlobal

Docs/Exercises/Arrays/Sort/MergeSort.md

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Exercises.Arrays;
2+
3+
public static partial class Sort
4+
{
5+
public static int[][] MergeSort(int[][] intervals)
6+
{
7+
intervals = intervals.OrderBy(a => a[0]).ToArray();
8+
int st = intervals[0][0], ed = intervals[0][1];
9+
var ans = new List<int[]>();
10+
for (int i = 1; i < intervals.Length; ++i) {
11+
if (ed < intervals[i][0]) {
12+
ans.Add(new int[] { st, ed });
13+
st = intervals[i][0];
14+
ed = intervals[i][1];
15+
} else {
16+
ed = Math.Max(ed, intervals[i][1]);
17+
}
18+
}
19+
ans.Add(new int[] { st, ed });
20+
return ans.ToArray();
21+
}
22+
}

Exercises/Exercises/Class1.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using FluentAssertions;
2+
3+
namespace ExercisesTests.Arrays.Sort;
4+
5+
public class MergeSortTests
6+
{
7+
public static IEnumerable<object[]> TestData()
8+
{
9+
yield return
10+
[
11+
new int[][] {[1, 3], [2, 6], [8, 10], [15,18]},
12+
new int[][] {[1,6],[8,10],[15,18]},
13+
];
14+
15+
yield return
16+
[
17+
new int[][] {[1,4],[4,5]},
18+
new int[][] {[1,5]},
19+
];
20+
}
21+
22+
[Theory]
23+
[MemberData(nameof(TestData))]
24+
public void Test1(int[][] intervals, int[][] expected)
25+
{
26+
// Arrange
27+
// Act
28+
var result = Exercises.Arrays.Sort.MergeSort(intervals);
29+
30+
// Assert
31+
result.Should().BeEquivalentTo(expected);
32+
}
33+
}

Exercises/ExercisesTests/ExercisesTests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
1415
<PackageReference Include="xunit" Version="2.4.2"/>
1516
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
@@ -21,5 +22,9 @@
2122
<PrivateAssets>all</PrivateAssets>
2223
</PackageReference>
2324
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\Exercises\Exercises.csproj"/>
28+
</ItemGroup>
2429

2530
</Project>

Exercises/ExercisesTests/UnitTest1.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)