Balanced brackets: Difference between revisions
Content added Content deleted
Line 1,524: | Line 1,524: | ||
Recursively remove occurrences of '[]': |
Recursively remove occurrences of '[]': |
||
<lang javascript> |
<lang javascript> |
||
function |
function checkBalance(i) { |
||
while (i.length % 2 == 0) { |
while (i.length % 2 == 0) { |
||
j = i.replace(' |
j = i.replace('{}',''); |
||
if (j == i) |
if (j == i) |
||
break; |
break; |
||
i = j; |
i = j; |
||
} |
} |
||
return (i?false:true); |
return (i?false:true); |
||
} |
|||
var g = 10; |
|||
while (g--) { |
|||
var N = 10 - Math.floor(g/2), n=N, o=''; |
|||
while (n || N) { |
|||
if (N == 0 || n == 0) { |
|||
o+=Array(++N).join('}') + Array(++n).join('{'); |
|||
break; |
|||
} |
|||
if (Math.round(Math.random()) == 1) { |
|||
o+='}'; |
|||
N--; |
|||
} |
|||
else { |
|||
o+='{'; |
|||
n--; |
|||
} |
|||
} |
|||
alert(o+": "+checkBalance(o)); |
|||
} |
} |
||
</lang> |
</lang> |