0
$\begingroup$

Define a function g, which receives a list and returns True if that list represents a square array, and False otherwise.

I only can use Table, Map, and Apply.

Examples:

g[{{1,2,3},{4,5,6},{7,8,9}}] deverá retornar True
g[{{1,2,3},{4},{7,8}}] deverá retornar False
g[{{1,2,3},{4,5,6}}] deverá retornar False
g[{{1,2},1+x}] deverá retornar False

This is my code:

gl = 
  Function[w,
    Apply[
      And, 
      Table[If[w[[i]] == Length[w], True, False], {i, 1, Length[w]}]]]`

or

g = 
  Function[w,
    Apply[And, Map[Function[x, If[x == Length[x], True, False]], w]]]
$\endgroup$
  • $\begingroup$ 1) Statements like If[test, True, False] is always equal to simply test: Remember that If checks whether test == True. $\endgroup$ – Marius Ladegård Meyer Nov 27 '16 at 11:44
  • 1
    $\begingroup$ @MariusLadegårdMeyer Strictly speaking, your statement is not true. If test does not evaluate to True or False, then If remains unevaluated and is not equivalent to test (they are different expressions). $\endgroup$ – Leonid Shifrin Nov 27 '16 at 11:48
  • $\begingroup$ 2) When your test is w[[i]] == Length[w] or x == Length[x], you are asking whether the i'th row of w (generally a vector if w is a list of Depth 2 like in all the examples) is equal to the number of rows in w. What you want is to check that the number of columns is equal to the number of rows, right? Then Length[w[[i]]] will give you the number of columns in row i. $\endgroup$ – Marius Ladegård Meyer Nov 27 '16 at 11:49
  • $\begingroup$ @LeonidShifrin, of course, I forgot about that in this context sigh. Thanks for pointing it out :) $\endgroup$ – Marius Ladegård Meyer Nov 27 '16 at 11:52
  • 5
    $\begingroup$ Use SquareMatrixQ $\endgroup$ – Bob Hanlon Nov 27 '16 at 14:22
2
$\begingroup$

The only problem is that for a $ n \times 1$ matrix, Dimensions may b a singleton so

g = If[MatrixQ[#] && Length[Dimensions[#]] == 2 && 
    Dimensions[#][[1]] == Dimensions[#][[2]], True, False] &

Now if

l = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
g[l]

gives true and false for the other cases

$\endgroup$
  • $\begingroup$ The OP states "I only can use Table, Map, Apply..." though. $\endgroup$ – Marius Ladegård Meyer Nov 27 '16 at 15:02
  • $\begingroup$ True but the answer seems to satisfy Paulo Rodrigues $\endgroup$ – cyrille.piatecki Nov 27 '16 at 16:28
  • $\begingroup$ g[{{1, 2}, {3, {4, 5, 6, 7, 8}}}] returns True which seems wrong. $\endgroup$ – Simon Woods Nov 27 '16 at 20:27
  • $\begingroup$ Simon Woods, your Matrix is ambiguous and for Mathematica Dimensions[{{1, 2}, {3, {4, 5, 6, 7, 8}}}] si {2, 2}. $\endgroup$ – cyrille.piatecki Nov 27 '16 at 22:12
  • 1
    $\begingroup$ But {{1, 2}, {3, {4, 5, 6, 7, 8}}} // MatrixQ returns False, as does ArrayQ. So at least as far as Mathematica is concerned, Dimensions are not the only thing to take into account. $\endgroup$ – Michael E2 Nov 28 '16 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.