|
| 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 | +} |
0 commit comments