I have written a few challenges related to matrices, and common for all are that I use a format like the one below when representing the matrices, both in examples and in test cases:
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
This is likely a cumbersome format in many languages.
Challenge:
Write a program/function that can take a matrix on the form given in the top as input (directly copy/pasted from this post), and output the same matrix on all of the three other conventional formats shown below.
The input format:
Numbers separated by a varying number of spaces, and newlines to represent rows (see test cases).
- The number of spaces between numbers are not guaranteed to be consistent. You may however assume that the last digit in each column align (if that helps any).
- There can be both integers and floats, and they can be positive, negative or zero. A matrix will not contain integers and floats at the same time.
- You may assume that no number is longer than 10 characters, including the minus and decimal point for negative floats.
- You may assume that there are the same number of entries in each row and in each column.
- There won't be any empty input matrices, but there can be single numbers, or matrices with only one row or column.
- You may in these cases choose between the output formats shown in the test cases
Your program/function must handle the input if it's directly copied from this post and pasted into the interpreter (STDIN or as function argument or something equivalent). You may have whatever you like (brackets, quotation marks, parentheses) in front of, and/or after the matrix, but you must consider the matrix a sequence of characters that can't be altered (that includes the newlines).
To clarify: Assume your function/program is called f
and the matrix is:
1 -2
3 5
6 7
then you may give the matrix as function arguments like this (and infinitely many other options):
f(1 -2
3 5
6 7)
f([1 -2
3 5
6 7])
f("""1 -2
3 5
6 7""")
If your language can't, in any way, take the copy/pasted matrix as input then I'm afraid you have to pick another language.
The output format:
You should output the matrix on the following three formats (order doesn't matter):
[[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
{{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}}
[16, 2, 3, 13; 5, 11, 10, 8; 9, 7, 6, 12; 4, 14, 15, 1]
- You may separate the three outputs however you want (e.g. a newline)
- You must output the numbers using the same precision as the input (for instance, you must not trim the number of decimals, nor output integers as floats).
- The spaces are mandatory
- You must use
-
for negative numbers, not_
or similar.
Test cases:
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
----
[[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
{{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}}
[16, 2, 3, 13; 5, 11, 10, 8; 9, 7, 6, 12; 4, 14, 15, 1]
0.14778 0.27114 0.24415
0.45997 0.12287 0.67470
0.28945 0.37928 0.51887
----
[[0.14778, 0.27114, 0.24415], [0.45997, 0.12287, 0.6747], [0.28945, 0.37928, 0.51887]]
{{0.14778, 0.27114, 0.24415}, {0.45997, 0.12287, 0.6747}, {0.28945, 0.37928, 0.51887}}
[0.14778, 0.27114, 0.24415; 0.45997, 0.12287, 0.6747; 0.28945, 0.37928, 0.51887]
-0.0398301 0.2403455 -0.2253368 0.3565870 0.0605803 0.0830780
-0.3254422 -0.1185191 -0.2989537 0.1647319 0.3621135 0.2018815
-0.0022281 -0.3362287 -0.3568447 0.4419063 0.3801872 -0.2847033
---
[[-0.0398301, 0.2403455, -0.2253368, 0.3565870, 0.0605803, 0.0830780], [-0.3254422, -0.1185191, -0.2989537, 0.1647319, 0.3621135, 0.2018815], [-0.0022281, -0.3362287, -0.3568447, 0.4419063, 0.3801872, -0.2847033],]
{{-0.0398301, 0.2403455, -0.2253368, 0.3565870, 0.0605803, 0.0830780}, {-0.3254422, -0.1185191, -0.2989537, 0.1647319, 0.3621135, 0.2018815}, {-0.0022281, -0.3362287, -0.3568447, 0.4419063, 0.3801872, -0.2847033},}
[-0.0398301, 0.2403455, -0.2253368, 0.3565870, 0.0605803, 0.0830780; -0.3254422, -0.1185191, -0.2989537, 0.1647319, 0.3621135, 0.2018815; -0.0022281, -0.3362287, -0.3568447, 0.4419063, 0.3801872, -0.2847033]
0 4 1 0
0 0 -6 0
0 1 4 -3
2 0 0 8
0 0 0 0
----
[[0, 4, 1, 0], [0, 0, -6, 0], [0, 1, 4, -3], [2, 0, 0, 8], [0, 0, 0, 0]]
{{0, 4, 1, 0}, {0, 0, -6, 0}, {0, 1, 4, -3}, {2, 0, 0, 8}, {0, 0, 0, 0}}
[0, 4, 1, 0; 0, 0, -6, 0; 0, 1, 4, -3; 2, 0, 0, 8; 0, 0, 0, 0]
1
----
[1] (or [[1]])
{1} (or {{1}})
[1] (or 1)
1 2
----
[1, 2] (or [[1, 2]])
{1, 2} (or {{1, 2}})
[1, 2]
4
5
----
[[4], [5]]
{{4}, {5}}
[4; 5]
I'm fully aware of this, but in this challenge, the cumbersome I/O format is the whole point. The challenge will be all about formatting the output in some languages, while reading the input will be the hardest part in other languages.
Please do not be discouraged if reading the input is hard, those submissions might be the most interesting ones. Short isn't necessarily the same as impressive. And as always, explanations are encouraged!