Balanced brackets: Difference between revisions
Content added Content deleted
(→{{header|XBasic}}: Added) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 598: | Line 598: | ||
input is: []line 1:2 missing NEWLINE at ']' |
input is: []line 1:2 missing NEWLINE at ']' |
||
</pre> |
</pre> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 1,791: | Line 1,790: | ||
]]]] false |
]]]] false |
||
</lang> |
</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 1,839: | Line 1,837: | ||
NIL: ]][[[[][]][[[[]]]] |
NIL: ]][[[[][]][[[[]]]] |
||
</pre> |
</pre> |
||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
BlackBox Component Builder |
BlackBox Component Builder |
||
Line 1,913: | Line 1,912: | ||
[]][[]:> $FALSE |
[]][[]:> $FALSE |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
===Standard Version=== |
===Standard Version=== |
||
Line 3,792: | Line 3,792: | ||
The string '[[]]][][[][]' is not OK. Unbalanced. |
The string '[[]]][][[][]' is not OK. Unbalanced. |
||
The string '][[][[][][]]][[]' is not OK. Unbalanced. |
The string '][[][[][][]]][[]' is not OK. Unbalanced. |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 4,461: | Line 4,460: | ||
return !$depth |
return !$depth |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
There's More Than One Way To Do It. |
|||
===Depth counter=== |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>sub balanced($s) { |
|||
my $l = 0; |
|||
for $s.comb { |
|||
when "]" { |
|||
--$l; |
|||
return False if $l < 0; |
|||
} |
|||
when "[" { |
|||
++$l; |
|||
} |
|||
} |
|||
return $l == 0; |
|||
} |
|||
my $n = prompt "Number of brackets"; |
|||
my $s = (<[ ]> xx $n).flat.pick(*).join; |
|||
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang> |
|||
===FP oriented=== |
|||
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance. |
|||
<lang perl6>sub balanced($s) { |
|||
.none < 0 and .[*-1] == 0 |
|||
given ([\+] '\\' «leg« $s.comb).cache; |
|||
} |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = <[ ]>.roll($n*2).join; |
|||
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</lang> |
|||
===String munging=== |
|||
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left. |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>sub balanced($_ is copy) { |
|||
Nil while s:g/'[]'//; |
|||
$_ eq ''; |
|||
} |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = <[ ]>.roll($n*2).join; |
|||
say "$s is", ' not' x not balanced($s), " well-balanced";</lang> |
|||
===Parsing with a grammar=== |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>grammar BalBrack { token TOP { '[' <TOP>* ']' } } |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = ('[' xx $n, ']' xx $n).flat.pick(*).join; |
|||
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 5,117: | Line 5,061: | ||
</lang> |
</lang> |
||
=={{header|R}}== |
=={{header|R}}== |
||
Line 5,176: | Line 5,119: | ||
(for ([n 10]) (try n)) |
(for ([n 10]) (try n)) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
There's More Than One Way To Do It. |
|||
===Depth counter=== |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>sub balanced($s) { |
|||
my $l = 0; |
|||
for $s.comb { |
|||
when "]" { |
|||
--$l; |
|||
return False if $l < 0; |
|||
} |
|||
when "[" { |
|||
++$l; |
|||
} |
|||
} |
|||
return $l == 0; |
|||
} |
|||
my $n = prompt "Number of brackets"; |
|||
my $s = (<[ ]> xx $n).flat.pick(*).join; |
|||
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang> |
|||
===FP oriented=== |
|||
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance. |
|||
<lang perl6>sub balanced($s) { |
|||
.none < 0 and .[*-1] == 0 |
|||
given ([\+] '\\' «leg« $s.comb).cache; |
|||
} |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = <[ ]>.roll($n*2).join; |
|||
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</lang> |
|||
===String munging=== |
|||
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left. |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>sub balanced($_ is copy) { |
|||
Nil while s:g/'[]'//; |
|||
$_ eq ''; |
|||
} |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = <[ ]>.roll($n*2).join; |
|||
say "$s is", ' not' x not balanced($s), " well-balanced";</lang> |
|||
===Parsing with a grammar=== |
|||
{{works with|Rakudo|2015.12}} |
|||
<lang perl6>grammar BalBrack { token TOP { '[' <TOP>* ']' } } |
|||
my $n = prompt "Number of bracket pairs: "; |
|||
my $s = ('[' xx $n, ']' xx $n).flat.pick(*).join; |
|||
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</lang> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
Line 6,271: | Line 6,270: | ||
balanced : x[ y [ [] z ]][ 1 ][]abcd |
balanced : x[ y [ [] z ]][ 1 ][]abcd |
||
</pre> |
</pre> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
<lang simula>BEGIN |
<lang simula>BEGIN |
||
Line 6,801: | Line 6,801: | ||
'[[][]]': true, |
'[[][]]': true, |
||
'[[[]]]': true></pre> |
'[[[]]]': true></pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<lang vb> |
<lang vb> |
||
Line 7,233: | Line 7,234: | ||
[[][]] OK |
[[][]] OK |
||
</pre> |
</pre> |
||
=={{header|Ya}}== |
=={{header|Ya}}== |