Skip to content

Commit 5c99b3a

Browse files
ducnt7-rksngtduc693
authored andcommitted
Fix issue #534
Author: Duc Nguyen <[email protected]>
1 parent 6c220ce commit 5c99b3a

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

Algorithms.Tests/Other/LuhnTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,29 @@ public void GetLostNum(string number)
5656
// Assert
5757
Assert.That(validate, Is.True);
5858
}
59+
60+
[TestCase("")]
61+
[TestCase("xxxx")]
62+
[TestCase("abcde")]
63+
[TestCase("1x345678901234567")]
64+
[TestCase("x1234567890123456")]
65+
[TestCase("1234567890123456x")]
66+
[TestCase("1111111111111111")]
67+
[TestCase("1a2b3c4d5e6f7g8h9i0j")]
68+
public void EdgeCases_GetLostNum(string number)
69+
{
70+
// Act
71+
int lostNum = Luhn.GetLostNum(number.Replace("x", "0"));
72+
// Assert
73+
Assert.That(lostNum, Is.InRange(0, 9));
74+
}
75+
76+
[TestCase("1a2b3c4d5e6f7g8h9i0j")]
77+
public void EdgeCases_Validate(string number)
78+
{
79+
// Act
80+
bool result = Luhn.Validate(number);
81+
// Assert
82+
Assert.That(result, Is.False);
83+
}
5984
}

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)