Skip to content

Commit cb20589

Browse files
authored
Merge branch 'master' into globalUsing-datastructures/issue-520
2 parents b939ca0 + ca458ab commit cb20589

File tree

4 files changed

+79
-138
lines changed

4 files changed

+79
-138
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/BitArray.cs

Lines changed: 28 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ public sealed class BitArray : ICloneable, IEnumerator<bool>, IEnumerable<bool>
135135
/// <param name="n">length of the array.</param>
136136
public BitArray(int n)
137137
{
138-
if (n < 1)
139-
{
140-
field = new bool[0];
141-
}
142-
143-
field = new bool[n];
138+
field = n <= 0 ? new bool[0] : new bool[n];
144139
}
145140

146141
/// <summary>
@@ -328,40 +323,32 @@ public void Dispose()
328323
{
329324
var sequence1 = one.ToString();
330325
var sequence2 = two.ToString();
331-
var result = string.Empty;
332-
var tmp = string.Empty;
326+
var result = new StringBuilder();
333327

334328
// for scaling of same length.
335329
if (one.Length != two.Length)
336330
{
331+
var tmp = new StringBuilder();
337332
int difference;
338333
if (one.Length > two.Length)
339334
{
340335
// one is greater
341336
difference = one.Length - two.Length;
342337

343338
// fills up with 0's
344-
for (var i = 0; i < difference; i++)
345-
{
346-
tmp += '0';
347-
}
348-
349-
tmp += two.ToString();
350-
sequence2 = tmp;
339+
tmp.Append('0', difference);
340+
tmp.Append(two.ToString());
341+
sequence2 = tmp.ToString();
351342
}
352343
else
353344
{
354345
// two is greater
355346
difference = two.Length - one.Length;
356347

357348
// fills up with 0's
358-
for (var i = 0; i < difference; i++)
359-
{
360-
tmp += '0';
361-
}
362-
363-
tmp += one.ToString();
364-
sequence1 = tmp;
349+
tmp.Append('0', difference);
350+
tmp.Append(one.ToString());
351+
sequence1 = tmp.ToString();
365352
}
366353
} // end scaling
367354

@@ -370,11 +357,10 @@ public void Dispose()
370357

371358
for (var i = 0; i < len; i++)
372359
{
373-
result += sequence1[i].Equals('0') && sequence2[i].Equals('0') ? '0' : '1';
360+
result.Append(sequence1[i].Equals('0') && sequence2[i].Equals('0') ? '0' : '1');
374361
}
375362

376-
result = result.Trim();
377-
ans.Compile(result);
363+
ans.Compile(result.ToString());
378364

379365
return ans;
380366
}
@@ -389,22 +375,14 @@ public void Dispose()
389375
{
390376
var ans = new BitArray(one.Length);
391377
var sequence = one.ToString();
392-
var result = string.Empty;
378+
var result = new StringBuilder(sequence.Length);
393379

394380
foreach (var ch in sequence)
395381
{
396-
if (ch == '1')
397-
{
398-
result += '0';
399-
}
400-
else
401-
{
402-
result += '1';
403-
}
382+
result.Append(ch == '1' ? '0' : '1');
404383
}
405384

406-
result = result.Trim();
407-
ans.Compile(result);
385+
ans.Compile(result.ToString());
408386

409387
return ans;
410388
}
@@ -440,54 +418,45 @@ public void Dispose()
440418
{
441419
var sequence1 = one.ToString();
442420
var sequence2 = two.ToString();
443-
var tmp = string.Empty;
444421

445422
// for scaling of same length.
446423
if (one.Length != two.Length)
447424
{
425+
var tmp = new StringBuilder();
448426
int difference;
449427
if (one.Length > two.Length)
450428
{
451429
// one is greater
452430
difference = one.Length - two.Length;
453431

454432
// fills up with 0's
455-
for (var i = 0; i < difference; i++)
456-
{
457-
tmp += '0';
458-
}
459-
460-
tmp += two.ToString();
461-
sequence2 = tmp;
433+
tmp.Append('0', difference);
434+
tmp.Append(two.ToString());
435+
sequence2 = tmp.ToString();
462436
}
463437
else
464438
{
465439
// two is greater
466440
difference = two.Length - one.Length;
467441

468442
// fills up with 0's
469-
for (var i = 0; i < difference; i++)
470-
{
471-
tmp += '0';
472-
}
473-
474-
tmp += one.ToString();
475-
sequence1 = tmp;
443+
tmp.Append('0', difference);
444+
tmp.Append(one.ToString());
445+
sequence1 = tmp.ToString();
476446
}
477447
} // end scaling
478448

479449
var len = one.Length > two.Length ? one.Length : two.Length;
480450
var ans = new BitArray(len);
481451

482-
var sb = new StringBuilder();
452+
var sb = new StringBuilder(len);
483453

484454
for (var i = 0; i < len; i++)
485455
{
486-
_ = sb.Append(sequence1[i] == sequence2[i] ? '0' : '1');
456+
sb.Append(sequence1[i] == sequence2[i] ? '0' : '1');
487457
}
488458

489-
var result = sb.ToString().Trim();
490-
ans.Compile(result);
459+
ans.Compile(sb.ToString());
491460

492461
return ans;
493462
}
@@ -571,18 +540,10 @@ public void Compile(string sequence)
571540
ThrowIfSequenceIsInvalid(sequence);
572541

573542
// for appropriate scaling
574-
var tmp = string.Empty;
575543
if (sequence.Length < field.Length)
576544
{
577545
var difference = field.Length - sequence.Length;
578-
579-
for (var i = 0; i < difference; i++)
580-
{
581-
tmp += '0';
582-
}
583-
584-
tmp += sequence;
585-
sequence = tmp;
546+
sequence = new string('0', difference) + sequence;
586547
}
587548

588549
// actual compile procedure.
@@ -599,8 +560,6 @@ public void Compile(string sequence)
599560
/// <param name="number">A positive integer number.</param>
600561
public void Compile(int number)
601562
{
602-
var tmp = string.Empty;
603-
604563
// precondition I
605564
if (number <= 0)
606565
{
@@ -620,14 +579,7 @@ public void Compile(int number)
620579
if (binaryNumber.Length < field.Length)
621580
{
622581
var difference = field.Length - binaryNumber.Length;
623-
624-
for (var i = 0; i < difference; i++)
625-
{
626-
tmp += '0';
627-
}
628-
629-
tmp += binaryNumber;
630-
binaryNumber = tmp;
582+
binaryNumber = new string('0', difference) + binaryNumber;
631583
}
632584

633585
// actual compile procedure.
@@ -644,8 +596,6 @@ public void Compile(int number)
644596
/// <param name="number">A positive long integer number.</param>
645597
public void Compile(long number)
646598
{
647-
var tmp = string.Empty;
648-
649599
// precondition I
650600
if (number <= 0)
651601
{
@@ -665,14 +615,7 @@ public void Compile(long number)
665615
if (binaryNumber.Length < field.Length)
666616
{
667617
var difference = field.Length - binaryNumber.Length;
668-
669-
for (var i = 0; i < difference; i++)
670-
{
671-
tmp += '0';
672-
}
673-
674-
tmp += binaryNumber;
675-
binaryNumber = tmp;
618+
binaryNumber = new string('0', difference) + binaryNumber;
676619
}
677620

678621
// actual compile procedure.
@@ -828,7 +771,7 @@ private static void ThrowIfSequenceIsInvalid(string sequence)
828771
}
829772

830773
/// <summary>
831-
/// Utility method foir checking a given sequence contains only zeros and ones.
774+
/// Utility method for checking a given sequence contains only zeros and ones.
832775
/// This method will used in Constructor (sequence : string) and Compile(sequence : string).
833776
/// </summary>
834777
/// <param name="sequence">String sequence.</param>

0 commit comments

Comments
 (0)