Skip to content

Commit 618ba94

Browse files
authored
Fix codacy (#56)
* Refactor code * Simplify FillGaps in HillEncoder
1 parent e6d3d91 commit 618ba94

30 files changed

+112
-146
lines changed

Algorithms.Tests/Encoders/CaesarEncoderTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using System;
2-
using Algorithms.Encoders;
1+
using Algorithms.Encoders;
32
using NUnit.Framework;
43
using NUnit.Framework.Internal;
4+
using System;
55

66
namespace Algorithms.Tests.Encoders
77
{
88
public class CaesarEncoderTests
99
{
10-
readonly Randomizer random = new Randomizer();
11-
readonly CaesarEncoder encoder = new CaesarEncoder();
12-
string RandomMessage => random.GetString();
10+
private readonly Randomizer random = new Randomizer();
11+
private readonly CaesarEncoder encoder = new CaesarEncoder();
12+
13+
private string RandomMessage => random.GetString();
1314

1415
[Test]
1516
[Parallelizable]

Algorithms.Tests/Encoders/VigenereEncoderTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ namespace Algorithms.Tests.Encoders
66
{
77
public class VigenereEncoderTests
88
{
9-
readonly Randomizer random = new Randomizer();
10-
readonly VigenereEncoder encoder = new VigenereEncoder();
11-
string RandomMessage => random.GetString();
9+
private readonly Randomizer random = new Randomizer();
10+
private readonly VigenereEncoder encoder = new VigenereEncoder();
11+
12+
private string RandomMessage => random.GetString();
1213

1314
[Test]
1415
[Parallelizable]

Algorithms.Tests/Sorters/BubbleSorterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
1+
using Algorithms.Sorters;
22
using NUnit.Framework;
33
using NUnit.Framework.Internal;
4-
using Algorithms.Sorters;
4+
using System;
55
using System.Collections.Generic;
66

77
namespace Algorithms.Tests.Sorters
88
{
99
public class BubbleSorterTests
1010
{
11-
readonly BubbleSorter<int> sorter = new BubbleSorter<int>();
12-
readonly Random random = new Random();
11+
private readonly BubbleSorter<int> sorter = new BubbleSorter<int>();
12+
private readonly Random random = new Random();
1313

1414
[Test]
1515
[Parallelizable]

Algorithms.Tests/Sorters/SelectionSorterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System;
1+
using Algorithms.Sorters;
22
using NUnit.Framework;
33
using NUnit.Framework.Internal;
4-
using Algorithms.Sorters;
4+
using System;
55
using System.Collections.Generic;
66

77
namespace Algorithms.Tests.Sorters
88
{
99
public class SelectionSorterTests
1010
{
11-
readonly SelectionSorter<int> sorter = new SelectionSorter<int>();
12-
readonly Random random = new Random();
11+
private readonly SelectionSorter<int> sorter = new SelectionSorter<int>();
12+
private readonly Random random = new Random();
1313

1414
[Test]
1515
[Parallelizable]

Algorithms/DataCompression/huffman.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace DC5
66
{
7-
class Huff
7+
internal class Huff
88
{
99
public string Data { get; set; }
1010
public int Frequency { get; set; }
@@ -24,14 +24,16 @@ public Huff(Huff leftChild, Huff rightChild)
2424
Frequency = leftChild.Frequency + rightChild.Frequency;
2525
}
2626
}
27-
class Man
27+
28+
internal class Man
2829
{
2930
public List<string> Codec { get; set; } = new List<string>();
3031
public List<string> Data { get; set; } = new List<string>();
3132
}
32-
class Program
33+
34+
internal class Program
3335
{
34-
static void Main()
36+
private static void Main()
3537
{
3638
IList<Huff> list = new List<Huff>();
3739
Console.Write("Enter String: ");

Algorithms/DataCompression/shannon_fano.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace DC4
55
{
6-
class Program
6+
internal class Program
77
{
8-
class Fano
8+
private class Fano
99
{
1010
public float pro;
1111
public int[] arr = new int[20];
1212
public int top;
1313
}
14-
static void Main()
14+
15+
private static void Main()
1516
{
1617
var f = Enumerable.Range(0, 20).Select(ind => new Fano()).ToArray();
1718
Console.Write("Enter String: ");
@@ -133,7 +134,8 @@ static void Main()
133134
Console.WriteLine(cStr.Replace(" ", ""));
134135
Console.ReadKey();
135136
}
136-
static void Shannon(int l, int h, Fano[] f)
137+
138+
private static void Shannon(int l, int h, Fano[] f)
137139
{
138140
float set1 = 0, set2 = 0;
139141
int i, j, k = 0;

Algorithms/Encoders/CaesarEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class CaesarEncoder : IEncoder<int>
2929
/// <returns>Decoded text</returns>
3030
public string Decode(string text, int key) => Cipher(text, -key);
3131

32-
private string Cipher(string text, int key)
32+
private static string Cipher(string text, int key)
3333
{
3434
var newText = new StringBuilder(text.Length);
3535
for (var i = 0; i < text.Length; i++)

Algorithms/Encoders/HillEncoder.cs

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Algorithms.Numeric;
22
using System;
3-
using System.Text;
3+
using System.Linq;
44

55
namespace Algorithms.Encoders
66
{
@@ -11,33 +11,21 @@ namespace Algorithms.Encoders
1111
/// </summary>
1212
public class HillEncoder : IEncoder<double[,]>
1313
{
14-
#region DEFINITION
15-
1614
private readonly GaussJordanElimination LinearEquationSolver;
1715

18-
public HillEncoder()
19-
{
20-
LinearEquationSolver = new GaussJordanElimination();
21-
}
22-
23-
public string Decode(string text, double[,] key) => Decipher(text, key);
24-
25-
public string Encode(string text, double[,] key) => Cipher(text, key);
26-
27-
#endregion DEFINITION
16+
public HillEncoder() => LinearEquationSolver = new GaussJordanElimination(); //TODO: add DI
2817

29-
#region MAIN
30-
private string Cipher(string text, double[,] key)
18+
public string Encode(string text, double[,] key)
3119
{
3220
var preparedText = FillGaps(text);
3321
var chunked = ChunkTextToArray(preparedText);
3422
var splitted = SplitToCharArray(chunked);
3523

36-
double[][] ciphered = new double[chunked.Length][];
24+
var ciphered = new double[chunked.Length][];
3725

3826
for (var i = 0; i < chunked.Length; i++)
3927
{
40-
double[] vector = new double[3];
28+
var vector = new double[3];
4129
Array.Copy(splitted, i * 3, vector, 0, 3);
4230
var product = MatrixCipher(vector, key);
4331
ciphered[i] = product;
@@ -48,16 +36,16 @@ private string Cipher(string text, double[,] key)
4836
return BuildStringFromArray(merged);
4937
}
5038

51-
private string Decipher(string text, double[,] key)
39+
public string Decode(string text, double[,] key)
5240
{
5341
var chunked = ChunkTextToArray(text);
5442
var splitted = SplitToCharArray(chunked);
5543

56-
double[][] deciphered = new double[chunked.Length][];
44+
var deciphered = new double[chunked.Length][];
5745

5846
for (var i = 0; i < chunked.Length; i++)
5947
{
60-
double[] vector = new double[3];
48+
var vector = new double[3];
6149
Array.Copy(splitted, i * 3, vector, 0, 3);
6250
var product = MatrixDeCipher(vector, key);
6351
deciphered[i] = product;
@@ -68,25 +56,13 @@ private string Decipher(string text, double[,] key)
6856

6957
return UnFillGaps(str);
7058
}
71-
#endregion MAIN
7259

7360
/// <summary>
74-
/// To convert elements from the array to their corresponding char ASCII.
61+
/// Converts elements from the array to their corresponding Unicode characters.
7562
/// </summary>
7663
/// <param name="arr">array of vectors</param>
7764
/// <returns>Message</returns>
78-
private static string BuildStringFromArray(double[] arr)
79-
{
80-
var strBuilder = new StringBuilder();
81-
82-
for (var i = 0; i < arr.Length; i++)
83-
{
84-
// To cast it to its corresponding (char)value.
85-
strBuilder.Append((char)arr[i]);
86-
}
87-
88-
return strBuilder.ToString();
89-
}
65+
private static string BuildStringFromArray(double[] arr) => new string(arr.Select(c => (char)c).ToArray());
9066

9167
/// <summary>
9268
/// Given a list of vectors, returns a single array of elements.
@@ -106,7 +82,7 @@ private double[] MergeArrayList(double[][] list)
10682
}
10783

10884
/// <summary>
109-
/// To multiply the key for the given scalar.
85+
/// Multiplies the key for the given scalar.
11086
/// </summary>
11187
/// <param name="vector">list of splitted words as numbers.</param>
11288
/// <param name="key">Cipher selected key</param>
@@ -127,17 +103,17 @@ private static double[] MatrixCipher(double[] vector, double[,] key)
127103
}
128104

129105
/// <summary>
130-
/// To split the input text message as chunks of words.
106+
/// Splits the input text message as chunks of words.
131107
/// </summary>
132108
/// <param name="chunked">chunked words list</param>
133109
/// <returns>spliiter char array.</returns>
134110
private static char[] SplitToCharArray(string[] chunked)
135111
{
136-
char[] splitted = new char[chunked.Length * 3];
112+
var splitted = new char[chunked.Length * 3];
137113

138-
for (int i = 0; i < chunked.Length; i++)
114+
for (var i = 0; i < chunked.Length; i++)
139115
{
140-
for (int j = 0; j < 3; j++)
116+
for (var j = 0; j < 3; j++)
141117
{
142118
splitted[(i * 3) + j] = chunked[i].ToCharArray()[j];
143119
}
@@ -147,7 +123,7 @@ private static char[] SplitToCharArray(string[] chunked)
147123
}
148124

149125
/// <summary>
150-
/// To chunk the input text message.
126+
/// Chunks the input text message.
151127
/// </summary>
152128
/// <param name="text">text message</param>
153129
/// <returns>array of words.</returns>
@@ -173,33 +149,16 @@ private static string[] ChunkTextToArray(string text)
173149
/// <returns>Modified text Message.</returns>
174150
private static string FillGaps(string text)
175151
{
176-
string newText = text;
177-
var isChunkable = false;
178-
179-
while (!isChunkable)
180-
{
181-
if (newText.Length % 3 != 0)
182-
{
183-
newText += " ";
184-
}
185-
else
186-
{
187-
isChunkable = true;
188-
}
189-
}
190-
191-
return newText;
152+
var remainder = text.Length % 3;
153+
return remainder == 0 ? text : text + new string(' ', 3 - remainder);
192154
}
193155

194156
/// <summary>
195157
/// Removes the extra spaces included on the cipher phase.
196158
/// </summary>
197159
/// <param name="text">Text message</param>
198160
/// <returns>Deciphered Message</returns>
199-
private static string UnFillGaps(string text)
200-
{
201-
return text.TrimEnd();
202-
}
161+
private static string UnFillGaps(string text) => text.TrimEnd();
203162

204163
/// <summary>
205164
/// Finds the inverse of the given matrix using a linear equation solver.
@@ -214,13 +173,13 @@ private double[] MatrixDeCipher(double[] vector, double[,] key)
214173

215174
for (var i = 0; i < key.GetLength(0); i++)
216175
{
217-
for (int j = 0; j < key.GetLength(1); j++)
176+
for (var j = 0; j < key.GetLength(1); j++)
218177
{
219178
augM[i, j] = key[i, j];
220179
}
221180
}
222181

223-
for (int k = 0; k < vector.Length; k++)
182+
for (var k = 0; k < vector.Length; k++)
224183
{
225184
augM[k, 3] = vector[k];
226185
}

Algorithms/Encoders/VigenereEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Algorithms.Encoders
88
/// </summary>
99
public class VigenereEncoder : IEncoder<string>
1010
{
11-
readonly CaesarEncoder caesarEncoder = new CaesarEncoder();
11+
private readonly CaesarEncoder caesarEncoder = new CaesarEncoder();
1212

1313
/// <summary>
1414
/// Encodes text using specified key,

Algorithms/Numeric/GaussJordanElimination.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private bool CanMatrixBeUsed(double[,] matrix)
5555
/// <returns>Matrix</returns>
5656
private bool PivotMatrix(ref double[,] matrix)
5757
{
58-
for (int col = 0; col + 1 < RowCount; col++)
58+
for (var col = 0; col + 1 < RowCount; col++)
5959
{
6060
if (matrix[col, col] == 0)
6161
{
@@ -65,7 +65,7 @@ private bool PivotMatrix(ref double[,] matrix)
6565
if (matrix[rowToSwap, col] != 0)
6666
{
6767
var tmp = new double[RowCount + 1];
68-
for (int i = 0; i < RowCount + 1; i++)
68+
for (var i = 0; i < RowCount + 1; i++)
6969
{
7070
// To make the swap with the element above.
7171
tmp[i] = matrix[rowToSwap, i];
@@ -105,14 +105,14 @@ private int FindNonZeroCoefficient(ref double[,] matrix, int col)
105105
/// <param name="matrix">Matrix</param>
106106
private void Elimination(ref double[,] matrix)
107107
{
108-
for (int srcRow = 0; srcRow + 1 < RowCount; srcRow++)
108+
for (var srcRow = 0; srcRow + 1 < RowCount; srcRow++)
109109
{
110-
for (int destRow = srcRow + 1; destRow < RowCount; destRow++)
110+
for (var destRow = srcRow + 1; destRow < RowCount; destRow++)
111111
{
112-
double df = matrix[srcRow, srcRow];
113-
double sf = matrix[destRow, srcRow];
112+
var df = matrix[srcRow, srcRow];
113+
var sf = matrix[destRow, srcRow];
114114

115-
for (int i = 0; i < RowCount + 1; i++)
115+
for (var i = 0; i < RowCount + 1; i++)
116116
{
117117
matrix[destRow, i] = matrix[destRow, i] * df - matrix[srcRow, i] * sf;
118118
}
@@ -135,12 +135,12 @@ private bool ElementaryReduction(ref double[,] matrix)
135135
return false;
136136
}
137137

138-
for (int i = 0; i < RowCount + 1; i++)
138+
for (var i = 0; i < RowCount + 1; i++)
139139
{
140140
matrix[row, i] /= element;
141141
}
142142

143-
for (int destRow = 0; destRow < row; destRow++)
143+
for (var destRow = 0; destRow < row; destRow++)
144144
{
145145
matrix[destRow, RowCount] -= matrix[destRow, row] * matrix[row, RowCount];
146146
matrix[destRow, row] = 0;

0 commit comments

Comments
 (0)