Balanced brackets: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
Not a robot (talk | contribs) (add SETL) |
||
Line 7,963: | Line 7,963: | ||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program balanced_brackets; |
|||
setrandom(0); |
|||
loop for length in [1..10] do |
|||
s := generate length; |
|||
print(balanced s, s); |
|||
end loop; |
|||
op generate(n); |
|||
s := "["*n + "]"*n; |
|||
loop for i in [n*2, n*2-1..2] do |
|||
j := 1 + random(i - 1); |
|||
[s(i), s(j)] := [s(j), s(i)]; |
|||
end loop; |
|||
return s; |
|||
end op; |
|||
op balanced(s); |
|||
depth := 0; |
|||
loop for c in s do |
|||
case c of |
|||
("["): |
|||
depth +:= 1; |
|||
("]"): |
|||
depth -:= 1; |
|||
if depth<0 then return false; end if; |
|||
end case; |
|||
end loop; |
|||
return depth = 0; |
|||
end op; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>#F ][ |
|||
#F ]][[ |
|||
#T [[]][] |
|||
#F [[]]][][ |
|||
#F ][[][][]][ |
|||
#F ]][]]]][[[[[ |
|||
#F ]]][[]]][[[[[] |
|||
#T [[][][[][[]][]]] |
|||
#F []][[]]]][][][[][[ |
|||
#F []][]]][]][][[[][[][</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang="ruby">func balanced (str) { |
<syntaxhighlight lang="ruby">func balanced (str) { |