Skip to content

Commit a6732f9

Browse files
resolved codacy issues
1 parent 64fe596 commit a6732f9

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Algorithms.Tests/Stack/NextGreaterElementTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void FindNextGreaterElement_AllElementsHaveNoGreaterElement_ReturnsAllNeg
6666
var result = FindNextGreaterElement(input);
6767

6868
// Assert
69-
Assert.That(result, Is.EqualTo(result));
69+
Assert.That(result, Is.EqualTo(expected));
7070
}
7171

7272
[Test]

Algorithms.Tests/Stack/ReverseStackTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Reverse_MultipleElementStack_ReturnsCorrectOrder()
5858
Assert.That(stack.Count, Is.EqualTo(3));
5959
Assert.That(stack.Pop(), Is.EqualTo(1)); // Should return 1
6060
Assert.That(stack.Pop(), Is.EqualTo(2)); // Should return 2
61-
Assert.That(stack.Pop(), Is.EqualTo(3)); ; // Should return 3
61+
Assert.That(stack.Pop(), Is.EqualTo(3)); // Should return 3
6262
}
6363

6464
[Test]

Algorithms/Stack/BalancedParenthesesChecker.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,47 @@ public bool IsBalanced(string expression)
4141
Stack<char> stack = new Stack<char>();
4242
foreach (char c in expression)
4343
{
44-
if (c == '(' || c == '{' || c == '[')
44+
if (IsOpeningParenthesis(c))
4545
{
4646
stack.Push(c);
4747
}
48-
else if (c == ')' || c == '}' || c == ']')
48+
else if (IsClosingParenthesis(c))
4949
{
50-
if (stack.Count == 0)
51-
{
52-
return false;
53-
}
54-
55-
char open = stack.Pop();
56-
57-
if (!IsMatchingPair(open, c))
50+
if (!IsBalancedClosing(stack, c))
5851
{
5952
return false;
6053
}
6154
}
6255
else
6356
{
64-
// Throw an exception if an invalid character is found
6557
throw new ArgumentException($"Invalid character '{c}' found in the expression.");
6658
}
6759
}
6860

6961
return stack.Count == 0;
7062
}
7163

64+
private static bool IsOpeningParenthesis(char c)
65+
{
66+
return c == '(' || c == '{' || c == '[';
67+
}
68+
69+
private static bool IsClosingParenthesis(char c)
70+
{
71+
return c == ')' || c == '}' || c == ']';
72+
}
73+
74+
private static bool IsBalancedClosing(Stack<char> stack, char close)
75+
{
76+
if (stack.Count == 0)
77+
{
78+
return false;
79+
}
80+
81+
char open = stack.Pop();
82+
return IsMatchingPair(open, close);
83+
}
84+
7285
private static bool IsMatchingPair(char open, char close)
7386
{
7487
return ParenthesesMap.ContainsKey(open) && ParenthesesMap[open] == close;

0 commit comments

Comments
 (0)