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 c832071c..74f96161 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 d8628526..e74b6b3b 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);