Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Code Golf 1 - Nesting Brackets?

Asked by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 7 years ago

Hello ScriptingHelpers!

This is not a "question" -- it is a challenge. There are no prizes.

Code Golf

Code golf is a "game" where you try to make a program as small as possible. Note: this usually means throwing things like speed and good-practice out the window.

The smallest program "wins". For this code golf competition, you are required to use Lua. Post your solution (or a link to your solution in Hastebin to avoid spoilers) and give a brief explanation of how you did it. If you can, include a "pretty" version of your answer that is more readable.

Scoring

Your answer will be a single function called f which takes in input and returns the correct output.

Your score is the number of bytes your solution uses (which is characters, including spaces, newlines, etc).

Thus a solution that looked like this:

f=function()return 5 end

would have a score of 24.

The Task

Given input that loos like {1, {{3}}, {5, 6}, {{{{8}}}}, 1}, you need to return the deepest "depth" of the nested brackets. For example, in {5, {9}}, the answer is 2 since 9 is inside a list that is inside a list.

The elements of the lists are always positive integers, and the brackets are always { and }.

You can assume the answer is always at least 1. i.e., your program does not need to work for f(5).

You may not assume that the elements have things in them. e.g. f{{{}}} is still 3 even though no numbers are inside.

Task A

The first version of your function takes a string as input.

Here is sample input/output:

f "{1}"                                                               --> 1
f "{1, 2, {{3}}, {}}"                                                 --> 3
f "{{1, 2, 3}}"                                                       --> 2
f "{3, {3, {3}, 3}, 3}"                                               --> 3
f "{{{{1}, 2}, {3, {4}}}}"                                            --> 4
f "{1, {{3}}, {5, 6}, {{{{8}}}}, 1}"                                  --> 5
f "{1, {{2, 3, {{4}, 5}, 6, {7, 8}}, 9, {10, {{{11}}}}, 12, 13}, 14}" --> 6
f "{{{{{{{3}}}}}}}"                                                   --> 7

Task B

The second version of your input actually takes lists of lists as input.

f {1}                                                               --> 1
f {1, 2, {{3}}, {}}                                                 --> 3
f {{1, 2, 3}}                                                       --> 2
f {3, {3, {3}, 3}, 3}                                               --> 3
f {{{{1}, 2}, {3, {4}}}}                                            --> 4
f {1, {{3}}, {5, 6}, {{{{8}}}}, 1}                                  --> 5
f {1, {{2, 3, {{4}, 5}, 6, {7, 8}}, 9, {10, {{{11}}}}, 12, 13}, 14} --> 6
f {{{{{{{3}}}}}}}                                                   --> 7

Leaderboard - A (Strings)

Place User Score
1. XAXA 99
2. BlueTaslem 110

Leaderboard - B (Lists)

Place User Score
1. NullSenseStudio 95
2. BlueTaslem 106

2 answers

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
8 years ago

TASK A (Strings) - 99 bytes

golfed

de-golfed

Ad
Log in to vote
2
Answered by 8 years ago

Task B

99 bytes

answer

prettified

Edit thanks to XAXA:

95 bytes

answer

prettified

1
You can drop one byte by substituting the `pairs` iterator with `next`. XAXA 1569 — 8y
1
Even shorter yet: replace `type(o)=="table"` with `pcall(next,o)`! XAXA 1569 — 8y
0
Quite creative. NullSenseStudio 342 — 8y

Answer this question