Skip to content

Commit 4094e51

Browse files
committed
Improved tasks 399, 530, 637
1 parent e0ff6de commit 4094e51

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// For comparing double arrays with precision, use a loop or a custom assertion method
32+
Assert.Equal(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
33+
}
34+
35+
[Fact]
36+
public void CalcEquation2() {
37+
IList<IList<string>> equations =
38+
ArrayUtils.GetLists(new string[][] {new string[] {"a", "b"}, new string[] {"b", "c"}, new string[] {"bc", "cd"}});
39+
double[] values = new double[] {1.5, 2.5, 5.0};
40+
IList<IList<string>> queries =
41+
ArrayUtils.GetLists(
42+
new string[][] {new string[] {"a", "c"}, new string[] {"c", "b"}, new string[] {"bc", "cd"}, new string[] {"cd", "bc"}});
43+
double[] expected = {3.75000, 0.40000, 5.00000, 0.20000};
44+
Assert.Equal(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
45+
}
46+
47+
[Fact]
48+
public void CalcEquation3() {
49+
// Direct initialization of List<List<string>>
50+
IList<IList<string>> equations = new List<IList<string>> {
51+
new List<string> {"a", "b"}
52+
};
53+
double[] values = new double[] {0.5};
54+
IList<IList<string>> queries =
55+
ArrayUtils.GetLists(
56+
new string[][] {new string[] {"a", "b"}, new string[] {"b", "a"}, new string[] {"a", "c"}, new string[] {"x", "y"}});
57+
double[] expected = {0.50000, 2.00000, -1.00000, -1.00000};
58+
Assert.Equal(expected, new Solution().CalcEquation(equations, values, queries), Tolerance);
59+
}
60+
}
61+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace LeetCodeNet.G0601_0700.S0637_average_of_levels_in_binary_tree {
2+
3+
using System;
4+
using LeetCodeNet.Com_github_leetcode;
5+
using Xunit;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void AverageOfLevels() {
10+
TreeNode treeNode = TreeNode.Create(new List<int?> {3, 9, 20, null, null, 15, 7});
11+
Assert.Equal(new List<double> {3.00000, 14.50000, 11.00000}, new Solution().AverageOfLevels(treeNode),
12+
new DoubleListComparer(0.00001));
13+
}
14+
15+
[Fact]
16+
public void AverageOfLevels2() {
17+
TreeNode treeNode = TreeNode.Create(new List<int?> {3, 9, 20, 15, 7});
18+
Assert.Equal(new List<double> {3.00000, 14.50000, 11.00000}, new Solution().AverageOfLevels(treeNode),
19+
new DoubleListComparer(0.00001));
20+
}
21+
}
22+
23+
public class DoubleListComparer : IEqualityComparer<IList<double>> {
24+
private readonly double _tolerance;
25+
26+
public DoubleListComparer(double tolerance) {
27+
_tolerance = tolerance;
28+
}
29+
30+
public bool Equals(IList<double> x, IList<double> y) {
31+
if (ReferenceEquals(x, y)) return true;
32+
if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) return false;
33+
if (x.Count != y.Count) return false;
34+
35+
for (int i = 0; i < x.Count; i++) {
36+
if (Math.Abs(x[i] - y[i]) > _tolerance) {
37+
return false;
38+
}
39+
}
40+
return true;
41+
}
42+
43+
public int GetHashCode(IList<double> obj) {
44+
int hash = 17;
45+
foreach (var d in obj) {
46+
hash = hash * 31 + d.GetHashCode();
47+
}
48+
return hash;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)