Parentheses sequences in lexicographical order
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Challenge Taken from here and also here
An n parentheses sequence consists of n (
s and n )
s.
A valid parentheses sequence is defined as the following:
You can find a way to repeat erasing adjacent pair of parentheses "()" until it becomes empty.
For example,
(())
is a valid parentheses, you can erase the pair on the 2nd and 3rd position and it becomes()
, then you can make it empty.
)()(
is not a valid parentheses, after you erase the pair on the 2nd and 3rd position, it becomes)(
and you cannot erase any more
Task
Given a number n you need to generate all correct parenthesis sequence in lexicographical order
Output can be an array, list or string (in this case a sequence per line)
You can use a different pair of parenthesis such as ,
,
()
or any open-close sign
Example
n = 3
((()))
(()())
(())()
()(())
()()()n = 2
(())
()()
code-golf
add a comment |Â
up vote
3
down vote
favorite
Challenge Taken from here and also here
An n parentheses sequence consists of n (
s and n )
s.
A valid parentheses sequence is defined as the following:
You can find a way to repeat erasing adjacent pair of parentheses "()" until it becomes empty.
For example,
(())
is a valid parentheses, you can erase the pair on the 2nd and 3rd position and it becomes()
, then you can make it empty.
)()(
is not a valid parentheses, after you erase the pair on the 2nd and 3rd position, it becomes)(
and you cannot erase any more
Task
Given a number n you need to generate all correct parenthesis sequence in lexicographical order
Output can be an array, list or string (in this case a sequence per line)
You can use a different pair of parenthesis such as ,
,
()
or any open-close sign
Example
n = 3
((()))
(()())
(())()
()(())
()()()n = 2
(())
()()
code-golf
Can we use a different pair of parenthesises, e.g.?
– Jo King
34 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Challenge Taken from here and also here
An n parentheses sequence consists of n (
s and n )
s.
A valid parentheses sequence is defined as the following:
You can find a way to repeat erasing adjacent pair of parentheses "()" until it becomes empty.
For example,
(())
is a valid parentheses, you can erase the pair on the 2nd and 3rd position and it becomes()
, then you can make it empty.
)()(
is not a valid parentheses, after you erase the pair on the 2nd and 3rd position, it becomes)(
and you cannot erase any more
Task
Given a number n you need to generate all correct parenthesis sequence in lexicographical order
Output can be an array, list or string (in this case a sequence per line)
You can use a different pair of parenthesis such as ,
,
()
or any open-close sign
Example
n = 3
((()))
(()())
(())()
()(())
()()()n = 2
(())
()()
code-golf
Challenge Taken from here and also here
An n parentheses sequence consists of n (
s and n )
s.
A valid parentheses sequence is defined as the following:
You can find a way to repeat erasing adjacent pair of parentheses "()" until it becomes empty.
For example,
(())
is a valid parentheses, you can erase the pair on the 2nd and 3rd position and it becomes()
, then you can make it empty.
)()(
is not a valid parentheses, after you erase the pair on the 2nd and 3rd position, it becomes)(
and you cannot erase any more
Task
Given a number n you need to generate all correct parenthesis sequence in lexicographical order
Output can be an array, list or string (in this case a sequence per line)
You can use a different pair of parenthesis such as ,
,
()
or any open-close sign
Example
n = 3
((()))
(()())
(())()
()(())
()()()n = 2
(())
()()
code-golf
code-golf
edited 32 mins ago
asked 41 mins ago


Luis felipe De jesus Munoz
3,54011049
3,54011049
Can we use a different pair of parenthesises, e.g.?
– Jo King
34 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago
add a comment |Â
Can we use a different pair of parenthesises, e.g.?
– Jo King
34 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago
Can we use a different pair of parenthesises, e.g.
?– Jo King
34 mins ago
Can we use a different pair of parenthesises, e.g.
?– Jo King
34 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
3
down vote
Perl 6, 36 bytes
grep try !.EVAL,[X~] <[ ]>xx$_*2
Try it online!
Finds all lexographical combinations of $2n$ s and filters the ones that
EVAL
correctly. Note that is evaluated perfectly fine
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
„()©s·ãʒ®õ:õQ
Try it online or verify some more test cases.
Explanation:
„() # Push string "()"
© # Store it in the register without popping
s· # Swap to get the (implicit) input, and double it
ã # Cartesian product that many times
ʒ # Filter it by:
® # Push the "()" from the register
õ: # Infinite replacement with an empty string
õQ # And only keep those which are empty after the infinite replacement
add a comment |Â
up vote
1
down vote
Python 2, 91 88 84 bytes
f=lambda n:n and sorted(set(a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)))or['']
Try it online!
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 88 84 bytes
Returns an array.
n=>(g=(n,s='')=>n--?g(n,s+'()')&g(n,'()'+s)&g(n,`($s)`):g[s]=s)(n)||Object.keys(g)
Try it online!
add a comment |Â
up vote
0
down vote
Japt, 15 bytes
ç"" á Ôke"%
Try it
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Perl 6, 36 bytes
grep try !.EVAL,[X~] <[ ]>xx$_*2
Try it online!
Finds all lexographical combinations of $2n$ s and filters the ones that
EVAL
correctly. Note that is evaluated perfectly fine
add a comment |Â
up vote
3
down vote
Perl 6, 36 bytes
grep try !.EVAL,[X~] <[ ]>xx$_*2
Try it online!
Finds all lexographical combinations of $2n$ s and filters the ones that
EVAL
correctly. Note that is evaluated perfectly fine
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Perl 6, 36 bytes
grep try !.EVAL,[X~] <[ ]>xx$_*2
Try it online!
Finds all lexographical combinations of $2n$ s and filters the ones that
EVAL
correctly. Note that is evaluated perfectly fine
Perl 6, 36 bytes
grep try !.EVAL,[X~] <[ ]>xx$_*2
Try it online!
Finds all lexographical combinations of $2n$ s and filters the ones that
EVAL
correctly. Note that is evaluated perfectly fine
answered 25 mins ago
Jo King
18.5k241100
18.5k241100
add a comment |Â
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
„()©s·ãʒ®õ:õQ
Try it online or verify some more test cases.
Explanation:
„() # Push string "()"
© # Store it in the register without popping
s· # Swap to get the (implicit) input, and double it
ã # Cartesian product that many times
ʒ # Filter it by:
® # Push the "()" from the register
õ: # Infinite replacement with an empty string
õQ # And only keep those which are empty after the infinite replacement
add a comment |Â
up vote
2
down vote
05AB1E, 13 bytes
„()©s·ãʒ®õ:õQ
Try it online or verify some more test cases.
Explanation:
„() # Push string "()"
© # Store it in the register without popping
s· # Swap to get the (implicit) input, and double it
ã # Cartesian product that many times
ʒ # Filter it by:
® # Push the "()" from the register
õ: # Infinite replacement with an empty string
õQ # And only keep those which are empty after the infinite replacement
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 13 bytes
„()©s·ãʒ®õ:õQ
Try it online or verify some more test cases.
Explanation:
„() # Push string "()"
© # Store it in the register without popping
s· # Swap to get the (implicit) input, and double it
ã # Cartesian product that many times
ʒ # Filter it by:
® # Push the "()" from the register
õ: # Infinite replacement with an empty string
õQ # And only keep those which are empty after the infinite replacement
05AB1E, 13 bytes
„()©s·ãʒ®õ:õQ
Try it online or verify some more test cases.
Explanation:
„() # Push string "()"
© # Store it in the register without popping
s· # Swap to get the (implicit) input, and double it
ã # Cartesian product that many times
ʒ # Filter it by:
® # Push the "()" from the register
õ: # Infinite replacement with an empty string
õQ # And only keep those which are empty after the infinite replacement
answered 15 mins ago


Kevin Cruijssen
33k554176
33k554176
add a comment |Â
add a comment |Â
up vote
1
down vote
Python 2, 91 88 84 bytes
f=lambda n:n and sorted(set(a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)))or['']
Try it online!
add a comment |Â
up vote
1
down vote
Python 2, 91 88 84 bytes
f=lambda n:n and sorted(set(a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)))or['']
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 2, 91 88 84 bytes
f=lambda n:n and sorted(set(a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)))or['']
Try it online!
Python 2, 91 88 84 bytes
f=lambda n:n and sorted(set(a[:i]+'()'+a[i:]for a in f(n-1)for i in range(n)))or['']
Try it online!
answered 18 mins ago


TFeld
12.9k2836
12.9k2836
add a comment |Â
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 88 84 bytes
Returns an array.
n=>(g=(n,s='')=>n--?g(n,s+'()')&g(n,'()'+s)&g(n,`($s)`):g[s]=s)(n)||Object.keys(g)
Try it online!
add a comment |Â
up vote
1
down vote
JavaScript (ES6), 88 84 bytes
Returns an array.
n=>(g=(n,s='')=>n--?g(n,s+'()')&g(n,'()'+s)&g(n,`($s)`):g[s]=s)(n)||Object.keys(g)
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 88 84 bytes
Returns an array.
n=>(g=(n,s='')=>n--?g(n,s+'()')&g(n,'()'+s)&g(n,`($s)`):g[s]=s)(n)||Object.keys(g)
Try it online!
JavaScript (ES6), 88 84 bytes
Returns an array.
n=>(g=(n,s='')=>n--?g(n,s+'()')&g(n,'()'+s)&g(n,`($s)`):g[s]=s)(n)||Object.keys(g)
Try it online!
edited 6 mins ago
answered 12 mins ago


Arnauld
67.4k584285
67.4k584285
add a comment |Â
add a comment |Â
up vote
0
down vote
Japt, 15 bytes
ç"" á Ôke"%
Try it
add a comment |Â
up vote
0
down vote
Japt, 15 bytes
ç"" á Ôke"%
Try it
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Japt, 15 bytes
ç"" á Ôke"%
Try it
Japt, 15 bytes
ç"" á Ôke"%
Try it
answered 4 mins ago


Shaggy
17.6k21663
17.6k21663
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175381%2fparentheses-sequences-in-lexicographical-order%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can we use a different pair of parenthesises, e.g.
?
– Jo King
34 mins ago
@JoKing Yes of course. I assume that wont make any difference anyway to the main concept of the challenge.
– Luis felipe De jesus Munoz
33 mins ago
Eh, I can think of a few languages where eval would interpret them differently for example
– Jo King
31 mins ago
Related: Catalan Numbers (result of that challenge = number of lines of result of this challenge)
– user202729
22 mins ago
Virtually the same, but with some weird restrictions like "You may not write recursive functions". /// A superset of this challenge (allow all Brain-Flak brackets)
– user202729
21 mins ago