From 491e355fc77a17194adfff1bf3326c3d91a7b91f Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Thu, 18 Sep 2025 07:31:56 +0000
Subject: [PATCH 1/4] Initial plan
From 365030425446076e74a3d6637afcbbf4a4f9c432 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Thu, 18 Sep 2025 07:40:15 +0000
Subject: [PATCH 2/4] Improve code performance and maintainability
Co-authored-by: michaelkrisper <733482+michaelkrisper@users.noreply.github.com>
---
Algorithms/Encoders/NysiisEncoder.cs | 3 +
Algorithms/Encoders/SoundexEncoder.cs | 62 ++++++-------
Algorithms/Other/Luhn.cs | 38 ++++----
DataStructures/BitArray.cs | 113 ++++++-----------------
Utilities/Extensions/VectorExtensions.cs | 4 +-
5 files changed, 82 insertions(+), 138 deletions(-)
diff --git a/Algorithms/Encoders/NysiisEncoder.cs b/Algorithms/Encoders/NysiisEncoder.cs
index 503c8585..5e940c3f 100644
--- a/Algorithms/Encoders/NysiisEncoder.cs
+++ b/Algorithms/Encoders/NysiisEncoder.cs
@@ -1,3 +1,6 @@
+using System.Globalization;
+using System.Text;
+
namespace Algorithms.Encoders;
///
diff --git a/Algorithms/Encoders/SoundexEncoder.cs b/Algorithms/Encoders/SoundexEncoder.cs
index ce0d51e1..9554067d 100644
--- a/Algorithms/Encoders/SoundexEncoder.cs
+++ b/Algorithms/Encoders/SoundexEncoder.cs
@@ -5,6 +5,36 @@ namespace Algorithms.Encoders;
///
public class SoundexEncoder
{
+ private static readonly Dictionary 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,
+ };
+
///
/// Encodes a string using the Soundex Algorithm.
///
@@ -72,36 +102,6 @@ private IEnumerable CollapseDoubles(IEnumerable numbers)
private int MapToNumber(char ch)
{
- var mapping = new Dictionary
- {
- ['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];
}
}
diff --git a/Algorithms/Other/Luhn.cs b/Algorithms/Other/Luhn.cs
index b6d873c9..58d652b9 100644
--- a/Algorithms/Other/Luhn.cs
+++ b/Algorithms/Other/Luhn.cs
@@ -21,49 +21,49 @@ public static class Luhn
public static bool Validate(string number) => GetSum(number) % 10 == 0;
///
- /// This algorithm only finds one number.
+ /// This algorithm finds one missing digit.
/// In place of the unknown digit, put "x".
///
/// The number in which to find the missing digit.
/// Missing digit.
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;
}
///
- /// Computes the sum found by the algorithm.
+ /// Computes the sum found by the Luhn algorithm.
///
- /// The number for which the sum will be found.
+ /// The number for which the sum will be calculated.
/// Sum.
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;
diff --git a/DataStructures/BitArray.cs b/DataStructures/BitArray.cs
index 6b5baeb2..6d0feab1 100644
--- a/DataStructures/BitArray.cs
+++ b/DataStructures/BitArray.cs
@@ -139,12 +139,7 @@ public sealed class BitArray : ICloneable, IEnumerator, IEnumerable
/// length of the array.
public BitArray(int n)
{
- if (n < 1)
- {
- field = new bool[0];
- }
-
- field = new bool[n];
+ field = n < 1 ? new bool[0] : new bool[n];
}
///
@@ -332,12 +327,12 @@ 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)
{
@@ -345,13 +340,9 @@ public void Dispose()
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
{
@@ -359,13 +350,9 @@ public void Dispose()
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
@@ -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;
}
@@ -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;
}
@@ -444,11 +422,11 @@ 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)
{
@@ -456,13 +434,9 @@ public void Dispose()
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
{
@@ -470,28 +444,23 @@ public void Dispose()
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;
}
@@ -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.
@@ -603,8 +564,6 @@ public void Compile(string sequence)
/// A positive integer number.
public void Compile(int number)
{
- var tmp = string.Empty;
-
// precondition I
if (number <= 0)
{
@@ -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.
@@ -648,8 +600,6 @@ public void Compile(int number)
/// A positive long integer number.
public void Compile(long number)
{
- var tmp = string.Empty;
-
// precondition I
if (number <= 0)
{
@@ -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.
@@ -832,7 +775,7 @@ private static void ThrowIfSequenceIsInvalid(string sequence)
}
///
- /// 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).
///
/// String sequence.
diff --git a/Utilities/Extensions/VectorExtensions.cs b/Utilities/Extensions/VectorExtensions.cs
index 7241bb17..6d34b846 100644
--- a/Utilities/Extensions/VectorExtensions.cs
+++ b/Utilities/Extensions/VectorExtensions.cs
@@ -68,9 +68,7 @@ public static double Dot(this double[] lhs, double[] rhs)
/// The magnitude.
public static double Magnitude(this double[] vector)
{
- var magnitude = Dot(vector, vector);
- magnitude = Math.Sqrt(magnitude);
- return magnitude;
+ return Math.Sqrt(Dot(vector, vector));
}
///
From fde77a8341ee919b6aa513f180a61a3f886cf11c Mon Sep 17 00:00:00 2001
From: Michael Krisper
Date: Thu, 18 Sep 2025 10:19:09 +0200
Subject: [PATCH 3/4] Apply suggestion from @Copilot
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---
DataStructures/BitArray.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/DataStructures/BitArray.cs b/DataStructures/BitArray.cs
index 6d0feab1..3eaa30e1 100644
--- a/DataStructures/BitArray.cs
+++ b/DataStructures/BitArray.cs
@@ -139,7 +139,7 @@ public sealed class BitArray : ICloneable, IEnumerator, IEnumerable
/// length of the array.
public BitArray(int n)
{
- field = n < 1 ? new bool[0] : new bool[n];
+ field = n <= 0 ? new bool[0] : new bool[n];
}
///
From 4b3da4a57427c9a2b7734ad4bca02a99790a36c6 Mon Sep 17 00:00:00 2001
From: Michael Krisper
Date: Thu, 18 Sep 2025 16:49:09 +0200
Subject: [PATCH 4/4] Clean up unused using directives in NysiisEncoder
---
Algorithms/Encoders/NysiisEncoder.cs | 3 ---
1 file changed, 3 deletions(-)
diff --git a/Algorithms/Encoders/NysiisEncoder.cs b/Algorithms/Encoders/NysiisEncoder.cs
index 5e940c3f..503c8585 100644
--- a/Algorithms/Encoders/NysiisEncoder.cs
+++ b/Algorithms/Encoders/NysiisEncoder.cs
@@ -1,6 +1,3 @@
-using System.Globalization;
-using System.Text;
-
namespace Algorithms.Encoders;
///