Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 20b89bb

Browse files
~Ran main project against ReSharper.
1 parent 656767d commit 20b89bb

File tree

8 files changed

+104
-173
lines changed

8 files changed

+104
-173
lines changed

net-solution/PalindromePartitions.Tests/Common/InvalidCalls.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public static TryCatchResult PartitionMerge(Partition entryObj, int inpStart, in
106106
// Check if correct error caught.
107107
public static void CheckErrorResult(TryCatchResult resObj, string expectedError)
108108
{
109-
Assert.False(resObj.successful);
110-
Assert.AreEqual(resObj.errorMessage, expectedError);
109+
Assert.False(resObj.Successful);
110+
Assert.AreEqual(resObj.ErrorMessage, expectedError);
111111
}
112112
}
113113
}

net-solution/PalindromePartitions.Tests/TryCatchSchema.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public void FunctionExists()
1616
Type boolType = typeof(bool);
1717
Type strType = typeof(string);
1818

19-
SchemaValidation.CheckProperty(classObject, "successful", boolType);
20-
SchemaValidation.CheckProperty(classObject, "errorMessage", strType);
19+
SchemaValidation.CheckProperty(classObject, "Successful", boolType);
20+
SchemaValidation.CheckProperty(classObject, "ErrorMessage", strType);
2121
}
2222

2323
}

net-solution/PalindromePartitions/Classes/Partition.cs

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,33 @@ namespace PalindromePartitions.Classes
88
public class Partition
99
{
1010
// Ignore substrings that contain invalid characters when parsing.
11-
private static Regex invalidChars = new Regex("[^A-Z0-9]+", RegexOptions.IgnoreCase);
11+
private static readonly Regex InvalidChars = new Regex("[^A-Z0-9]+", RegexOptions.IgnoreCase);
1212

1313
// Wrap string list.
14-
private List<string> substrings = new List<string>();
14+
private readonly List<string> _substrings;
1515

1616

1717
// Base constructor.
1818
private Partition(List<string> listVal)
1919
{
20-
substrings = listVal;
20+
_substrings = listVal;
2121
}
2222

2323

2424
// Constructor - Partition from input string.
2525
public static Partition Initialize(ref string fullStr)
2626
{
27-
int loopIndex = 0;
28-
string currentChar = "";
29-
bool currentSkip = false;
30-
List<string> charList = new List<string>();
27+
List<string> charList = new List<string>();
3128

3229
// Loop characters in string.
33-
for (loopIndex = 0; loopIndex < fullStr.Length; loopIndex = loopIndex + 1)
30+
for (int loopIndex = 0; loopIndex < fullStr.Length; loopIndex++)
3431
{
35-
currentChar = fullStr[loopIndex].ToString();
36-
currentSkip = invalidChars.IsMatch(currentChar);
32+
string currentChar = fullStr[loopIndex].ToString();
33+
bool currentSkip = InvalidChars.IsMatch(currentChar);
3734

38-
if (currentSkip != true)
39-
{
40-
charList.Add(currentChar);
41-
}
42-
43-
}
35+
if (currentSkip != true) charList.Add(currentChar);
36+
37+
}
4438

4539
Partition objectRes = new Partition(charList);
4640
return objectRes;
@@ -59,30 +53,19 @@ public static Partition Empty()
5953
// Constructor - Parsed from comma-separated string.
6054
public static Partition Parse(string fullText)
6155
{
62-
string[] splitArray = new string[0];
63-
64-
int subIndex = 0;
65-
string currentSubstring = "";
66-
bool badFound = false;
67-
68-
List<string> listObj = new List<string>();
69-
70-
// Split by comma.
71-
splitArray = fullText.Split(",", StringSplitOptions.RemoveEmptyEntries);
72-
73-
56+
string[] splitArray = fullText.Split(",", StringSplitOptions.RemoveEmptyEntries);
57+
List<string> listObj = new List<string>();
58+
59+
7460
// Loop substrings.
75-
for (subIndex = 0; subIndex < splitArray.Length; subIndex = subIndex + 1)
61+
for (int subIndex = 0; subIndex < splitArray.Length; subIndex++)
7662
{
7763
// Read substring and validate characters.
78-
currentSubstring = splitArray[subIndex];
79-
badFound = invalidChars.IsMatch(currentSubstring);
64+
string currentSubstring = splitArray[subIndex];
65+
bool badFound = InvalidChars.IsMatch(currentSubstring);
8066

8167
// Add string if valid.
82-
if (currentSubstring.Length > 0 && badFound != true)
83-
{
84-
listObj.Add(currentSubstring);
85-
}
68+
if (currentSubstring.Length > 0 && !badFound) listObj.Add(currentSubstring);
8669
}
8770

8871
Partition objectRes = new Partition(listObj);
@@ -114,13 +97,8 @@ public static Partition Derive(Partition origObj, string foundPalindrome, int me
11497
public List<string> Slice(int sliceStart, int sliceLength)
11598
{
11699
List<string> sliceRes = new List<string>();
117-
118-
if (sliceLength > 0)
119-
{
120-
sliceRes = substrings.GetRange(sliceStart, sliceLength);
121-
}
122-
123-
return sliceRes;
100+
if (sliceLength > 0) sliceRes = _substrings.GetRange(sliceStart, sliceLength);
101+
return sliceRes;
124102
}
125103

126104

@@ -129,51 +107,37 @@ public string MergeSubstrings(int sIndex, int eIndex)
129107
{
130108
int prepStart = sIndex;
131109
int prepEnd = eIndex;
132-
int itemCount = -1;
133-
List<string> chosenElements = new List<string>();
134-
string mergeRes = "";
135-
136-
// Swap range index numbers if need be.
110+
111+
// Swap range index numbers if need be.
137112
if (sIndex > eIndex || eIndex < sIndex)
138113
{
139114
prepStart = eIndex;
140115
prepEnd = sIndex;
141116
}
142117

143118
// Select strings and merge together.
144-
itemCount = (prepEnd - prepStart) + 1;
145-
chosenElements = substrings.GetRange(prepStart, itemCount);
146-
mergeRes = String.Join("", chosenElements);
147-
148-
return mergeRes;
149-
}
119+
int itemCount = (prepEnd - prepStart) + 1;
120+
List<string> chosenElements = _substrings.GetRange(prepStart, itemCount);
121+
return String.Join("", chosenElements);
122+
}
150123

151124

152125
// Write full partition string.
153126
public string Join()
154127
{
155-
string joinRes = String.Join(",", substrings);
156-
return joinRes;
157-
}
128+
return String.Join(",", _substrings);
129+
}
158130

159131
// Read particular substring.
160132
public string GetSubstring(int tgtIndex)
161133
{
162134
string subRes = "";
163-
164-
if (tgtIndex >= 0 && tgtIndex < substrings.Count)
165-
{
166-
subRes = substrings[tgtIndex];
167-
}
168-
169-
return subRes;
135+
if (tgtIndex >= 0 && tgtIndex < _substrings.Count) subRes = _substrings[tgtIndex];
136+
return subRes;
170137
}
171138

172139

173140
// Substring count property.
174-
public int Count
175-
{
176-
get {return substrings.Count;}
177-
}
178-
}
141+
public int Count => _substrings.Count;
142+
}
179143
}

net-solution/PalindromePartitions/Classes/TryCatchResult.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@ namespace PalindromePartitions.Classes
44
public class TryCatchResult
55
{
66
// Private properties.
7-
private bool pSuccessful = false;
8-
private string pErrorMessage = "";
7+
private bool _successful;
8+
private string _errorMessage;
99

1010

1111
// Constructor.
1212
public TryCatchResult(bool successVal, string msgVal)
1313
{
14-
pSuccessful = successVal;
15-
pErrorMessage = msgVal;
14+
_successful = successVal;
15+
_errorMessage = msgVal;
1616
}
1717

18-
// Successful.
19-
public bool successful
20-
{
21-
get {return pSuccessful;}
22-
}
23-
24-
// Error message string.
25-
public string errorMessage
26-
{
27-
get {return pErrorMessage;}
28-
}
29-
}
18+
// Successful property.
19+
public bool Successful => _successful;
20+
21+
// Error message string property.
22+
public string ErrorMessage => _errorMessage;
23+
}
3024
}

net-solution/PalindromePartitions/Program.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13-
string enteredText = "";
14-
bool inputValid = false;
15-
List<Partition> resultList = new List<Partition>();
16-
17-
try
13+
try
1814
{
1915
// Read and validate input text.
20-
enteredText = InputArg.ReadInputText(args);
21-
inputValid = InputArg.ValidateInputText(ref enteredText);
16+
string enteredText = InputArg.ReadInputText(args);
17+
bool inputValid = InputArg.ValidateInputText(ref enteredText);
2218

23-
if (inputValid == true)
19+
if (inputValid)
2420
{
2521
// Perform algorithm.
26-
resultList = AlgorithmResults.InitializeList(ref enteredText);
22+
List<Partition> resultList = AlgorithmResults.InitializeList(ref enteredText);
2723
StringPartitioning.RunLoop(resultList);
2824

2925
// Display results to console.

net-solution/PalindromePartitions/Tasks/AlgorithmResults.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace PalindromePartitions.Tasks
88
public class AlgorithmResults
99
{
1010
// Max number of partitions to display.
11-
private const int maxResultsDisplay = 100;
11+
private const int MaxResultsDisplay = 100;
1212

1313

1414
// Initialize list of partitions using input string.
@@ -40,15 +40,15 @@ public static void OutputPartitions(List<Partition> resList)
4040

4141
// Debug function indicates output mode based on given number. Used for unit testing.
4242
public static int GetOutputType(int resultCount)
43-
{
44-
int resultFlag = -1;
43+
{
44+
int resultFlag;
4545

46-
if (resultCount > 0 && resultCount <= maxResultsDisplay)
46+
if (resultCount > 0 && resultCount <= MaxResultsDisplay)
4747
{
4848
// Complete.
4949
resultFlag = 1;
5050
}
51-
else if (resultCount > maxResultsDisplay)
51+
else if (resultCount > MaxResultsDisplay)
5252
{
5353
// Truncated.
5454
resultFlag = 0;
@@ -66,24 +66,21 @@ public static int GetOutputType(int resultCount)
6666
// Output display loop.
6767
private static void PrintOutput(List<Partition> rList)
6868
{
69-
int loopIndex = 0;
70-
int loopCutoff = Math.Min(rList.Count, maxResultsDisplay);
71-
Partition currentPartition = Partition.Empty();
72-
string currentString = "";
73-
74-
// Header
69+
int loopCutoff = Math.Min(rList.Count, MaxResultsDisplay);
70+
71+
// Header
7572
Console.WriteLine("");
7673
Console.WriteLine("---RESULTS---");
7774

7875
// Loop writes individual partitions to console, up to 100.
79-
for (loopIndex = 0; loopIndex < loopCutoff; loopIndex = loopIndex + 1)
76+
for (int loopIndex = 0; loopIndex < loopCutoff; loopIndex++)
8077
{
81-
currentPartition = rList[loopIndex];
82-
currentString = currentPartition.Join();
78+
Partition currentPartition = rList[loopIndex];
79+
string currentString = currentPartition.Join();
8380
Console.WriteLine(currentString);
8481
}
8582

86-
if (rList.Count > maxResultsDisplay)
83+
if (rList.Count > MaxResultsDisplay)
8784
{
8885
// Truncate further results.
8986
TruncateResults(rList.Count);
@@ -96,8 +93,8 @@ private static void PrintOutput(List<Partition> rList)
9693
// Displays result truncate message.
9794
private static void TruncateResults(int totalResCount)
9895
{
99-
int truncateCount = totalResCount - maxResultsDisplay;
100-
string outputString = "";
96+
int truncateCount = totalResCount - MaxResultsDisplay;
97+
string outputString;
10198

10299
if (truncateCount > 1)
103100
{

net-solution/PalindromePartitions/Tasks/InputArg.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace PalindromePartitions.Tasks
66
// Functions related to input text argument.
77
public class InputArg
88
{
9-
private static Regex syntax = new Regex("^[A-Z0-9]+$", RegexOptions.IgnoreCase);
10-
private const int maxLength = 100;
9+
private static readonly Regex Syntax = new Regex("^[A-Z0-9]+$", RegexOptions.IgnoreCase);
10+
private const int MaxLength = 100;
1111

1212
// Read text string.
1313
public static string ReadInputText(string[] argArray)
14-
{
15-
string inputRes = "";
14+
{
15+
string inputRes;
1616

1717
try
1818
{
@@ -31,28 +31,28 @@ public static string ReadInputText(string[] argArray)
3131
// Validate string length and characters.
3232
public static bool ValidateInputText(ref string inpTxt)
3333
{
34-
bool correctSyntax = syntax.IsMatch(inpTxt);
35-
bool validRes = false;
34+
bool correctSyntax = Syntax.IsMatch(inpTxt);
35+
bool validRes = false;
3636

37-
if (inpTxt.Length > 0 && inpTxt.Length <= maxLength && correctSyntax == true)
37+
if (inpTxt.Length > 0 && inpTxt.Length <= MaxLength && correctSyntax)
3838
{
3939
// Valid
4040
validRes = true;
4141
}
42-
else if (inpTxt.Length > 0 && inpTxt.Length <= maxLength)
42+
else if (inpTxt.Length > 0 && inpTxt.Length <= MaxLength)
4343
{
4444
// Invalid characters.
45-
throw new Exception("Input string must only contain alphanumeric characters.");
45+
throw new Exception("Input string must only contain alphanumeric characters.");
4646
}
47-
else if (inpTxt.Length > maxLength)
47+
else if (inpTxt.Length > MaxLength)
4848
{
4949
// Too long.
50-
throw new Exception("Input string can only have up to 100 characters.");
50+
throw new Exception("Input string can only have up to 100 characters.");
5151
}
5252
else
5353
{
5454
// Empty.
55-
throw new Exception("Input string is missing.");
55+
throw new Exception("Input string is missing.");
5656
}
5757

5858
return validRes;

0 commit comments

Comments
 (0)