Skip to content

Commit 5adc707

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] Check null arguments
1 parent eb03387 commit 5adc707

File tree

17 files changed

+45
-17
lines changed

17 files changed

+45
-17
lines changed

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ protected ArraysLeftRotation() { }
1616
*/
1717
public static List<int> rotLeftOne(List<int> input)
1818
{
19+
ArgumentNullException.ThrowIfNull(input);
20+
1921
int first = input[FIRST_POSITION];
2022
input.RemoveAt(FIRST_POSITION);
2123
input.Add(first);
@@ -28,6 +30,8 @@ public static List<int> rotLeftOne(List<int> input)
2830
*/
2931
public static List<int> rotLeft(List<int> input, int d)
3032
{
33+
ArgumentNullException.ThrowIfNull(input);
34+
3135
// Clone the list
3236
List<int> output = input.GetRange(FIRST_POSITION, input.Count);
3337

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ protected CrushBruteForce() { }
1414

1515
public static long arrayManipulation(int n, List<List<int>> queries)
1616
{
17+
ArgumentNullException.ThrowIfNull(queries);
18+
1719
// why adding 1?
1820
// first slot to adjust 1-based index and
1921
int[] result = new int[n + 1];

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/CrushOptimized.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ private CrushOptimized() { }
1515
*/
1616
public static long arrayManipulation(int n, List<List<int>> queries)
1717
{
18+
ArgumentNullException.ThrowIfNull(queries);
19+
1820
// why adding 2?
1921
// first slot to adjust 1-based index and
2022
// last slot for storing accumSum result

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.arrays;
44

5-
using System.Diagnostics.CodeAnalysis;
6-
7-
public class NewYearChaos
5+
public static class NewYearChaos
86
{
9-
[ExcludeFromCodeCoverage]
10-
protected NewYearChaos() { }
11-
12-
public const String TOO_CHAOTIC_ERROR = "Too chaotic";
7+
public const string TOO_CHAOTIC_ERROR = "Too chaotic";
138

149
/**
1510
* minimumBribesCalculate.
1611
*/
1712
public static int minimumBribesCalculate(List<int> q)
1813
{
14+
ArgumentNullException.ThrowIfNull(q);
15+
1916
int bribes = 0;
2017
int i = 0;
2118

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/arrays/TwoDArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ private static List<int> getHourGlass(List<List<int>> arr, int positionX, int po
3131

3232
public static int hourglassSum(List<List<int>> arr)
3333
{
34+
ArgumentNullException.ThrowIfNull(arr);
35+
3436
int matrixSize = arr.Count;
3537

3638
int matrixStartIndex = 1;

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.cs

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

55
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
66

7-
using System.Diagnostics.CodeAnalysis;
87
using System.Collections.Generic;
98

109
public class CountTriplets
@@ -14,6 +13,8 @@ protected CountTriplets() { }
1413

1514
public static long countTriplets(List<long> arr, long r)
1615
{
16+
ArgumentNullException.ThrowIfNull(arr);
17+
1718
Dictionary<long, long> aCounter = [];
1819
Dictionary<long, long> bCounter = [];
1920
long triplets = 0L;

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTripletsBruteForce.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ protected CountTripletsBruteForce() { }
1212

1313
public static long countTriplets(List<long> arr, long r)
1414
{
15+
ArgumentNullException.ThrowIfNull(arr);
16+
1517
long size = arr.Count;
1618
long counter = 0L;
1719

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/FrequencyQueries.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ void select(long value)
116116
*/
117117
public static List<int> freqQuery(List<List<int>> queries)
118118
{
119+
ArgumentNullException.ThrowIfNull(queries);
120+
119121
FrequencyQueries fq = new();
120122

121123
foreach (List<int> query in queries)

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public InvalidValueException(string msg)
2424

2525
public static bool checkMagazineCompute(List<string> magazine, List<string> note)
2626
{
27+
ArgumentNullException.ThrowIfNull(magazine);
28+
ArgumentNullException.ThrowIfNull(note);
29+
2730
Dictionary<string, int> dictionary = [];
2831

2932
foreach (string word in magazine)

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/SherlockAndAnagrams.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static BigInteger factorial(int number)
2626

2727
public static int sherlockAndAnagrams(string s)
2828
{
29+
ArgumentException.ThrowIfNullOrEmpty(s);
30+
2931
Dictionary<string, List<string>> candidates = [];
3032

3133
int size = s.Length;

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ protected TwoStrings() { }
1515

1616
public static bool twoStringsCompute(string s1, string s2)
1717
{
18-
char occurrence = s1.FirstOrDefault(c => s2.Contains(c), __EMPTY_CHAR__);
18+
ArgumentException.ThrowIfNullOrEmpty(s1);
19+
ArgumentException.ThrowIfNullOrEmpty(s2);
20+
21+
char occurrence = s1.FirstOrDefault(s2.Contains, __EMPTY_CHAR__);
1922

2023
if (occurrence != __EMPTY_CHAR__)
2124
{

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ protected LuckBalance() { }
1717

1818
public static int luckBalance(int k, List<List<int>> contests)
1919
{
20+
ArgumentNullException.ThrowIfNull(contests);
21+
2022
List<Competition> important_competitions = [];
2123
List<Competition> nonimportant_competitions = [];
2224

src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/greedy_algorithms/MinimumAbsoluteDifferenceInAnArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ private MinimumAbsoluteDifferenceInAnArray()
1616
*/
1717
public static int minimumAbsoluteDifference(List<int> arr)
1818
{
19+
ArgumentNullException.ThrowIfNull(arr);
20+
1921
List<int> sortedNums = [.. arr.ConvertAll(x => x).OrderBy(x => x).ToList()];
2022

2123
// Find the minimum absolute difference

src/algorithm_exercises_csharp/hackerrank/warmup/AVeryBigSum.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
namespace algorithm_exercises_csharp.hackerrank.warmup;
44

5-
using System.Diagnostics.CodeAnalysis;
6-
7-
public class AVeryBigSum
5+
public static class AVeryBigSum
86
{
9-
[ExcludeFromCodeCoverage]
10-
protected AVeryBigSum() { }
11-
12-
public static long aVeryBigSum(List<long> _ar)
7+
public static long aVeryBigSum(List<long> ar)
138
{
9+
ArgumentNullException.ThrowIfNull(ar);
10+
1411
var total = 0L;
1512

16-
foreach (long x in _ar)
13+
foreach (long x in ar)
1714
{
1815
total += x;
1916
}

src/algorithm_exercises_csharp/hackerrank/warmup/BirthdayCakeCandles.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ protected BirthdayCakeCandles() { }
1111

1212
public static int birthdayCakeCandles(List<int> _arr)
1313
{
14+
ArgumentNullException.ThrowIfNull(_arr);
15+
1416
if (_arr.Count == 0)
1517
{
1618
throw new ArgumentException("Parameter cannot be empty", nameof(_arr));

src/algorithm_exercises_csharp/hackerrank/warmup/CompareTriplets.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ protected CompareTriplets() { }
1111

1212
public static List<int> compareTriplets(List<int> _a, List<int> _b)
1313
{
14+
ArgumentNullException.ThrowIfNull(_a);
15+
ArgumentNullException.ThrowIfNull(_b);
16+
1417
List<int> awards = [0, 0];
1518

1619
for (int i = 0; i < _a.Count; i++)

src/algorithm_exercises_csharp/hackerrank/warmup/DiagonalDifference.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ protected DiagonalDifference() { }
1111

1212
public static int diagonalDifference(List<List<int>> _arr)
1313
{
14+
ArgumentNullException.ThrowIfNull(_arr);
15+
1416
int diag1 = 0;
1517
int diag2 = 0;
1618
int last = _arr.Count - 1;

0 commit comments

Comments
 (0)