File tree Expand file tree Collapse file tree 3 files changed +26
-13
lines changed Expand file tree Collapse file tree 3 files changed +26
-13
lines changed Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ public void FindNextGreaterElement_AllElementsHaveNoGreaterElement_ReturnsAllNeg
66
66
var result = FindNextGreaterElement ( input ) ;
67
67
68
68
// Assert
69
- Assert . That ( result , Is . EqualTo ( result ) ) ;
69
+ Assert . That ( result , Is . EqualTo ( expected ) ) ;
70
70
}
71
71
72
72
[ Test ]
Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ public void Reverse_MultipleElementStack_ReturnsCorrectOrder()
58
58
Assert . That ( stack . Count , Is . EqualTo ( 3 ) ) ;
59
59
Assert . That ( stack . Pop ( ) , Is . EqualTo ( 1 ) ) ; // Should return 1
60
60
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
62
62
}
63
63
64
64
[ Test ]
Original file line number Diff line number Diff line change @@ -41,34 +41,47 @@ public bool IsBalanced(string expression)
41
41
Stack < char > stack = new Stack < char > ( ) ;
42
42
foreach ( char c in expression )
43
43
{
44
- if ( c == '(' || c == '{' || c == '[' )
44
+ if ( IsOpeningParenthesis ( c ) )
45
45
{
46
46
stack . Push ( c ) ;
47
47
}
48
- else if ( c == ')' || c == '}' || c == ']' )
48
+ else if ( IsClosingParenthesis ( c ) )
49
49
{
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 ) )
58
51
{
59
52
return false ;
60
53
}
61
54
}
62
55
else
63
56
{
64
- // Throw an exception if an invalid character is found
65
57
throw new ArgumentException ( $ "Invalid character '{ c } ' found in the expression.") ;
66
58
}
67
59
}
68
60
69
61
return stack . Count == 0 ;
70
62
}
71
63
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
+
72
85
private static bool IsMatchingPair ( char open , char close )
73
86
{
74
87
return ParenthesesMap . ContainsKey ( open ) && ParenthesesMap [ open ] == close ;
You can’t perform that action at this time.
0 commit comments