Balanced brackets: Difference between revisions
Content added Content deleted
m (→{{header|TUSCRIPT}}: Omit from bc, dc. No string operations.) |
(Added C#.) |
||
Line 81: | Line 81: | ||
[[][]]][: FALSE |
[[][]]][: FALSE |
||
[]]]][[[: FALSE</pre> |
[]]]][[[: FALSE</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Linq; |
|||
class Program |
|||
{ |
|||
static bool IsBalanced(string text, char open = '[', char close = ']') |
|||
{ |
|||
var level = 0; |
|||
foreach (var character in text) |
|||
{ |
|||
if (character == close) |
|||
{ |
|||
if (level == 0) |
|||
{ |
|||
return false; |
|||
} |
|||
level--; |
|||
} |
|||
if (character == open) |
|||
{ |
|||
level++; |
|||
} |
|||
} |
|||
return level == 0; |
|||
} |
|||
static string RandomBrackets(int count, char open = '[', char close = ']') |
|||
{ |
|||
var random = new Random(); |
|||
return string.Join(string.Empty, (new string(open, count) + new string(close, count)).OrderBy(c => random.Next())); |
|||
} |
|||
static void Main() |
|||
{ |
|||
for (var count = 0; count < 9; count++) |
|||
{ |
|||
var text = RandomBrackets(count); |
|||
Console.WriteLine("\"{0}\" is {1}balanced.", text, IsBalanced(text) ? string.Empty : "not "); |
|||
} |
|||
} |
|||
}</lang> |
|||
Sample output: |
|||
<lang>"" is balanced. |
|||
"[]" is balanced. |
|||
"[]][" is not balanced. |
|||
"[][][]" is balanced. |
|||
"[[[]][]]" is balanced. |
|||
"[][[][[]]]" is balanced. |
|||
"[]][][][[][]" is not balanced. |
|||
"[]]][][]][[][[" is not balanced. |
|||
"[]]][]]][[][[][[" is not balanced.</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp>#include <algorithm> |
<lang cpp>#include <algorithm> |