32
3
Background
Inspired by Octave's (and, by extension, MATL's) very convenient interpretation of truthy/falsy matrices, Jelly got the Ȧ (Octave-style all) atom.
Ȧ takes an array as input and returns 1 if the array is non-empty and does not contain the number 0 (integer, float, or complex) anywhere in the tree structure; otherwise, it returns 0.
For example, the array [[]] is truthy because it is non-empty and contains no zeroes, but [[0]] is falsy because it contains a 0 at the innermost level.
Task
In a programming language of your choice, write a full program or a function that takes a possibly empty, possibly jagged array of integers as input and prints or returns a truthy or falsy value that indicates if Ȧ would return 1 or 0, respectively.
Your submission must abide to the following rules.
The truthy and falsy values must be consistent for all inputs, i.e, all arrays for which Ȧ returns 1 must map to the same truthy value, and all arrays for which Ȧ returns 0 must map to the same falsy value.
Since full programs can only take string representations of arrays as input, this is allowed. However, you must use the canocical representation of your language, as returned by
repr
or similar.In particular you cannot assume that the first element of the array will be preceded by a space.
If (and only if) your language cannot represent jagged arrays natively, you may take a string representation of the input, using the canonical syntax of any pre-existing programming language.
If your language has several ways of representing jagged arrays (e.g., lists and tuples), you only have to support one of them.
If your language has a built-in that is itself a valid submission to this challenge, you may not use it in your answer. All other built-ins are allowed.
You are encouraged to post answers using both array and string manipulation, even if one is significantly shorter than the other.
All standard code-golf rules apply.
May the shortest code in bytes win!
Truthy test cases
[1]
[10]
[[]]
[[[[1]]]]
[[], [1], [1, 2]]
[[1], [1, [2]], [1, [2, [3]]]]
[[8], [8, [9]], [8, [9, [10]]]]
Falsy test cases
[]
[0]
[0, -1]
[-1, 0]
[[[[0]]]]
[[0], [1, 2], [3, 4, 5]]
[[8], [8, [9]], [8, [9, [1, 0]]]]
[-1, 0, 0, 0]
Based on the test cases, do you mean "contain the number 0" to mean anywhere in the tree structure? That's not what I have guessed it meant. – xnor – 2017-03-18T17:35:09.173
Yes, anywhere. I'll try to clarify that. – Dennis – 2017-03-18T17:36:32.770
What exactly do you mean "you cannot assume that the string representation will have a particular format"? – Dada – 2017-03-18T17:40:04.177
@Dada Hopefully clarified. For example, you cannot assume that there will be a space before each element, e.g.,
[ 0, 1]
for Python. – Dennis – 2017-03-18T17:43:31.980@Dennis Yea, that sentence and the example you added clarified it, thanks – Dada – 2017-03-18T17:46:22.730
@Dennis Might the list string contain numbers like
03
? That would mess up some string-based methods. – xnor – 2017-03-18T17:47:56.797CC @xnor Only the canonical representation has to be supported, so
03
doesn't have to be supported. – Dennis – 2017-03-18T17:52:07.347Very convenient, but also very non-sensical interpretation. – Fatalize – 2017-03-18T18:13:22.660
2These are not jagged arrays - jagged arrays would have all numbers at the same depth, because only sizes vary, not element types. – Ørjan Johansen – 2017-03-18T18:41:23.847
@ØrjanJohansen,
Object[]
isObject
in most languages ;) – Qwertiy – 2017-03-18T22:14:29.6032@Qwertiy Right, "most" languages where "everything" is an
Object
... my favorite is Haskell, where it isn't. Nor in C, at least not in a way that allows you to mix arrays and ints safely. Both of those languages are perfectly capable of jagged arrays, but still cannot use them for this problem. – Ørjan Johansen – 2017-03-18T23:41:48.317@ØrjanJohansen On those cases, you could use string manipulation techniques :) – Matthew Roh – 2017-03-19T16:23:04.253
@MatthewRoh Not if you take the rules of this question literally. You're only allowed that if the language does not support jagged arrays natively. Which is really what my whole quibble is about - the rules don't say what they intend to say. – Ørjan Johansen – 2017-03-19T16:35:40.907
Would this be truthy: ["0"] – ev3commander – 2017-03-19T22:47:51.293
@ev3commander "takes a possibly empty, possibly jagged array of integers", so you don't need to deal with string input. – trichoplax – 2017-03-19T23:16:46.320
Dang it Dennis. I was going to use a builtin :P – Christopher – 2017-03-24T15:06:01.920
Is it okay if we have more than one output for false? – user41805 – 2017-03-25T06:15:42.860
@KritixiLithos From the spec: The truthy and falsy values must be consistent for all inputs, i.e, all arrays for which Ȧ returns 1 must map to the same truthy value, and all arrays for which Ȧ returns 0 must map to the same falsy value. – Dennis – 2017-03-25T12:04:50.867