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
62 changes: 31 additions & 31 deletions Algorithms/Encoders/SoundexEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@ namespace Algorithms.Encoders;
/// </summary>
public class SoundexEncoder
{
private static readonly Dictionary<char, int> CharacterMapping = new()
{
['a'] = 0,
['e'] = 0,
['i'] = 0,
['o'] = 0,
['u'] = 0,
['y'] = 0,
['h'] = 8,
['w'] = 8,
['b'] = 1,
['f'] = 1,
['p'] = 1,
['v'] = 1,
['c'] = 2,
['g'] = 2,
['j'] = 2,
['k'] = 2,
['q'] = 2,
['s'] = 2,
['x'] = 2,
['z'] = 2,
['d'] = 3,
['t'] = 3,
['l'] = 4,
['m'] = 5,
['n'] = 5,
['r'] = 6,
};

/// <summary>
/// Encodes a string using the Soundex Algorithm.
/// </summary>
Expand Down Expand Up @@ -72,36 +102,6 @@ private IEnumerable<int> CollapseDoubles(IEnumerable<int> numbers)

private int MapToNumber(char ch)
{
var mapping = new Dictionary<char, int>
{
['a'] = 0,
['e'] = 0,
['i'] = 0,
['o'] = 0,
['u'] = 0,
['y'] = 0,
['h'] = 8,
['w'] = 8,
['b'] = 1,
['f'] = 1,
['p'] = 1,
['v'] = 1,
['c'] = 2,
['g'] = 2,
['j'] = 2,
['k'] = 2,
['q'] = 2,
['s'] = 2,
['x'] = 2,
['z'] = 2,
['d'] = 3,
['t'] = 3,
['l'] = 4,
['m'] = 5,
['n'] = 5,
['r'] = 6,
};

return mapping[ch];
return CharacterMapping[ch];
}
}
38 changes: 19 additions & 19 deletions Algorithms/Other/Luhn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,49 @@ public static class Luhn
public static bool Validate(string number) => GetSum(number) % 10 == 0;

/// <summary>
/// This algorithm only finds one number.
/// This algorithm finds one missing digit.
/// In place of the unknown digit, put "x".
/// </summary>
/// <param name="number">The number in which to find the missing digit.</param>
/// <returns>Missing digit.</returns>
public static int GetLostNum(string number)
{
var lostIndex = number.Length - 1 - number.LastIndexOf("x", StringComparison.CurrentCultureIgnoreCase);
var lostNum = GetSum(number.Replace("x", "0", StringComparison.CurrentCultureIgnoreCase)) * 9 % 10;
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 lost digit is even.
if (lostIndex % 2 == 0)
// Case 1: If the index of the missing digit is even.
if (missingDigitIndex % 2 == 0)
{
return lostNum;
return checkDigit;
}

var tempLostNum = lostNum / 2;
var candidateDigit = checkDigit / 2;

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

/// <summary>
/// Computes the sum found by the algorithm.
/// Computes the sum found by the Luhn algorithm.
/// </summary>
/// <param name="number">The number for which the sum will be found.</param>
/// <param name="number">The number for which the sum will be calculated.</param>
/// <returns>Sum.</returns>
private static int GetSum(string number)
{
var sum = 0;
for (var i = 0; i < number.Length; i++)
{
var d = number[i] - '0';
d = (i + number.Length) % 2 == 0
? 2 * d
: d;
if (d > 9)
var digit = number[i] - '0';
digit = (i + number.Length) % 2 == 0
? 2 * digit
: digit;
if (digit > 9)
{
d -= 9;
digit -= 9;
}

sum += d;
sum += digit;
}

return sum;
Expand Down
113 changes: 28 additions & 85 deletions DataStructures/BitArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,7 @@ public sealed class BitArray : ICloneable, IEnumerator<bool>, IEnumerable<bool>
/// <param name="n">length of the array.</param>
public BitArray(int n)
{
if (n < 1)
{
field = new bool[0];
}

field = new bool[n];
field = n <= 0 ? new bool[0] : new bool[n];
}

/// <summary>
Expand Down Expand Up @@ -332,40 +327,32 @@ public void Dispose()
{
var sequence1 = one.ToString();
var sequence2 = two.ToString();
var result = string.Empty;
var tmp = string.Empty;
var result = new StringBuilder();

// for scaling of same length.
if (one.Length != two.Length)
{
var tmp = new StringBuilder();
int difference;
if (one.Length > two.Length)
{
// one is greater
difference = one.Length - two.Length;

// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += two.ToString();
sequence2 = tmp;
tmp.Append('0', difference);
tmp.Append(two.ToString());
sequence2 = tmp.ToString();
}
else
{
// two is greater
difference = two.Length - one.Length;

// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += one.ToString();
sequence1 = tmp;
tmp.Append('0', difference);
tmp.Append(one.ToString());
sequence1 = tmp.ToString();
}
} // end scaling

Expand All @@ -374,11 +361,10 @@ public void Dispose()

for (var i = 0; i < len; i++)
{
result += sequence1[i].Equals('0') && sequence2[i].Equals('0') ? '0' : '1';
result.Append(sequence1[i].Equals('0') && sequence2[i].Equals('0') ? '0' : '1');
}

result = result.Trim();
ans.Compile(result);
ans.Compile(result.ToString());

return ans;
}
Expand All @@ -393,22 +379,14 @@ public void Dispose()
{
var ans = new BitArray(one.Length);
var sequence = one.ToString();
var result = string.Empty;
var result = new StringBuilder(sequence.Length);

foreach (var ch in sequence)
{
if (ch == '1')
{
result += '0';
}
else
{
result += '1';
}
result.Append(ch == '1' ? '0' : '1');
}

result = result.Trim();
ans.Compile(result);
ans.Compile(result.ToString());

return ans;
}
Expand Down Expand Up @@ -444,54 +422,45 @@ public void Dispose()
{
var sequence1 = one.ToString();
var sequence2 = two.ToString();
var tmp = string.Empty;

// for scaling of same length.
if (one.Length != two.Length)
{
var tmp = new StringBuilder();
int difference;
if (one.Length > two.Length)
{
// one is greater
difference = one.Length - two.Length;

// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += two.ToString();
sequence2 = tmp;
tmp.Append('0', difference);
tmp.Append(two.ToString());
sequence2 = tmp.ToString();
}
else
{
// two is greater
difference = two.Length - one.Length;

// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += one.ToString();
sequence1 = tmp;
tmp.Append('0', difference);
tmp.Append(one.ToString());
sequence1 = tmp.ToString();
}
} // end scaling

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

var sb = new StringBuilder();
var sb = new StringBuilder(len);

for (var i = 0; i < len; i++)
{
_ = sb.Append(sequence1[i] == sequence2[i] ? '0' : '1');
sb.Append(sequence1[i] == sequence2[i] ? '0' : '1');
}

var result = sb.ToString().Trim();
ans.Compile(result);
ans.Compile(sb.ToString());

return ans;
}
Expand Down Expand Up @@ -575,18 +544,10 @@ public void Compile(string sequence)
ThrowIfSequenceIsInvalid(sequence);

// for appropriate scaling
var tmp = string.Empty;
if (sequence.Length < field.Length)
{
var difference = field.Length - sequence.Length;

for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += sequence;
sequence = tmp;
sequence = new string('0', difference) + sequence;
}

// actual compile procedure.
Expand All @@ -603,8 +564,6 @@ public void Compile(string sequence)
/// <param name="number">A positive integer number.</param>
public void Compile(int number)
{
var tmp = string.Empty;

// precondition I
if (number <= 0)
{
Expand All @@ -624,14 +583,7 @@ public void Compile(int number)
if (binaryNumber.Length < field.Length)
{
var difference = field.Length - binaryNumber.Length;

for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += binaryNumber;
binaryNumber = tmp;
binaryNumber = new string('0', difference) + binaryNumber;
}

// actual compile procedure.
Expand All @@ -648,8 +600,6 @@ public void Compile(int number)
/// <param name="number">A positive long integer number.</param>
public void Compile(long number)
{
var tmp = string.Empty;

// precondition I
if (number <= 0)
{
Expand All @@ -669,14 +619,7 @@ public void Compile(long number)
if (binaryNumber.Length < field.Length)
{
var difference = field.Length - binaryNumber.Length;

for (var i = 0; i < difference; i++)
{
tmp += '0';
}

tmp += binaryNumber;
binaryNumber = tmp;
binaryNumber = new string('0', difference) + binaryNumber;
}

// actual compile procedure.
Expand Down Expand Up @@ -832,7 +775,7 @@ private static void ThrowIfSequenceIsInvalid(string sequence)
}

/// <summary>
/// Utility method foir checking a given sequence contains only zeros and ones.
/// Utility method for checking a given sequence contains only zeros and ones.
/// This method will used in Constructor (sequence : string) and Compile(sequence : string).
/// </summary>
/// <param name="sequence">String sequence.</param>
Expand Down
Loading