25
1
You will be given a string s
. It is guaranteed that the string has equal and at least one [
s and ]
s. It is also guaranteed that the brackets are balanced. The string can also have other characters.
The objective is to output/return a list of tuples or a list of lists containing indices of each [
and ]
pair.
note: The string is zero-indexed.
Example:
!^45sdfd[hello world[[djfut]%%357]sr[jf]s][srtdg][]
should return
[(8, 41), (20, 33), (21, 27), (36, 39), (42, 48), (49, 50)]
or something equivalent to this. Tuples are not necessary. Lists can also be used.
Test cases:
input:[[asdf][][td([)ty54g% ]hg[[f]u][f[[jhg][gfd]sdf]sdfs]ghd]fr43f]
output:[(0, 62),(1, 6), (7, 8), (9, 56), (13, 22), (25, 30), (26, 28), (31, 52), (33, 47), (34, 38), (39, 43)]
input:[[][][][]][[][][][[[[(]]]]]))
output:[(0, 9), (1, 2), (3, 4), (5, 6), (7, 8), (10,26),(11, 12), (13, 14), (15, 16), (17, 25), (18, 24), (19, 23), (20, 22)]
input:[][][[]]
output:[(0, 1), (2, 3), (4, 7), (5, 6)]
input:[[[[[asd]as]sd]df]fgf][][]
output:[(0, 21), (1, 17), (2, 14), (3, 11), (4, 8), (22, 23), (24, 25)]
input:[]
output:[(0,1)]
input:[[(])]
output:[(0, 5), (1, 3)]
This is code-golf, so the shortest code in bytes for each programming language wins.
Related – JungHwan Min – 2018-06-07T17:03:22.893
1Does the output order matter? – wastl – 2018-06-07T17:04:57.347
1no, it does not. – Windmill Cookies – 2018-06-07T17:05:11.937
21"note: The string is zero-indexed." - It's very common to allow implementations to choose a consistent indexing in these kind of challenges (but it is, of course, up to you) – Jonathan Allan – 2018-06-07T17:05:33.910
Is there any restriction on the order of the tuple in the output? Must they be sorted in increasing open paren position? – user202729 – 2018-06-07T17:14:56.103
@user202729 no there is no restriction about the order. – Windmill Cookies – 2018-06-07T17:21:44.427
1Can we take input as an array of characters? – Shaggy – 2018-06-07T17:31:44.327
Related: Telescopic Parentheses
– Luis Mendo – 2018-06-07T17:32:06.627@LuisMendo I think the challenge of garbage inbetween the parens makes this unique enough that I definitely can't reuse the 05AB1E implementation. – Magic Octopus Urn – 2018-06-07T17:40:54.347
@ngn thanks, will edit – Windmill Cookies – 2018-06-07T17:57:48.903
@ngn update: edited. now there is one extra closed bracket at index 26 and the string is valid. – Windmill Cookies – 2018-06-07T18:07:45.320
Can the output format be
[0, 5, 1, 3]
instead of[[0, 5], [1, 3]]
(for example) – dylnan – 2018-06-07T18:08:52.063@dylnan yes, but only prefer that if your language does not support nested lists. – Windmill Cookies – 2018-06-07T18:10:12.567
The language I'm thinking of does support nested lists, but it is one indexed. Can one indexed languages use one indexing? – dylnan – 2018-06-07T18:18:05.447
1why not just subtract 1 from every index? – Windmill Cookies – 2018-06-07T18:18:59.570
7Costs one byte... – dylnan – 2018-06-07T18:28:16.353
More in some languages. – Shaggy – 2018-06-08T09:25:01.090