Array-of-array string conversion to and from matrix

2

2

Matrix challenges are popular. There's an issue of how to provide test cases. A common method is the row-wise array-of-arrays representation, such as:

[[1,2],[3,4],[5,6]] -> a 3 by 2 matrix

This is good with vertical space, but there's always the task of converting the test cases into something your language can use, and vice versa for producing test cases to this format.

So this is a challenge and also a repository of conversion functions that people can use when trying or composing matrix challenges.

Defining the format

A row string of length m is a comma-separated list of m ≥ 1 numbers, enclosed in square brackets.

A row-wise n×m array-of-arrays string is a comma-separated list of n ≥ 1 row strings of length m enclosed in square brackets with the following whitespace rules:

  • has no whitespace before the first [ or after the last ], except for an optional trailing newline.
  • otherwise may contain spaces

The challenge

Produce two* programs/functions in your language:

  1. one which takes a rowwise n×m array-of-arrays string as an input, and outputs the matrix (as implemented in the language.)

  2. another one which takes an n×m matrix (as implemented in the language) as an input and outputs the rowwise n×m array-of-arrays string.

*It is allowed for one program/function to perform both tasks and you can have helper code used by both conversion functions.

Rules

  • To be clear: the language must support matrices (or something equivalent like 2D arrays - the name is not important) because the output/input of the two tasks needs to be an actual matrix in the language.
  • This is .
  • Fewest total number of bytes (from all functions and any helper code) in each language wins.
  • If one program or function does both conversions, only count those bytes once.
  • Standard rules apply/loopholes forbidden.

Test strings

[[1]]
[ [ 1, 2 ], [ 3, 4 ] ]
[[1.2,3.4,5.6],[3,2,1], [10, 11, 12.1]]

Not really possible to provide test matrices.

A non-golfed, possibly not bug-free worked example

I hope this doesn't violate the "don't answer your own question" guideline.

The R function I use to convert from string to matrix

The R function I use to convert from matrix to string

Here's the Sandbox and a related question.

ngm

Posted 2018-06-01T16:10:44.333

Reputation: 3 974

Question was closed 2018-06-01T19:01:10.780

So are either of the inputs actually strings? ("A row string of length m is a comma-separated list...A row-wise n×m array-of-arrays string is a comma-separated list" - I'd expect these to be flat strings as if copied from a challenge). – Jonathan Allan – 2018-06-01T16:38:31.977

1

...i.e. would Python have a valid 7-byte total submission of eval and str? TIO

– Jonathan Allan – 2018-06-01T16:55:31.133

1Can you please add some test cases? – Shaggy – 2018-06-01T17:22:37.640

... And a worked example? – Shaggy – 2018-06-01T17:34:06.367

@JonathanAllan One input is a string. The other input is a matrix. The term "list" was used colloquially, and not meant to imply any meaning of the word "list" in a programming or computer science sense. I'm happy to use another term. – ngm – 2018-06-01T17:35:44.950

@Shaggy I can give some example strings for the string-to-matrix conversion. As for worked examples, I could post TIO links to the (non-golfed, and not completely tested) R code I use personally. Is that what you mean? – ngm – 2018-06-01T17:39:13.843

What does it mean to "output a matrix” if I use STDOUT? Should/can it be a string representation of a matrix? – Luis Mendo – 2018-06-01T18:00:28.920

@LuisMendo the return value of the function/program needs to be a matrix "as implemented in the language". So if whatever gets printed to STDOUT could be used as an input to another function in your language that is expecting a matrix, that's fine. – ngm – 2018-06-01T18:07:43.763

So, does a list of lists constitute a matrix? If so, then it's really just 4 bytes in Jelly: ŒṘ to return a string, ŒV to eval it. If not, Jelly is out of the question (but that shouldn't really matter, there are still languages which do have a concept of "matrices"). – Erik the Outgolfer – 2018-06-01T18:10:28.283

@EriktheOutgolfer I think a list of lists would qualify as "something equivalent" to a matrix, even if it can be more general than a matrix. If your language participates in matrix challenges using lists of lists, that satisfies the spirit of this one. – ngm – 2018-06-01T18:15:28.680

@ngm My point is: anything output by a program in STDOUT is a string. It cannot be a matrix; only a string representing a matrix. So is that acceptable? – Luis Mendo – 2018-06-01T21:10:41.623

Answers

0

Japt, 30 bytes

F=_OxZ}G=_Oo'[+Zm_'[+Z+']} +']

Try it online!

Function F converts a string into a matrix, and function G converts a matrix into a string.

Logern

Posted 2018-06-01T16:10:44.333

Reputation: 845