Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Algorithms.Tests/Other/LuhnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
31 changes: 15 additions & 16 deletions Algorithms/Other/Luhn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -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;
Expand Down
14 changes: 2 additions & 12 deletions DataStructures/BinarySearchTree/BinarySearchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,9 @@ public class BinarySearchTree<TKey>
/// </summary>
public BinarySearchTreeNode<TKey>? Root { get; private set; }

public BinarySearchTree()
{
Root = null;
Count = 0;
comparer = Comparer<TKey>.Default;
}
public BinarySearchTree() => (Root, Count, comparer) = (null, 0, Comparer<TKey>.Default);

public BinarySearchTree(Comparer<TKey> customComparer)
{
Root = null;
Count = 0;
comparer = customComparer;
}
public BinarySearchTree(Comparer<TKey> customComparer) => (Root, Count, comparer) = (null, 0, customComparer);

/// <summary>
/// Gets the number nodes currently in the BST.
Expand Down
7 changes: 1 addition & 6 deletions DataStructures/Timeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down