Skip to content

Commit 0615abd

Browse files
authored
Update to net9.0, bump packages, tidy up (#135)
1 parent bb7beb2 commit 0615abd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+295
-316
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ indent_size = 4
1313
# CA1822: Mark members as static
1414
dotnet_diagnostic.ca1822.severity = silent
1515

16+
csharp_style_expression_bodied_methods = true:suggestion
1617
csharp_style_namespace_declarations = file_scoped
1718

1819
# Microsoft .NET properties

.github/workflows/workflow.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
DOTNET_NOLOGO: true
1111
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
1212
DOTNET_CLI_TELEMETRY_OPTOUT: true
13-
DOTNET_VERSION: 8.0.x
13+
DOTNET_VERSION: 9.0.x
1414

1515
jobs:
1616
build:
@@ -38,6 +38,7 @@ jobs:
3838

3939
benchmark:
4040
needs: [ build ]
41+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
4142
runs-on: ubuntu-latest
4243
steps:
4344
- name: Checkout

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bin
33
obj
44
*.DotSettings.user
55
BenchmarkDotNet.Artifacts
6+
/.idea/.idea.LeetCode/.idea/material_theme_project_new.xml

Directory.Build.props

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<Project>
2-
32
<Import Project="Directory.Build.user.props" Condition="Exists('Directory.Build.user.props')"/>
43

54
<PropertyGroup>
6-
<TargetFramework>net8.0</TargetFramework>
75
<Authors>Simon McKenna</Authors>
8-
<Nullable>enable</Nullable>
9-
<ImplicitUsings>enable</ImplicitUsings>
6+
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
107
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<LangVersion>latest</LangVersion>
10+
<Nullable>enable</Nullable>
11+
<TargetFramework>net9.0</TargetFramework>
1112
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1213
</PropertyGroup>
1314

15+
<ItemGroup>
16+
<PackageReference Include="Nerdbank.GitVersioning" />
17+
</ItemGroup>
1418
</Project>

Directory.Packages.props

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88
<PrivateAssets>all</PrivateAssets>
99
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1010
</PackageVersion>
11-
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
12-
<PackageVersion Include="FsUnit.xUnit" Version="6.0.0" />
13-
<PackageVersion Include="FSharp.Core" Version="8.0.400" />
11+
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
12+
<PackageVersion Include="FsUnit.xUnit" Version="6.0.1" />
13+
<PackageVersion Include="FSharp.Core" Version="8.0.100" />
1414
<PackageVersion Include="lolcat" Version="1.1.64" />
1515
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
16+
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.143">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageVersion>
1620
<PackageVersion Include="Spectre.Console" Version="0.49.1" />
17-
<PackageVersion Include="Spectre.Console.Analyzer" Version="1.0.0" />
21+
<PackageVersion Include="Spectre.Console.Analyzer" Version="1.0.0">
22+
<PrivateAssets>all</PrivateAssets>
23+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
24+
</PackageVersion>
1825
<PackageVersion Include="Spectre.Console.Cli" Version="0.49.1" />
19-
<PackageVersion Include="xunit" Version="2.9.0" />
26+
<PackageVersion Include="xunit" Version="2.9.2" />
2027
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2">
2128
<PrivateAssets>all</PrivateAssets>
2229
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

LeetCode.CSharp/Benchmark.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
[Config(typeof(BenchmarkConfig))]
44
public abstract class Benchmark
55
{
6-
protected char[][] CharArrayMulti { get; set; } = { new[] { char.MinValue } };
6+
protected char[][] CharArrayMulti { get; set; } = [[char.MinValue]];
77

88
protected int Int1 { get; set; }
99
protected int Int2 { get; set; }
1010

11-
protected int[] IntArray1 { get; set; } = Array.Empty<int>();
12-
protected int[] IntArray2 { get; set; } = Array.Empty<int>();
13-
protected int[][] IntArrayMulti1 { get; set; } = Array.Empty<int[]>();
14-
protected int?[] IntArrayNullable { get; set; } = Array.Empty<int?>();
11+
protected int[] IntArray1 { get; set; } = [];
12+
protected int[] IntArray2 { get; set; } = [];
13+
protected int[][] IntArrayMulti1 { get; set; } = [];
14+
protected int?[] IntArrayNullable { get; set; } = [];
1515

16-
protected List<Interval> Intervals1 { get; } = new();
16+
protected List<Interval> Intervals1 { get; } = [];
1717

1818
protected ListNode ListNode1 { get; set; } = new();
1919
protected ListNode ListNode2 { get; set; } = new();
2020

2121
protected string String1 { get; set; } = string.Empty;
2222
protected string String2 { get; set; } = string.Empty;
23-
protected string[] StringArray1 { get; set; } = Array.Empty<string>();
23+
protected string[] StringArray1 { get; set; } = [];
2424

2525
protected TreeNode TreeNode1 { get; set; } = new();
2626
protected TreeNode TreeNode2 { get; set; } = new();

LeetCode.CSharp/Benchmarks/AddTwoNumbersBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public void AddTwoNumbersSetup()
1919
[GlobalCleanup(Target = nameof(AddTwoNumbers))]
2020
public void AddTwoNumbersCleanup()
2121
{
22-
IntArray1 = Array.Empty<int>();
23-
IntArray2 = Array.Empty<int>();
22+
IntArray1 = [];
23+
IntArray2 = [];
2424
ListNode1 = new ListNode(IntArray1);
2525
ListNode2 = new ListNode(IntArray2);
2626
}

LeetCode.CSharp/Benchmarks/BinarySearchBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public class BinarySearchBenchmark : Benchmark
99
public int BinarySearch() => Problem.BinarySearch(IntArray1, 1_000);
1010

1111
[GlobalCleanup(Target = nameof(BinarySearch))]
12-
public void BinarySearchCleanup() => IntArray1 = Array.Empty<int>();
12+
public void BinarySearchCleanup() => IntArray1 = [];
1313
}

LeetCode.CSharp/Problems/AddTwoNumbers.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,24 @@ public static ListNode AddTwoNumbers(ListNode l1, ListNode l2)
4646
return result.next!;
4747
}
4848

49-
[Fact]
50-
public void AddTwoNumbersTest()
49+
[Theory]
50+
[InlineData(new[] { 2, 4, 3 }, new[] { 5, 6, 4 }, new[] { 7, 0, 8 })]
51+
[InlineData(new int[] { }, new int[] { }, new int[] { })]
52+
[InlineData(
53+
new[] { 9, 9, 9, 9, 9, 9, 9 },
54+
new[] { 9, 9, 9, 9 },
55+
new[] { 8, 9, 9, 9, 0, 0, 0, 1 })]
56+
public void AddTwoNumbersTest(int[] list1, int[] list2, int[] expectedList)
5157
{
52-
var ex11 = new ListNode(new[] { 2, 4, 3 });
53-
var ex12 = new ListNode(new[] { 5, 6, 4 });
54-
var ex1Expected = new ListNode(new[] { 7, 0, 8 });
55-
var ex21 = new ListNode();
56-
var ex22 = new ListNode();
57-
var ex2Expected = new ListNode();
58-
var ex31 = new ListNode(new[] { 9, 9, 9, 9, 9, 9, 9 });
59-
var ex32 = new ListNode(new[] { 9, 9, 9, 9 });
60-
var ex3Expected = new ListNode(new[] { 8, 9, 9, 9, 0, 0, 0, 1 });
58+
var listNode1 = new ListNode(list1);
59+
var listNode2 = new ListNode(list2);
60+
var expectedNode = new ListNode(expectedList);
61+
var result = AddTwoNumbers(listNode1, listNode2);
62+
result.Should().NotBeNull();
63+
AssertEqual(result, expectedNode);
6164

62-
var ex1Result = AddTwoNumbers(ex11, ex12);
63-
var ex2Result = AddTwoNumbers(ex21, ex22);
64-
var ex3Result = AddTwoNumbers(ex31, ex32);
65+
return;
6566

66-
ex1Result.Should().NotBeNull();
67-
ex2Result.Should().NotBeNull();
68-
ex3Result.Should().NotBeNull();
6967
static void AssertEqual(ListNode? l1, ListNode? l2)
7068
{
7169
while (true)
@@ -84,8 +82,5 @@ static void AssertEqual(ListNode? l1, ListNode? l2)
8482
l2 = l2.next;
8583
}
8684
}
87-
AssertEqual(ex1Result, ex1Expected);
88-
AssertEqual(ex2Result, ex2Expected);
89-
AssertEqual(ex3Result, ex3Expected);
9085
}
9186
}

LeetCode.CSharp/Problems/BinarySearch.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ public static int BinarySearch(int[] nums, int target)
3535
return -1;
3636
}
3737

38-
[Fact]
39-
public void BinarySearchTest()
40-
{
41-
var nums = new[] { -1, 0, 3, 5, 9, 12 };
42-
43-
BinarySearch(nums, 9).Should().Be(4);
44-
BinarySearch(nums, 2).Should().Be(-1);
45-
BinarySearch(Array.Empty<int>(), 0).Should().Be(-1);
46-
}
38+
[Theory]
39+
[InlineData(new[] { -1, 0, 3, 5, 9, 12 }, 9, 4)]
40+
[InlineData(new[] { -1, 0, 3, 5, 9, 12 }, 2, -1)]
41+
[InlineData(new int[] { }, 0, -1)]
42+
public void BinarySearchTest(int[] nums, int target, int expected) =>
43+
BinarySearch(nums, target).Should().Be(expected);
4744
}

LeetCode.CSharp/Problems/ClimbStairs.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public static int ClimbStairs(int n)
2424
return oneStep;
2525
}
2626

27-
[Fact]
28-
public void ClimbStairsTest()
29-
{
30-
ClimbStairs(2).Should().Be(2);
31-
ClimbStairs(3).Should().Be(3);
32-
}
27+
[Theory]
28+
[InlineData(2, 2)]
29+
[InlineData(3, 3)]
30+
public void ClimbStairsTest(int n, int expected) => ClimbStairs(n).Should().Be(expected);
3331
}

LeetCode.CSharp/Problems/ContainsDuplicate.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,13 @@ public static bool ContainsDuplicate(int[] nums)
1111
{
1212
var set = new HashSet<int>();
1313

14-
foreach (var num in nums)
15-
{
16-
if (set.Contains(num))
17-
{
18-
return true;
19-
}
20-
21-
set.Add(num);
22-
}
23-
24-
return false;
14+
return nums.Any(num => !set.Add(num));
2515
}
2616

27-
[Fact]
28-
public void ContainsDuplicateTest()
29-
{
30-
var ex1 = new[] { 1, 2, 3, 1 };
31-
var ex2 = new[] { 1, 2, 3, 4 };
32-
var ex3 = new[] { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 };
33-
34-
ContainsDuplicate(ex1).Should().BeTrue();
35-
ContainsDuplicate(ex2).Should().BeFalse();
36-
ContainsDuplicate(ex3).Should().BeTrue();
37-
}
17+
[Theory]
18+
[InlineData(new[] { 1, 2, 3, 1 }, true)]
19+
[InlineData(new[] { 1, 2, 3, 4 }, false)]
20+
[InlineData(new[] { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 }, true)]
21+
public void ContainsDuplicateTest(int[] nums, bool expected) =>
22+
ContainsDuplicate(nums).Should().Be(expected);
3823
}

LeetCode.CSharp/Problems/CountBits.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ public static int[] CountBits(int n)
2626
return result;
2727
}
2828

29-
[Fact]
30-
public void CountBitsTest()
31-
{
32-
const int ex1 = 2;
33-
const int ex2 = 5;
34-
35-
CountBits(ex1).Should().Equal(0, 1, 1);
36-
CountBits(ex2).Should().Equal(0, 1, 1, 2, 1, 2);
37-
}
29+
[Theory]
30+
[InlineData(2, new[] { 0, 1, 1 })]
31+
[InlineData(5, new[] { 0, 1, 1, 2, 1, 2 })]
32+
public void CountBitsTest(int n, int[] expected) => CountBits(n).Should().Equal(expected);
3833
}

LeetCode.CSharp/Problems/DiameterOfBinaryTree.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public static int DiameterOfBinaryTree(TreeNode root)
1010
{
1111
var maxDiameter = 0;
1212

13+
DepthFirstSearch(root);
14+
15+
return maxDiameter;
16+
1317
// Calculates height and stores maximum diameter
1418
int DepthFirstSearch(TreeNode? node)
1519
{
@@ -26,10 +30,6 @@ int DepthFirstSearch(TreeNode? node)
2630
// Add 1 to include current node's height
2731
return Math.Max(left, right) + 1;
2832
}
29-
30-
DepthFirstSearch(root);
31-
32-
return maxDiameter;
3333
}
3434

3535
[Fact]

LeetCode.CSharp/Problems/IsBalanced.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public sealed partial class Problem
88
"https://www.youtube.com/watch?v=QfJsau0ItOY")]
99
public static bool IsBalanced(TreeNode root)
1010
{
11+
return DepthFirstSearch(root).IsBalanced;
12+
1113
static (bool IsBalanced, int Height) DepthFirstSearch(TreeNode? node)
1214
{
1315
if (node is null)
@@ -19,24 +21,22 @@ public static bool IsBalanced(TreeNode root)
1921
var right = DepthFirstSearch(node.right);
2022

2123
var isBalanced = left.IsBalanced && right.IsBalanced
22-
&& Math.Abs(left.Height - right.Height) <= 1;
24+
&& Math.Abs(left.Height - right.Height) <= 1;
2325

2426
// Add 1 to include current node's height
2527
var height = Math.Max(left.Height, right.Height) + 1;
2628

2729
return (isBalanced, height);
2830
}
29-
30-
return DepthFirstSearch(root).IsBalanced;
3131
}
3232

3333
[Fact]
3434
public void IsBalancedTest()
3535
{
36-
var root1 = new TreeNode(new int?[] { 3, 9, 20, null, null, 15, 7 });
37-
var root2 = new TreeNode(new int?[] { 1, 2, 2, 3, 3, null, null, 4, 4 });
38-
var root3 = new TreeNode(new int?[] { 1, 2, null, 3 });
39-
var root4 = new TreeNode(new int?[] { 1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8 });
36+
var root1 = new TreeNode([3, 9, 20, null, null, 15, 7]);
37+
var root2 = new TreeNode([1, 2, 2, 3, 3, null, null, 4, 4]);
38+
var root3 = new TreeNode([1, 2, null, 3]);
39+
var root4 = new TreeNode([1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8]);
4040
var root5 = new TreeNode();
4141

4242
IsBalanced(root1).Should().BeTrue();

LeetCode.CSharp/Problems/IsHappy.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ public static bool IsHappy(int n)
1010
{
1111
var visited = new HashSet<int>();
1212

13-
int SumOfSquares(int i)
14-
{
15-
var result = 0;
16-
17-
while (i != 0)
18-
{
19-
var digit = i % 10;
20-
digit *= digit;
21-
result += digit;
22-
i /= 10;
23-
}
24-
return result;
25-
}
26-
2713
while (!visited.Contains(n))
2814
{
2915
visited.Add(n);
@@ -37,6 +23,20 @@ int SumOfSquares(int i)
3723
}
3824

3925
return false; // :(
26+
27+
static int SumOfSquares(int i)
28+
{
29+
var result = 0;
30+
31+
while (i != 0)
32+
{
33+
var digit = i % 10;
34+
digit *= digit;
35+
result += digit;
36+
i /= 10;
37+
}
38+
return result;
39+
}
4040
}
4141

4242
[Fact]

0 commit comments

Comments
 (0)