Skip to content

Commit d4a209d

Browse files
committed
fixed bug in exercise 1.3 quicksort
1 parent b423f69 commit d4a209d

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

Examples/Examples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<packagereference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></packagereference>
3333
<packagereference Include="NUnit" Version="3.6.1"></packagereference>
34-
<packagereference Include="NUnit3TestAdapter" Version="3.8.0-alpha1"></packagereference>
34+
<packagereference Include="NUnit3TestAdapter" Version="3.8.0"></packagereference>
3535
</ItemGroup>
3636

3737
<ItemGroup>

Exercises/Chapter01/Solutions.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using NUnit.Framework;
5+
using LaYumba.Functional;
6+
using static LaYumba.Functional.F;
47

58
namespace Exercises.Chapter1
69
{
@@ -14,22 +17,59 @@ static Func<T, bool> Negate<T>(this Func<T, bool> pred)
1417
static List<int> QuickSort(this List<int> list)
1518
{
1619
if (list.Count == 0) return new List<int>();
20+
1721
var pivot = list[0];
18-
var small = from item in list where item <= pivot select item;
19-
var large = from item in list where pivot < item select item;
22+
var rest = list.Skip(1);
23+
24+
var small = from item in rest where item <= pivot select item;
25+
var large = from item in rest where pivot < item select item;
26+
2027
return small.ToList().QuickSort()
28+
.Append(pivot)
2129
.Concat(large.ToList().QuickSort())
2230
.ToList();
2331
}
2432

33+
// a more terse solution, using helper methods that will be discussed later in the book
34+
static List<int> QSort(this List<int> list)
35+
=> list.Match(
36+
() => List<int>(),
37+
(pivot, rest) => rest.Where(i => i <= pivot).ToList().QSort()
38+
.Append(pivot)
39+
.Concat(rest.Where(i => pivot < i).ToList().QSort())
40+
).ToList();
41+
42+
[Test]
43+
public static void TestQuickSort()
44+
{
45+
var list = new List<int> {-100, 63, 30, 45, 1, 1000, -23, -67, 1, 2, 56, 75, 975, 432, -600, 193, 85, 12};
46+
var expected = new List<int> {-600, -100, -67, -23, 1, 1, 2, 12, 30, 45, 56, 63, 75, 85, 193, 432, 975, 1000};
47+
var actual = list.QuickSort();
48+
Assert.AreEqual(expected, actual);
49+
}
50+
51+
[Test]
52+
public static void TestQSort()
53+
{
54+
var list = new List<int> {-100, 63, 30, 45, 1, 1000, -23, -67, 1, 2, 56, 75, 975, 432, -600, 193, 85, 12};
55+
var expected = new List<int> {-600, -100, -67, -23, 1, 1, 2, 12, 30, 45, 56, 63, 75, 85, 193, 432, 975, 1000};
56+
var actual = list.QSort();
57+
Assert.AreEqual(expected, actual);
58+
}
59+
2560
// 3.
2661
static List<T> QuickSort<T>(this List<T> list, Comparison<T> compare)
2762
{
2863
if (list.Count == 0) return new List<T>();
64+
2965
var pivot = list[0];
30-
var small = from item in list where compare(item, pivot) <= 0 select item;
31-
var large = from item in list where 0 < compare(item, pivot) select item;
66+
var rest = list.Skip(1);
67+
68+
var small = from item in rest where compare(item, pivot) <= 0 select item;
69+
var large = from item in rest where 0 < compare(item, pivot) select item;
70+
3271
return small.ToList().QuickSort(compare)
72+
.Concat(new List<T> { pivot })
3373
.Concat(large.ToList().QuickSort(compare))
3474
.ToList();
3575
}

Exercises/Chapter05/Exercises.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using NUnit.Framework;
7+
using Examples.Chapter3;
78

89
namespace Exercises.Chapter5
910
{
1011
public static class Exercises
1112
{
1213
// 1. Without looking at any code or documentation (or intllisense), write the function signatures of
1314
// `OrderByDescending`, `Take` and `Average`, which we used to implement `AverageEarningsOfRichestQuartile`:
14-
decimal AverageEarningsOfRichestQuartile(List<Person> population)
15+
static decimal AverageEarningsOfRichestQuartile(List<Person> population)
1516
=> population
1617
.OrderByDescending(p => p.Earnings)
1718
.Take(population.Count/4)

Exercises/Chapter05/Solutions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using NUnit.Framework;
7+
using Examples.Chapter3;
78

89
namespace Exercises.Chapter5.Solutions
910
{
1011
static class Exercises
1112
{
1213
// 1. Without looking at any code or documentation (or intllisense), write the function signatures of
1314
// `OrderByDescending`, `Take` and `Average`, which we used to implement `AverageEarningsOfRichestQuartile`:
14-
decimal AverageEarningsOfRichestQuartile(List<Person> population)
15+
static decimal AverageEarningsOfRichestQuartile(List<Person> population)
1516
=> population
1617
.OrderByDescending(p => p.Earnings)
1718
.Take(population.Count/4)

Exercises/Exercises.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<packagereference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></packagereference>
1313
<packagereference Include="NUnit" Version="3.6.1"></packagereference>
14-
<packagereference Include="NUnit3TestAdapter" Version="3.8.0-alpha1"></packagereference>
14+
<packagereference Include="NUnit3TestAdapter" Version="3.8.0"></packagereference>
1515

1616
<ProjectReference Include="..\Examples\Examples.csproj" />
1717
<ProjectReference Include="..\LaYumba.Functional.Data\LaYumba.Functional.Data.csproj" />

0 commit comments

Comments
 (0)