Skip to content

Commit ae68363

Browse files
authored
Merge branch 'master' into fix/update-tests-for-timsort-and-hashtable
2 parents 476437e + a985c67 commit ae68363

File tree

89 files changed

+160
-440
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+160
-440
lines changed

Algorithms/Encoders/SoundexEncoder.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ namespace Algorithms.Encoders;
55
/// </summary>
66
public class SoundexEncoder
77
{
8+
private static readonly Dictionary<char, int> CharacterMapping = new()
9+
{
10+
['a'] = 0,
11+
['e'] = 0,
12+
['i'] = 0,
13+
['o'] = 0,
14+
['u'] = 0,
15+
['y'] = 0,
16+
['h'] = 8,
17+
['w'] = 8,
18+
['b'] = 1,
19+
['f'] = 1,
20+
['p'] = 1,
21+
['v'] = 1,
22+
['c'] = 2,
23+
['g'] = 2,
24+
['j'] = 2,
25+
['k'] = 2,
26+
['q'] = 2,
27+
['s'] = 2,
28+
['x'] = 2,
29+
['z'] = 2,
30+
['d'] = 3,
31+
['t'] = 3,
32+
['l'] = 4,
33+
['m'] = 5,
34+
['n'] = 5,
35+
['r'] = 6,
36+
};
37+
838
/// <summary>
939
/// Encodes a string using the Soundex Algorithm.
1040
/// </summary>
@@ -72,36 +102,6 @@ private IEnumerable<int> CollapseDoubles(IEnumerable<int> numbers)
72102

73103
private int MapToNumber(char ch)
74104
{
75-
var mapping = new Dictionary<char, int>
76-
{
77-
['a'] = 0,
78-
['e'] = 0,
79-
['i'] = 0,
80-
['o'] = 0,
81-
['u'] = 0,
82-
['y'] = 0,
83-
['h'] = 8,
84-
['w'] = 8,
85-
['b'] = 1,
86-
['f'] = 1,
87-
['p'] = 1,
88-
['v'] = 1,
89-
['c'] = 2,
90-
['g'] = 2,
91-
['j'] = 2,
92-
['k'] = 2,
93-
['q'] = 2,
94-
['s'] = 2,
95-
['x'] = 2,
96-
['z'] = 2,
97-
['d'] = 3,
98-
['t'] = 3,
99-
['l'] = 4,
100-
['m'] = 5,
101-
['n'] = 5,
102-
['r'] = 6,
103-
};
104-
105-
return mapping[ch];
105+
return CharacterMapping[ch];
106106
}
107107
}

Algorithms/Other/Luhn.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,49 @@ public static class Luhn
2121
public static bool Validate(string number) => GetSum(number) % 10 == 0;
2222

2323
/// <summary>
24-
/// This algorithm only finds one number.
24+
/// This algorithm finds one missing digit.
2525
/// In place of the unknown digit, put "x".
2626
/// </summary>
2727
/// <param name="number">The number in which to find the missing digit.</param>
2828
/// <returns>Missing digit.</returns>
2929
public static int GetLostNum(string number)
3030
{
31-
var lostIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase);
32-
var lostNum = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10;
31+
var missingDigitIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase);
32+
var checkDigit = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10;
3333

34-
// Case 1: If the index of the lost digit is even.
35-
if (lostIndex % 2 == 0)
34+
// Case 1: If the index of the missing digit is even.
35+
if (missingDigitIndex % 2 == 0)
3636
{
37-
return lostNum;
37+
return checkDigit;
3838
}
3939

40-
var tempLostNum = lostNum / 2;
40+
var candidateDigit = checkDigit / 2;
4141

42-
// Case 2: if the index of the lost digit isn`t even and that number <= 4.
43-
// Case 3: if the index of the lost digit isn`t even and that number > 4.
44-
return Validate(number.Replace("x", tempLostNum.ToString())) ? tempLostNum : (lostNum + 9) / 2;
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;
4545
}
4646

4747
/// <summary>
48-
/// Computes the sum found by the algorithm.
48+
/// Computes the sum found by the Luhn algorithm.
4949
/// </summary>
50-
/// <param name="number">The number for which the sum will be found.</param>
50+
/// <param name="number">The number for which the sum will be calculated.</param>
5151
/// <returns>Sum.</returns>
5252
private static int GetSum(string number)
5353
{
5454
var sum = 0;
5555
for (var i = 0; i < number.Length; i++)
5656
{
57-
var d = number[i] - '0';
58-
d = (i + number.Length) % 2 == 0
59-
? 2 * d
60-
: d;
61-
if (d > 9)
57+
var digit = number[i] - '0';
58+
digit = (i + number.Length) % 2 == 0
59+
? 2 * digit
60+
: digit;
61+
if (digit > 9)
6262
{
63-
d -= 9;
63+
digit -= 9;
6464
}
6565

66-
sum += d;
66+
sum += digit;
6767
}
6868

6969
return sum;

DataStructures.Tests/AATreeTests.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using DataStructures.AATree;
5-
using FluentAssertions;
6-
using NUnit.Framework;
72

83
namespace DataStructures.Tests;
94

DataStructures.Tests/AVLTreeTests.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using DataStructures.AVLTree;
5-
using FluentAssertions;
6-
using NUnit.Framework;
72
using static FluentAssertions.FluentActions;
83

94
namespace DataStructures.Tests;
@@ -233,19 +228,19 @@ public void Remove_MultipleKeys_TreeStillValid_Variant2()
233228
tree.GetKeysInOrder()
234229
.Should()
235230
.BeEquivalentTo(
236-
new[] { 4,6,8 },
231+
new[] { 4, 6, 8 },
237232
config => config.WithStrictOrdering());
238233

239234
tree.GetKeysPreOrder()
240235
.Should()
241236
.BeEquivalentTo(
242-
new[] { 6,4,8 },
237+
new[] { 6, 4, 8 },
243238
config => config.WithStrictOrdering());
244239

245240
tree.GetKeysPostOrder()
246241
.Should()
247242
.BeEquivalentTo(
248-
new[] { 4,8,6 },
243+
new[] { 4, 8, 6 },
249244
config => config.WithStrictOrdering());
250245
}
251246

DataStructures.Tests/BinarySearchTreeTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using DataStructures.BinarySearchTree;
5-
using NUnit.Framework;
62

73
namespace DataStructures.Tests;
84

DataStructures.Tests/BitArrayTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using FluentAssertions;
3-
using NUnit.Framework;
4-
51
namespace DataStructures.Tests;
62

73
/// <summary>

DataStructures.Tests/Cache/LfuCacheTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using DataStructures.Cache;
2-
using FluentAssertions;
3-
using NUnit.Framework;
42

53
namespace DataStructures.Tests.Cache;
64

DataStructures.Tests/Cache/LruCacheTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using DataStructures.Cache;
2-
using FluentAssertions;
3-
using NUnit.Framework;
42

53
namespace DataStructures.Tests.Cache;
64

DataStructures.Tests/DisjointSet/DisjointSetTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using DataStructures.DisjointSet;
2-
using FluentAssertions;
3-
using NUnit.Framework;
42

53
namespace DataStructures.Tests.DisjointSet;
64

DataStructures.Tests/Fenwick/BinaryIndexedTreeTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using DataStructures.Fenwick;
2-
using FluentAssertions;
3-
using NUnit.Framework;
42

53
namespace DataStructures.Tests.Fenwick;
64

0 commit comments

Comments
 (0)