Skip to content

Commit 2cc5663

Browse files
authored
Improved tasks 399, 530, 637
1 parent e0ff6de commit 2cc5663

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace LeetCodeNet.G0301_0400.S0399_evaluate_division {
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
public static class ArrayUtils {
9+
public static IList<IList<string>> GetLists(string[][] array) {
10+
var result = new List<IList<string>>();
11+
foreach (var innerArray in array) {
12+
result.Add(innerArray.ToList());
13+
}
14+
return result;
15+
}
16+
}
17+
18+
public class SolutionTest {
19+
private const double Tolerance = 0.00001;
20+
21+
[Fact]
22+
public void CalcEquation() {
23+
IList<IList<string>> equations = ArrayUtils.GetLists(new string[][] {new string[] {"a", "b"}, new string[] {"b", "c"}});
24+
double[] values = new double[] {2.0, 3.0};
25+
IList<IList<string>> queries =
26+
ArrayUtils.GetLists(
27+
new string[][] {
28+
new string[] {"a", "c"}, new string[] {"b", "a"}, new string[] {"a", "e"}, new string[] {"a", "a"}, new string[] {"x", "x"}
29+
});
30+
double[] expected = {6.00000, 0.50000, -1.00000, 1.00000, -1.00000};
31+
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
32+
}
33+
34+
[Fact]
35+
public void CalcEquation2() {
36+
IList<IList<string>> equations =
37+
ArrayUtils.GetLists(new string[][] {new string[] {"a", "b"}, new string[] {"b", "c"}, new string[] {"bc", "cd"}});
38+
double[] values = new double[] {1.5, 2.5, 5.0};
39+
IList<IList<string>> queries =
40+
ArrayUtils.GetLists(
41+
new string[][] {new string[] {"a", "c"}, new string[] {"c", "b"}, new string[] {"bc", "cd"}, new string[] {"cd", "bc"}});
42+
double[] expected = {3.75000, 0.40000, 5.00000, 0.20000};
43+
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
44+
}
45+
46+
[Fact]
47+
public void CalcEquation3() {
48+
IList<IList<string>> equations = new List<IList<string>> {
49+
new List<string> {"a", "b"}
50+
};
51+
double[] values = new double[] {0.5};
52+
IList<IList<string>> queries =
53+
ArrayUtils.GetLists(
54+
new string[][] {new string[] {"a", "b"}, new string[] {"b", "a"}, new string[] {"a", "c"}, new string[] {"x", "y"}});
55+
double[] expected = {0.50000, 2.00000, -1.00000, -1.00000};
56+
AssertEqualWithTolerance(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
57+
}
58+
59+
private static void AssertEqualWithTolerance(double[] expected, double[] actual, double tolerance) {
60+
Assert.Equal(expected.Length, actual.Length);
61+
for (int i = 0; i < expected.Length; i++) {
62+
Assert.InRange(actual[i], expected[i] - tolerance, expected[i] + tolerance);
63+
}
64+
}
65+
}
66+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0501_0600.S0530_minimum_absolute_difference_in_bst {
2+
3+
using System;
4+
using LeetCodeNet.Com_github_leetcode;
5+
using Xunit;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void GetMinimumDifference() {
10+
TreeNode treeNode = TreeUtils.ConstructBinaryTree(new List<int?> {4, 2, 6, 1, 3});
11+
Assert.Equal(1, new Solution().GetMinimumDifference(treeNode));
12+
}
13+
14+
[Fact]
15+
public void GetMinimumDifference2() {
16+
TreeNode treeNode =
17+
TreeUtils.ConstructBinaryTree(new List<int?> {1, 0, 48, null, null, 12, 49});
18+
Assert.Equal(1, new Solution().GetMinimumDifference(treeNode));
19+
}
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LeetCodeNet.G0601_0700.S0637_average_of_levels_in_binary_tree {
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using LeetCodeNet.Com_github_leetcode;
7+
using Xunit;
8+
9+
public class SolutionTest {
10+
[Fact]
11+
public void AverageOfLevels() {
12+
TreeNode treeNode = TreeNode.Create(new List<int?> { 3, 9, 20, null, null, 15, 7 });
13+
var expected = new List<double> { 3.00000, 14.50000, 11.00000 };
14+
var actual = new Solution().AverageOfLevels(treeNode);
15+
AssertEqualWithTolerance(expected, actual, 0.00001);
16+
}
17+
18+
[Fact]
19+
public void AverageOfLevels2() {
20+
TreeNode treeNode = TreeNode.Create(new List<int?> { 3, 9, 20, 15, 7 });
21+
var expected = new List<double> { 3.00000, 14.50000, 11.00000 };
22+
var actual = new Solution().AverageOfLevels(treeNode);
23+
AssertEqualWithTolerance(expected, actual, 0.00001);
24+
}
25+
26+
private void AssertEqualWithTolerance(IList<double> expected, IList<double> actual, double tolerance) {
27+
Assert.Equal(expected.Count, actual.Count);
28+
for (var i = 0; i < expected.Count; i++) {
29+
Assert.True(Math.Abs(expected[i] - actual[i]) <= tolerance,
30+
$"Mismatch at index {i}: expected {expected[i]}, actual {actual[i]}");
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)