6
\$\begingroup\$

I'll use this challenge as an example. It says "Input will be a list of [...]" and I would like to code that challenge in Lua.

The problem here is, Lua doesn't accept lists as input, only strings (and maybe numbers since it has implicit coercion). There are two solutions:

  • I can write the list as a string and deserialize it no problem, but should I count this process in my byte count? Or there would be no problem in deserializing and passing it to the function/rest of the program as a list (we call lists 'tables' in Lua)?
  • I can pass each element on the list as different arguments, because then Lua would let me create a list (table) from these arguments.

I hope my question is clear. If there's already an answer for that I'm sorry, but I couldn't find.

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ A submission can be a function, so just make it a function (or lambda) accepting a list \$\endgroup\$ – ASCII-only Aug 1 '18 at 0:02
  • \$\begingroup\$ Yeah, but a function might not be the best solution for some challenges. If there's no problem in doing one (or both) of the things that I've said it would be better \$\endgroup\$ – Visckmart Aug 1 '18 at 0:10
  • \$\begingroup\$ If you have to process the input, it counts towards the byte count of your submission (for the case of deserialization). Using a function, or the built-in table arg (or even ...) for a program is the way to do it in Lua. \$\endgroup\$ – Katenkyo Aug 13 '18 at 12:28
8
\$\begingroup\$

Take input through any reasonable format, as allowed by the challenge

Lua is far from the only language with this problem; many langs only give access to STDIN and arguments as raw strings, not doing any parsing at all. Some methods of inputting an array include:

  • Putting each item on its own line.
  • Formatting the list in array syntax and using eval or similar.
  • Asking for one item at a time, with a sentinal value such as the empty string to mark end of input.

I don't know that I've seen an answer taking each item as a separate argument, but it seems fairly similar to the last option, and I would consider it a reasonable method. You'll need to watch out for challenges with stricter input requirements, however.

Also, parsing the input is part of the fundamental challenge of golfing—take the input you're given and apply transformations to it to give the desired output, all with as little code as possible. No bytes need to be taken off an answer just because the language has an inconvenient input system; in some esoteric languages, such as Brainf***, this can be half the challenge.

| |
\$\endgroup\$
  • \$\begingroup\$ Thank you. What I think I'm going to do is to use separate arguments and make sure to say clearly that I'm using this method. That way the user who made the question/moderators/... can correct me if they're not satisfied. And if I feel like the challenge requirements are really strict, then I'll draw even more attention to this fact or try to do the deserialization while counting the bytes. \$\endgroup\$ – Visckmart Aug 1 '18 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .