diff --git a/Algorithms.Tests/Other/LuhnTests.cs b/Algorithms.Tests/Other/LuhnTests.cs index 316e8b4e..c0ecbf66 100644 --- a/Algorithms.Tests/Other/LuhnTests.cs +++ b/Algorithms.Tests/Other/LuhnTests.cs @@ -56,4 +56,29 @@ public void GetLostNum(string number) // Assert Assert.That(validate, Is.True); } + + [TestCase("")] + [TestCase("xxxx")] + [TestCase("abcde")] + [TestCase("1x345678901234567")] + [TestCase("x1234567890123456")] + [TestCase("1234567890123456x")] + [TestCase("1111111111111111")] + [TestCase("1a2b3c4d5e6f7g8h9i0j")] + public void EdgeCases_GetLostNum(string number) + { + // Act + int lostNum = Luhn.GetLostNum(number.Replace("x", "0")); + // Assert + Assert.That(lostNum, Is.InRange(0, 9)); + } + + [TestCase("1a2b3c4d5e6f7g8h9i0j")] + public void EdgeCases_Validate(string number) + { + // Act + bool result = Luhn.Validate(number); + // Assert + Assert.That(result, Is.False); + } } diff --git a/Algorithms/Other/Luhn.cs b/Algorithms/Other/Luhn.cs index 58d652b9..b7b7836a 100644 --- a/Algorithms/Other/Luhn.cs +++ b/Algorithms/Other/Luhn.cs @@ -31,17 +31,11 @@ public static int GetLostNum(string number) var missingDigitIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase); var checkDigit = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10; - // Case 1: If the index of the missing digit is even. - if (missingDigitIndex % 2 == 0) - { - return checkDigit; - } - - var candidateDigit = checkDigit / 2; - - // Case 2: if the index of the missing digit is odd and the candidate is valid. - // Case 3: if the index of the missing digit is odd and we need the alternative. - return Validate(number.Replace("x", candidateDigit.ToString())) ? candidateDigit : (checkDigit + 9) / 2; + return missingDigitIndex % 2 == 0 + ? checkDigit + : Validate(number.Replace("x", (checkDigit / 2).ToString())) + ? checkDigit / 2 + : (checkDigit + 9) / 2; } /// @@ -52,12 +46,17 @@ public static int GetLostNum(string number) private static int GetSum(string number) { var sum = 0; - for (var i = 0; i < number.Length; i++) + var span = number.AsSpan(); + for (var i = 0; i < span.Length; i++) { - var digit = number[i] - '0'; - digit = (i + number.Length) % 2 == 0 - ? 2 * digit - : digit; + var c = span[i]; + if (c is < '0' or > '9') + { + continue; + } + + var digit = c - '0'; + digit = (i + span.Length) % 2 == 0 ? 2 * digit : digit; if (digit > 9) { digit -= 9; diff --git a/DataStructures/BinarySearchTree/BinarySearchTree.cs b/DataStructures/BinarySearchTree/BinarySearchTree.cs index 8a6f18a3..01b7026a 100644 --- a/DataStructures/BinarySearchTree/BinarySearchTree.cs +++ b/DataStructures/BinarySearchTree/BinarySearchTree.cs @@ -25,19 +25,9 @@ public class BinarySearchTree /// public BinarySearchTreeNode? Root { get; private set; } - public BinarySearchTree() - { - Root = null; - Count = 0; - comparer = Comparer.Default; - } + public BinarySearchTree() => (Root, Count, comparer) = (null, 0, Comparer.Default); - public BinarySearchTree(Comparer customComparer) - { - Root = null; - Count = 0; - comparer = customComparer; - } + public BinarySearchTree(Comparer customComparer) => (Root, Count, comparer) = (null, 0, customComparer); /// /// Gets the number nodes currently in the BST. diff --git a/DataStructures/Timeline.cs b/DataStructures/Timeline.cs index a0e110d2..ff218847 100644 --- a/DataStructures/Timeline.cs +++ b/DataStructures/Timeline.cs @@ -78,12 +78,7 @@ public TValue[] this[DateTime time] get => GetValuesByTime(time); set { - var overridenEvents = timeline.Where(@event => @event.Time == time).ToList(); - foreach (var @event in overridenEvents) - { - timeline.Remove(@event); - } - + timeline.RemoveAll(@event => @event.Time == time); foreach (var v in value) { Add(time, v);