Skip to content

Commit 72a4e7e

Browse files
committed
Revert "Optimize stage 1"
This reverts commit 89390c3.
1 parent 89390c3 commit 72a4e7e

File tree

3 files changed

+160
-44
lines changed

3 files changed

+160
-44
lines changed

Algorithms/Other/Luhn.cs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,62 @@ namespace Algorithms.Other;
1010
/// </summary>
1111
public static class Luhn
1212
{
13-
// Checking the validity of a sequence of numbers.
13+
/// <summary>
14+
/// Checking the validity of a sequence of numbers.
15+
/// </summary>
16+
/// <param name="number">The number that will be checked for validity.</param>
17+
/// <returns>
18+
/// True: Number is valid.
19+
/// False: Number isn`t valid.
20+
/// </returns>
1421
public static bool Validate(string number) => GetSum(number) % 10 == 0;
1522

16-
// Finds one missing digit. In place of the unknown digit, put "x".
23+
/// <summary>
24+
/// This algorithm finds one missing digit.
25+
/// In place of the unknown digit, put "x".
26+
/// </summary>
27+
/// <param name="number">The number in which to find the missing digit.</param>
28+
/// <returns>Missing digit.</returns>
1729
public static int GetLostNum(string number)
1830
{
19-
var missingDigitIndex = number.Length - 1 - number.LastIndexOf('x');
20-
var checkDigit = GetSum(number.Replace("x", "0")) * 9 % 10;
21-
22-
return missingDigitIndex % 2 == 0
23-
? checkDigit
24-
: Validate(number.Replace("x", (checkDigit / 2).ToString()))
25-
? checkDigit / 2
26-
: (checkDigit + 9) / 2;
31+
var missingDigitIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase);
32+
var checkDigit = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10;
33+
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;
2745
}
2846

29-
// Computes the sum found by the Luhn algorithm, optimized with Span.
47+
/// <summary>
48+
/// Computes the sum found by the Luhn algorithm.
49+
/// </summary>
50+
/// <param name="number">The number for which the sum will be calculated.</param>
51+
/// <returns>Sum.</returns>
3052
private static int GetSum(string number)
3153
{
3254
var sum = 0;
33-
var span = number.AsSpan();
34-
for (var i = 0; i < span.Length; i++)
55+
for (var i = 0; i < number.Length; i++)
3556
{
36-
var c = span[i];
37-
if (c is < '0' or > '9') continue;
38-
var digit = c - '0';
39-
digit = (i + span.Length) % 2 == 0 ? 2 * digit : digit;
40-
if (digit > 9) digit -= 9;
57+
var digit = number[i] - '0';
58+
digit = (i + number.Length) % 2 == 0
59+
? 2 * digit
60+
: digit;
61+
if (digit > 9)
62+
{
63+
digit -= 9;
64+
}
65+
4166
sum += digit;
4267
}
68+
4369
return sum;
4470
}
4571
}

DataStructures/BinarySearchTree/BinarySearchTree.cs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,88 @@ namespace DataStructures.BinarySearchTree;
1515
/// <typeparam name="TKey">Type of key for the BST. Keys must implement IComparable.</typeparam>
1616
public class BinarySearchTree<TKey>
1717
{
18-
// Comparer to use when comparing node elements/keys.
18+
/// <summary>
19+
/// Comparer to use when comparing node elements/keys.
20+
/// </summary>
1921
private readonly Comparer<TKey> comparer;
2022

21-
// Gets the root of the BST.
23+
/// <summary>
24+
/// Gets the root of the BST.
25+
/// </summary>
2226
public BinarySearchTreeNode<TKey>? Root { get; private set; }
2327

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

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

28-
// Gets the number nodes currently in the BST.
42+
/// <summary>
43+
/// Gets the number nodes currently in the BST.
44+
/// </summary>
2945
public int Count { get; private set; }
3046

31-
// Insert a key into the BST.
47+
/// <summary>
48+
/// Insert a key into the BST.
49+
/// </summary>
50+
/// <param name="key">The key to insert.</param>
51+
/// <exception cref="ArgumentException">
52+
/// Thrown if key is already in BST.
53+
/// </exception>
3254
public void Add(TKey key)
3355
{
3456
if (Root is null)
57+
{
3558
Root = new BinarySearchTreeNode<TKey>(key);
59+
}
3660
else
61+
{
3762
Add(Root, key);
63+
}
64+
3865
Count++;
3966
}
4067

41-
// Insert multiple keys into the BST.
68+
/// <summary>
69+
/// Insert multiple keys into the BST.
70+
/// Keys are inserted in the order they appear in the sequence.
71+
/// </summary>
72+
/// <param name="keys">Sequence of keys to insert.</param>
4273
public void AddRange(IEnumerable<TKey> keys)
4374
{
4475
foreach (var key in keys)
76+
{
4577
Add(key);
78+
}
4679
}
4780

48-
// Find a node with the specified key.
81+
/// <summary>
82+
/// Find a node with the specified key.
83+
/// </summary>
84+
/// <param name="key">The key to search for.</param>
85+
/// <returns>The node with the specified key if it exists, otherwise a default value is returned.</returns>
4986
public BinarySearchTreeNode<TKey>? Search(TKey key) => Search(Root, key);
5087

51-
// Checks if the specified key is in the BST.
88+
/// <summary>
89+
/// Checks if the specified key is in the BST.
90+
/// </summary>
91+
/// <param name="key">The key to search for.</param>
92+
/// <returns>true if the key is in the BST, false otherwise.</returns>
5293
public bool Contains(TKey key) => Search(Root, key) is not null;
5394

54-
// Removes a node with a key that matches key.
95+
/// <summary>
96+
/// Removes a node with a key that matches <paramref name="key" />.
97+
/// </summary>
98+
/// <param name="key">The key to search for.</param>
99+
/// <returns>true if the removal was successful, false otherwise.</returns>
55100
public bool Remove(TKey key)
56101
{
57102
if (Root is null)

DataStructures/Timeline.cs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ this data structure can be used to represent an ordered series of dates or times
1212
330: Constantine move the capital to Constantinople.
1313
*/
1414

15+
using System.Collections;
1516

1617
namespace DataStructures;
1718

@@ -22,39 +23,83 @@ namespace DataStructures;
2223
/// <typeparam name="TValue">Value associated with a <see cref="DateTime" />.</typeparam>
2324
public class Timeline<TValue> : ICollection<(DateTime Time, TValue Value)>, IEquatable<Timeline<TValue>>
2425
{
25-
// Inner collection storing the timeline events as key-tuples.
26-
private List<(DateTime Time, TValue Value)> timeline = [];
26+
/// <summary>
27+
/// Inner collection storing the timeline events as key-tuples.
28+
/// </summary>
29+
private readonly List<(DateTime Time, TValue Value)> timeline = new();
2730

28-
// Default constructor
29-
public Timeline() { }
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="Timeline{TValue}"/> class.
33+
/// </summary>
34+
public Timeline()
35+
{
36+
}
3037

31-
// Constructor with initial event
32-
public Timeline(DateTime time, TValue value) => timeline = [(time, value)];
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="Timeline{TValue}"/> class populated with an initial event.
40+
/// </summary>
41+
/// <param name="time">The time at which the given event occurred.</param>
42+
/// <param name="value">The event's content.</param>
43+
public Timeline(DateTime time, TValue value)
44+
=> timeline = new List<(DateTime, TValue)>
45+
{
46+
(time, value),
47+
};
3348

34-
// Constructor with provided events, ordered chronologically
35-
public Timeline(params (DateTime, TValue)[] timeline) => this.timeline = timeline.OrderBy(pair => pair.Item1).ToList();
49+
/// <summary>
50+
/// Initializes a new instance of the <see cref="Timeline{TValue}"/> class containing the provided events
51+
/// ordered chronologically.
52+
/// </summary>
53+
/// <param name="timeline">The timeline to represent.</param>
54+
public Timeline(params (DateTime, TValue)[] timeline)
55+
=> this.timeline = timeline
56+
.OrderBy(pair => pair.Item1)
57+
.ToList();
3658

37-
// Gets the number of unique times within this timeline.
38-
public int TimesCount => GetAllTimes().Length;
59+
/// <summary>
60+
/// Gets he number of unique times within this timeline.
61+
/// </summary>
62+
public int TimesCount
63+
=> GetAllTimes().Length;
3964

40-
// Gets all events that has occurred in this timeline.
41-
public int ValuesCount => GetAllValues().Length;
65+
/// <summary>
66+
/// Gets all events that has occurred in this timeline.
67+
/// </summary>
68+
public int ValuesCount
69+
=> GetAllValues().Length;
4270

43-
// Get or set all values associated with a time
71+
/// <summary>
72+
/// Get all values associated with <paramref name="time" />.
73+
/// </summary>
74+
/// <param name="time">Time to get values for.</param>
75+
/// <returns>Values associated with <paramref name="time" />.</returns>
4476
public TValue[] this[DateTime time]
4577
{
4678
get => GetValuesByTime(time);
4779
set
4880
{
49-
timeline.RemoveAll(@event => @event.Time == time);
81+
var overridenEvents = timeline.Where(@event => @event.Time == time).ToList();
82+
foreach (var @event in overridenEvents)
83+
{
84+
timeline.Remove(@event);
85+
}
86+
5087
foreach (var v in value)
88+
{
5189
Add(time, v);
90+
}
5291
}
5392
}
5493

55-
bool ICollection<(DateTime Time, TValue Value)>.IsReadOnly => false;
94+
/// <inheritdoc />
95+
bool ICollection<(DateTime Time, TValue Value)>.IsReadOnly
96+
=> false;
5697

57-
public int Count => timeline.Count;
98+
/// <summary>
99+
/// Gets the count of pairs.
100+
/// </summary>
101+
public int Count
102+
=> timeline.Count;
58103

59104
/// <summary>
60105
/// Clear the timeline, removing all events.

0 commit comments

Comments
 (0)