Skip to content

Commit 3ada9f7

Browse files
ducnt7-rksngtduc693
authored andcommitted
Fix issue #534
1 parent 6c220ce commit 3ada9f7

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

Algorithms/Other/Luhn.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,11 @@ public static int GetLostNum(string number)
3131
var missingDigitIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase);
3232
var checkDigit = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10;
3333

34-
// Case 1: If the index of the missing digit is even.
35-
if (missingDigitIndex % 2 == 0)
36-
{
37-
return checkDigit;
38-
}
39-
40-
var candidateDigit = checkDigit / 2;
41-
42-
// Case 2: if the index of the missing digit is odd and the candidate is valid.
43-
// Case 3: if the index of the missing digit is odd and we need the alternative.
44-
return Validate(number.Replace("x", candidateDigit.ToString())) ? candidateDigit : (checkDigit + 9) / 2;
34+
return missingDigitIndex % 2 == 0
35+
? checkDigit
36+
: Validate(number.Replace("x", (checkDigit / 2).ToString()))
37+
? checkDigit / 2
38+
: (checkDigit + 9) / 2;
4539
}
4640

4741
/// <summary>
@@ -52,12 +46,17 @@ public static int GetLostNum(string number)
5246
private static int GetSum(string number)
5347
{
5448
var sum = 0;
55-
for (var i = 0; i < number.Length; i++)
49+
var span = number.AsSpan();
50+
for (var i = 0; i < span.Length; i++)
5651
{
57-
var digit = number[i] - '0';
58-
digit = (i + number.Length) % 2 == 0
59-
? 2 * digit
60-
: digit;
52+
var c = span[i];
53+
if (c is < '0' or > '9')
54+
{
55+
continue;
56+
}
57+
58+
var digit = c - '0';
59+
digit = (i + span.Length) % 2 == 0 ? 2 * digit : digit;
6160
if (digit > 9)
6261
{
6362
digit -= 9;

DataStructures/BinarySearchTree/BinarySearchTree.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,9 @@ public class BinarySearchTree<TKey>
2525
/// </summary>
2626
public BinarySearchTreeNode<TKey>? Root { get; private set; }
2727

28-
public BinarySearchTree()
29-
{
30-
Root = null;
31-
Count = 0;
32-
comparer = Comparer<TKey>.Default;
33-
}
28+
public BinarySearchTree() => (Root, Count, comparer) = (null, 0, Comparer<TKey>.Default);
3429

35-
public BinarySearchTree(Comparer<TKey> customComparer)
36-
{
37-
Root = null;
38-
Count = 0;
39-
comparer = customComparer;
40-
}
30+
public BinarySearchTree(Comparer<TKey> customComparer) => (Root, Count, comparer) = (null, 0, customComparer);
4131

4232
/// <summary>
4333
/// Gets the number nodes currently in the BST.

DataStructures/Timeline.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ public TValue[] this[DateTime time]
7878
get => GetValuesByTime(time);
7979
set
8080
{
81-
var overridenEvents = timeline.Where(@event => @event.Time == time).ToList();
82-
foreach (var @event in overridenEvents)
83-
{
84-
timeline.Remove(@event);
85-
}
86-
81+
timeline.RemoveAll(@event => @event.Time == time);
8782
foreach (var v in value)
8883
{
8984
Add(time, v);

0 commit comments

Comments
 (0)