7
\$\begingroup\$

If I were to pass your program an integer array as input, what format would you prefer?

I understand that this question is primarily opinion-based, but it is also incredibly useful information for those of us who want to write challenges that appeal to the largest number of people.

\$\endgroup\$
7
  • 5
    \$\begingroup\$ What the point. I won't feel bound by this when writing question. Picking a format can advantage some language and disadvantage others so the site should not have a uniform input style. Diversity is good. \$\endgroup\$ Jun 20, 2014 at 1:15
  • \$\begingroup\$ @dmckee I'm going to use the results of this question to format the inputs for my future challenges. I'm going to point - I said point, not force it down their throat - people here when they ask how to format their inputs for their future challenges. Anyone who wants to cater to popular opinion now has the ability to do so, but only because I asked this question. Anyone who wants to be creative or original can do that too. \$\endgroup\$
    – Rainbolt
    Jun 20, 2014 at 1:53
  • 3
    \$\begingroup\$ @dmckee I agree that the site should not have a uniform input style. I also welcome a list of input styles each with supporting reasons, so anyone who wants to try something different can use these answers for inspiration. I don't think your point invalidates this question. I expect it to be useful both for those aiming for mass appeal and for those aiming to be awkward... I think this site benefits from both. \$\endgroup\$ Jun 20, 2014 at 11:06
  • \$\begingroup\$ Why not say you can use any format on this page that has 2 or more votes? \$\endgroup\$
    – PyRulez
    Jun 23, 2014 at 13:56
  • \$\begingroup\$ @dmckee It seems to me you have input specification for something like code golf in mind. For KotH's and code challenges that need a controller program which calls the submissions and feeds them input on STDIN, you'd want the pick the format that is most convenient for most people, because you don't want participants to waste time on parsing their input when they want to work on their algorithm. I find this post very useful for cases like that. (And I think this is the context in which Rusher asked the question, too.) \$\endgroup\$ Jun 27, 2014 at 18:40
  • \$\begingroup\$ @PyRulez, because that implies that you can't use one which doesn't, which is excessively restrictive. \$\endgroup\$ Jun 28, 2014 at 13:48
  • \$\begingroup\$ @Peter Taylor I neither implied nor meant the converse. \$\endgroup\$
    – PyRulez
    Jun 29, 2014 at 18:10

8 Answers 8

12
\$\begingroup\$

Comma, Semicolon separated

1,2,3;4,5,6;7,8,9

The logical separation of comma-delimited lists (in the English language) is semicolons. And this format makes it easy (and roughly equal between languages) to parse, split on ; and ,. Additionally, it is easy to add more dimensions: simply add another delimiter.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Similar to .CSV, but the lack of a trailing semicolon makes it easier to split (no empty element at the end), and ; is one char shorter than \n \$\endgroup\$
    – Justin
    Jun 19, 2014 at 21:22
  • 1
    \$\begingroup\$ Not in Golfscript. In golfscript, n is two chars shorter than ';'. But then again, golfscript doesn't really need any benefits in this sense. \$\endgroup\$ Jul 1, 2014 at 19:17
11
\$\begingroup\$

.CSV

Terminated with a newline.

1,1,1\n2,2,2\n3,3,3\n

You can see the RFC at https://www.rfc-editor.org/rfc/rfc4180, but it looks barbaric. There is no "official" standard.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ +1 This is the only format listed right now that has only two delimiter characters. \$\endgroup\$
    – Geobits
    Jun 19, 2014 at 18:19
  • \$\begingroup\$ @Geobits, the ones using {} or [] in combination with either , or whitespace can also be considered to have two delimiters. You just have to ignore {[. \$\endgroup\$ Jun 19, 2014 at 18:30
  • 1
    \$\begingroup\$ @PeterTaylor Right, if you strip them out or ignore them, that works. If you want to use a simple split method, that requires more characters either way. I think CSV is the most language-agnostic format listed so far. Others probably are more readily parsed in certain languages, due to being an inbuilt/expected format. \$\endgroup\$
    – Geobits
    Jun 19, 2014 at 18:39
  • 1
    \$\begingroup\$ +1 for not privileging a particular language, or in general if parsing via some form of eval is forbidden. \$\endgroup\$ Jun 19, 2014 at 18:48
9
\$\begingroup\$

JSON

For example, this 3 by 3 array of integers:

[[1,2,3],[4,5,6],[7,8,9]]

See format at http://json.org/

\$\endgroup\$
5
  • 1
    \$\begingroup\$ +1 as a JavaScript user. ;) \$\endgroup\$ Jun 19, 2014 at 18:48
  • 1
    \$\begingroup\$ This can be harder to parse for some languages. For example, Befunge would have problems with this. \$\endgroup\$
    – Justin
    Jun 19, 2014 at 22:24
  • 2
    \$\begingroup\$ Some languages have default parsing for this. For example, in haskell, you use read, and in python you use eval. Single functions are nice. \$\endgroup\$
    – PyRulez
    Jun 23, 2014 at 13:55
  • \$\begingroup\$ @PyRulez Haskell won't like you for the "object" part of "javascript object notation", though. \$\endgroup\$ Jul 1, 2014 at 19:19
  • \$\begingroup\$ @JanDvorak Oh yeah. I think there are JSON parsing libraries any way though. Besides, on this site you usually don't have different types of objects on standard input together. \$\endgroup\$
    – PyRulez
    Jul 1, 2014 at 21:45
6
\$\begingroup\$

Newline-separated

1
2
3
4
5
6
7
8
9

This is the most generic format, and the easiest to parse in any programming language. Not all programming languages can easily split strings on a delimiter, but all of them can easily read a single line of input from stdin. The program just needs to read the number of lines it expects in a loop, and add each number to an array.

This format does not directly support nested arrays. If you want to represent a 2D matrix, add two more lines before the list with the number of rows followed by the number of columns, e.g. 3 and 3.

\$\endgroup\$
2
  • \$\begingroup\$ +1 for using nothing but newlines. I think that JSON and this answer are basically the two extremes of this spectrum. \$\endgroup\$
    – Rainbolt
    Jun 27, 2014 at 15:33
  • \$\begingroup\$ +1 This is the most natural format to read an array/list in sed and other similar languages. \$\endgroup\$
    – seshoumara
    Sep 24, 2016 at 9:02
4
\$\begingroup\$

Comma-Separated, Curly-Brace Wrapped

For example, this 3 by 3 array of integers:

{{1,1,1},{2,2,2},{3,3,3}}
\$\endgroup\$
1
  • \$\begingroup\$ +1 as a Mathematica user :) \$\endgroup\$ Jun 19, 2014 at 18:46
4
\$\begingroup\$

Having the answer dictate how an array is to be entered would be preferable since some have integrated parsers for certain formats while other don't.

Since it isn't here yet I'll add LISP lists and vectors (called proper arrays in other languages):

(1 2 3 4 5)  ; list
#(1 2 3 4 5) ; vector

You can easily nest them:

((1 2 3) (4 5 6))    ; list in list
#(#(1 2 3) #(4 5 6)) ; vector in vector (compatible)
#2A((1 2 3) (4 5 6)) ; 2 dimentional vector in CL
\$\endgroup\$
3
\$\begingroup\$

Space-separated, square-bracket wrapped

[[1 2 3][4 5 6][7 8 9]]

This is the format which GolfScript can evaluate most easily, so naturally GolfScript users will prefer it.

\$\endgroup\$
1
  • \$\begingroup\$ Per-language preferences aside, I think this is the most readable option. \$\endgroup\$
    – Dennis
    Jun 21, 2014 at 18:09
2
\$\begingroup\$

Space separated:

1 2 3 4 5 6 7 8 9

which would give the 3x3

1 4 7
2 5 8
3 6 9

(at least it will if you're a column-major programmer).

\$\endgroup\$
8
  • \$\begingroup\$ With this you also have to explicitly list dimensions, don't you? \$\endgroup\$
    – Geobits
    Jun 19, 2014 at 18:20
  • 1
    \$\begingroup\$ @Geobits: I would presume that you would know what size integer array is being fed in the first place. \$\endgroup\$
    – Kyle Kanos
    Jun 19, 2014 at 18:21
  • 1
    \$\begingroup\$ If it's something static like a tic-tac-toe board, you'd know. If it's "evaluate this MxN maze", you wouldn't. \$\endgroup\$
    – Geobits
    Jun 19, 2014 at 18:23
  • \$\begingroup\$ @Geobits: Easily taken care of using allocatable... \$\endgroup\$
    – Kyle Kanos
    Jun 19, 2014 at 18:23
  • 1
    \$\begingroup\$ I'm not sure how allocatable helps here. My point is that if I am given an MxN maze with 36 values in this format, I don't know up-front whether it's 36x1,18x2,12x3,9x4, or 6x6. Every other format listed makes it clear without having to explicitly give the dimensions. \$\endgroup\$
    – Geobits
    Jun 19, 2014 at 18:29
  • \$\begingroup\$ @Geobits: I suppose it would require knowing what the shape of the array should be \$\endgroup\$
    – Kyle Kanos
    Jun 19, 2014 at 18:32
  • 2
    \$\begingroup\$ The question says integer array, not array of arrays or integer matrix. This is the only valid answer I can see so far :) \$\endgroup\$ Jun 24, 2014 at 19:28
  • \$\begingroup\$ For two-dimensional arrays, newlines make pretty natural row delimiters, exactly as in your second example. Or, at least, that's what I initially assumed you were proposing. \$\endgroup\$ Jun 25, 2014 at 23:22

You must log in to answer this question.

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