Skip to content

Commit d71bac9

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] Change {List-type property} to be read-only by removing the property setter (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2227)
1 parent a6df943 commit d71bac9

File tree

9 files changed

+198
-68
lines changed

9 files changed

+198
-68
lines changed

src/algorithm_exercises_csharp/hackerrank/warmup/SimpleArraySum.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ namespace algorithm_exercises_csharp.hackerrank.warmup;
44

55
public static class SimpleArraySum
66
{
7-
public static int simpleArraySum(int[] inputs)
7+
public static int simpleArraySum(List<int> ar)
88
{
9-
ArgumentNullException.ThrowIfNull(inputs);
9+
ArgumentNullException.ThrowIfNull(ar);
1010

1111
var total = 0;
1212

13-
foreach (int i in inputs)
13+
foreach (int i in ar)
1414
{
1515
total += i;
1616
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs

+22-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.a
55
[TestClass]
66
public class NewYearChaosTest
77
{
8-
public class NewYearChaosTestCase
8+
public class NewYearChaosTestCase(string title, int[] input, string expected)
99
{
10-
public string title { get; set; } = default!;
11-
public List<int> input { get; set; } = default!;
12-
public string expected { get; set; } = default!;
10+
private readonly string title = title;
11+
private readonly List<int> input = [.. input];
12+
private readonly string expected = expected;
13+
14+
public string Title
15+
{
16+
get { return title; }
17+
}
18+
19+
public List<int> Input
20+
{
21+
get { return input; }
22+
}
23+
24+
public string Expected
25+
{
26+
get { return expected; }
27+
}
1328
}
1429

1530
private List<NewYearChaosTestCase> testCases { get; set; } = default!;
@@ -29,10 +44,10 @@ public void testMinimumBribesText()
2944

3045
foreach (NewYearChaosTestCase test in testCases)
3146
{
32-
result = NewYearChaos.minimumBribesText(test.input);
33-
NewYearChaos.minimumBribes(test.input);
47+
result = NewYearChaos.minimumBribesText(test.Input);
48+
NewYearChaos.minimumBribes(test.Input);
3449

35-
Assert.AreEqual(test.expected, result);
50+
Assert.AreEqual(test.Expected, result);
3651
}
3752
}
3853
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.a
55
[TestClass]
66
public class TwoDArrayTest
77
{
8-
public class TwoDArrayTestCase
8+
public class TwoDArrayTestCase(string title, List<List<int>> input, long expected)
99
{
10-
public string title { get; set; } = default!;
11-
public List<List<int>> input { get; set; } = default!;
12-
public long expected { get; set; } = default!;
10+
private string title = title;
11+
private List<List<int>> input = input;
12+
private long expected = expected;
13+
14+
public string Title
15+
{
16+
get { return title; }
17+
}
18+
public List<List<int>> Input
19+
{
20+
get { return input; }
21+
}
22+
public long Expected
23+
{
24+
get { return expected; }
25+
}
1326
}
1427

1528
private List<TwoDArrayTestCase> testCases { get; set; } = default!;
@@ -29,8 +42,8 @@ public void testHourglassSum()
2942

3043
foreach (TwoDArrayTestCase test in testCases)
3144
{
32-
result = TwoDArray.hourglassSum(test.input);
33-
Assert.AreEqual(test.expected, result);
45+
result = TwoDArray.hourglassSum(test.Input);
46+
Assert.AreEqual(test.Expected, result);
3447
}
3548
}
3649

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.Test.cs

+29-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.d
55
[TestClass]
66
public class CountTripletsTest
77
{
8-
public class CountTripletsTestCase
8+
public class CountTripletsTestCase(string title, long[] input, int r, long expected)
99
{
10-
public string title { get; set; } = default!;
11-
public List<long> input { get; set; } = default!;
12-
public int r { get; set; } = default!;
13-
public long expected { get; set; } = default!;
10+
private readonly string title = title;
11+
private readonly List<long> input = [.. input];
12+
private readonly int r = r;
13+
private readonly long expected = expected;
14+
15+
public string Title
16+
{
17+
get { return title; }
18+
}
19+
20+
public List<long> Input
21+
{
22+
get { return input; }
23+
}
24+
25+
public int R
26+
{
27+
get { return r; }
28+
}
29+
30+
public long Expected
31+
{
32+
get { return expected; }
33+
}
1434
}
1535

1636
private List<CountTripletsTestCase> testCases { get; set; } = default!;
@@ -35,14 +55,14 @@ public void testCountTriplets()
3555

3656
foreach (CountTripletsTestCase test in testCases)
3757
{
38-
result = CountTriplets.countTriplets(test.input, test.r);
39-
Assert.AreEqual(test.expected, result);
58+
result = CountTriplets.countTriplets(test.Input, test.R);
59+
Assert.AreEqual(test.Expected, result);
4060
}
4161

4262
foreach (CountTripletsTestCase test in bigTestCases)
4363
{
44-
result = CountTriplets.countTriplets(test.input, test.r);
45-
Assert.AreEqual(test.expected, result);
64+
result = CountTriplets.countTriplets(test.Input, test.R);
65+
Assert.AreEqual(test.Expected, result);
4666
}
4767
}
4868
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.Test.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.d
55
[TestClass]
66
public class CountTripletsBruteForceTest
77
{
8-
public class CountTripletsBruteForceTestCase
8+
public class CountTripletsBruteForceTestCase(string title, long[] input, int r, long expected)
99
{
10-
public string title { get; set; } = default!;
11-
public List<long> input { get; set; } = default!;
12-
public int r { get; set; } = default!;
13-
public int expected { get; set; } = default!;
10+
private readonly string title = title;
11+
private readonly List<long> input = [.. input];
12+
private readonly int r = r;
13+
private readonly long expected = expected;
14+
15+
public string Title
16+
{
17+
get { return title; }
18+
}
19+
20+
public List<long> Input
21+
{
22+
get { return input; }
23+
}
24+
25+
public int R
26+
{
27+
get { return r; }
28+
}
29+
30+
public long Expected
31+
{
32+
get { return expected; }
33+
}
1434
}
1535

1636
private List<CountTripletsBruteForceTestCase> testCases { get; set; } = default!;
@@ -30,8 +50,8 @@ public void testCountTriplets()
3050

3151
foreach (CountTripletsBruteForceTestCase test in testCases)
3252
{
33-
result = CountTripletsBruteForce.countTriplets(test.input, test.r);
34-
Assert.AreEqual(test.expected, result);
53+
result = CountTripletsBruteForce.countTriplets(test.Input, test.R);
54+
Assert.AreEqual(test.Expected, result);
3555
}
3656
}
3757
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.d
55
[TestClass]
66
public class RansomNoteTest
77
{
8-
public class RansomNoteTestCase
8+
public class RansomNoteTestCase(string title, List<string> magazine, List<string> note, string expected)
99
{
10-
public string title { get; set; } = default!;
11-
public List<string> magazine { get; set; } = default!;
12-
public List<string> note { get; set; } = default!;
13-
public string expected { get; set; } = default!;
10+
private readonly string title = title;
11+
private readonly List<string> magazine = [.. magazine];
12+
private readonly List<string> note = [.. note];
13+
private readonly string expected = expected;
14+
15+
public string Title
16+
{
17+
get { return title; }
18+
}
19+
20+
public List<string> Magazine
21+
{
22+
get { return magazine; }
23+
}
24+
25+
public List<string> Note
26+
{
27+
get { return note; }
28+
}
29+
30+
public string Expected
31+
{
32+
get { return expected; }
33+
}
1434
}
1535

1636
private List<RansomNoteTestCase> testCases { get; set; } = default!;
@@ -30,8 +50,8 @@ public void testCheckMagazine()
3050

3151
foreach (RansomNoteTestCase test in testCases)
3252
{
33-
result = RansomNote.checkMagazine(test.magazine, test.note);
34-
Assert.AreEqual(test.expected, result);
53+
result = RansomNote.checkMagazine(test.Magazine, test.Note);
54+
Assert.AreEqual(test.Expected, result);
3555
}
3656
}
3757
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/SherlockAndAnagrams.Test.cs

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.d
55
[TestClass]
66
public class SherlockAndAnagramsTest
77
{
8-
public class SherlockAndAnagramsTestCase
8+
public class TestCase
99
{
10-
/**
11-
* SherlockAndAnagramsTestCase.TestCase.
12-
*/
13-
public class TestCase
10+
public string input { get; set; } = default!;
11+
public int expected { get; set; } = default!;
12+
}
13+
14+
public class SherlockAndAnagramsTestCase(string title, List<TestCase> tests)
15+
{
16+
private string title = title;
17+
private List<TestCase> tests = tests;
18+
19+
public string Title
1420
{
15-
public string input { get; set; } = default!;
16-
public int expected { get; set; } = default!;
21+
get { return title; }
22+
}
23+
public List<TestCase> Tests
24+
{
25+
get { return tests; }
1726
}
18-
19-
public string title { get; set; } = default!;
20-
public List<TestCase> tests { get; set; } = default!;
2127
}
2228

2329
private List<SherlockAndAnagramsTestCase> testCases { get; set; } = default!;
@@ -37,7 +43,7 @@ public void testSherlockAndAnagrams()
3743

3844
foreach (SherlockAndAnagramsTestCase testSet in testCases)
3945
{
40-
foreach (SherlockAndAnagramsTestCase.TestCase test in testSet.tests)
46+
foreach (SherlockAndAnagramsTest.TestCase test in testSet.Tests)
4147
{
4248
result = SherlockAndAnagrams.sherlockAndAnagrams(test.input);
4349
Assert.AreEqual(test.expected, result);

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.Test.cs

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.g
55
[TestClass]
66
public class MinimumAbsoluteDifferenceInAnArrayTest
77
{
8-
public class MinimumAbsoluteDifferenceInAnArrayTestCase
8+
public class MinimumAbsoluteDifferenceInAnArrayTestCase(string title, List<int> input, int expected)
99
{
10-
public string title = "";
11-
public List<int> input = [];
12-
public int expected;
10+
private readonly string title = title;
11+
private readonly List<int> input = [.. input];
12+
private readonly int expected = expected;
13+
14+
public string Title
15+
{
16+
get { return title; }
17+
}
18+
public List<int> Input
19+
{
20+
get { return input; }
21+
}
22+
public int Expected
23+
{
24+
get { return expected; }
25+
}
1326
}
1427

1528
private List<MinimumAbsoluteDifferenceInAnArrayTestCase> testCases { get; set; } = default!;
@@ -29,15 +42,15 @@ public void testMinimumAbsoluteDifferenceInAnArray()
2942

3043
foreach (MinimumAbsoluteDifferenceInAnArrayTestCase test in testCases)
3144
{
32-
result = MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference(test.input);
45+
result = MinimumAbsoluteDifferenceInAnArray.minimumAbsoluteDifference(test.Input);
3346
Assert.AreEqual(
34-
test.expected,
47+
test.Expected,
3548
result,
3649
string.Format(
3750
System.Globalization.CultureInfo.InvariantCulture,
3851
"minimumAbsoluteDifference({0}) => must be: {1}",
39-
test.input.ToString(),
40-
test.expected
52+
test.Input.ToString(),
53+
test.Expected
4154
)
4255
);
4356
}

0 commit comments

Comments
 (0)