Is it true? Ask Jelly!

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 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]

Dennis

Posted 2017-03-18T17:25:56.480

Reputation: 196 637

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.797

CC @xnor Only the canonical representation has to be supported, so 03 doesn't have to be supported. – Dennis – 2017-03-18T17:52:07.347

Very 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[] is Object in most languages ;) – Qwertiy – 2017-03-18T22:14:29.603

2@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

Answers

38

Jelly, 3 bytes

ṭFẠ

F flattens the input list.

tacks on the original input list as an element, which is falsy if and only if it is empty.

then checks if any element in the flattened list, or the original list itself, is falsy.


(Original answer)

FẠ^Ṇ

Thanks to Dennis for encouraging finding a solution matching his.

FẠ gives 0 if the input contains a falsy value at any depth, else 1. This is what Ȧ does, except for empty lists.

gives 1 if the input is a falsy value, else 0. The only falsy list is the empty list.

XOR-ing the two gives the answer.


F;WẠ

This is much in the same spirit as Dennis's F;LẠ, but instead of using L to put a zero in the list when the list is empty, it uses W to put the empty list into itself (producing [[]]), making it contain a falsy element.

hvd

Posted 2017-03-18T17:25:56.480

Reputation: 3 664

30Outgolfed in my own challenge and my own language... Well done! – Dennis – 2017-03-19T23:08:53.263

15

Retina, 10 bytes

A`\b0
^...

Try it online!

First we remove the input if it contains a zero. The we try to match at least three characters from the beginning of the string (to ensure that the input hasn't been eliminated in the previous stage, or was only [] to begin with).

Martin Ender

Posted 2017-03-18T17:25:56.480

Reputation: 184 808

12

Ruby, 25 24 23 18 16 bytes

p$_!~/\D0|^..$/

Requires the -n flag on the command line (+1 byte, -e -> -ne).

Try it online!

This is a full program that takes input in Ruby's canonical array format on STDIN and outputs true or false on STDOUT.

 $_              # line of input that was read automatically (-n)
   !~/        /  # does not match the regex...
      \D0        #   a non-digit followed by a 0
         |       #   or...
          ^..$   #   a 2-length string (which must be [], the empty array)
p                # output the result

23 byte function version:

->a{"#{a}"!~/\D0|^..$/}

This is a proc that takes one argument, the array to be tested.

Thanks to Martin Ender for a byte and to Ventero for two bytes!

Doorknob

Posted 2017-03-18T17:25:56.480

Reputation: 68 138

You could save two more bytes by using p$_!~/\D0|^..$/ (or p ! ~/\D0|^..$/, yay significant whitespace) together with the -n flag. – Ventero – 2017-03-19T14:16:49.417

8

05AB1E, 9 8 bytes

-1 bytes thanks to Emigna

)Q¹˜0å~_

Explanation:

)Q        Is the length of the input 0?
  ~    _  ... NOR ... (just for you, Dennis) 
   ¹˜     Input deep flattened
     0å   Contains 0

Try it online!

Okx

Posted 2017-03-18T17:25:56.480

Reputation: 15 025

Seems to fail for [[]]. – Dennis – 2017-03-18T17:45:00.607

Is 0 really truthy in 05AB1E? – Dennis – 2017-03-18T17:46:45.777

*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* (emphasis mine) – Dennis – 2017-03-18T17:53:08.420

1@Dennis Alright, chucked in a logical negation byte there. – Okx – 2017-03-18T17:55:35.547

1Aw, just for me. :P – Dennis – 2017-03-18T17:56:49.380

You could replace g0 with ). – Emigna – 2017-03-18T22:52:09.777

8

Jelly, 4 bytes

FẠ_Ṇ

Try it online!

Ȧ yields 0 if the input is empty or contains a 0, otherwise it is 1.

FẠ yields 0 if the flattened input contains a 0, leaving only the edge case of an empty array (since the input is guaranteed to be an array).

is a non-vectorising logical not monad, and hence returns 0 for any non-empty list and 1 for the empty list. As such this can simply be subtraced from the result of FẠ using _.

Jonathan Allan

Posted 2017-03-18T17:25:56.480

Reputation: 67 804

One down, at least one more to go. – Dennis – 2017-03-19T00:07:28.923

@Dennis It isn't FẠạṆ, right? – Erik the Outgolfer – 2017-03-19T08:01:04.900

@EriktheOutgolfer No, it is not. The answer I have in mind deals different with the edge case of an empty array and would produce a different result for non-arrays. – Dennis – 2017-03-19T14:48:40.490

@Dennis Like returning A for True, B for false, C for empty and D for non-array? That would be non-competing. What I did is use absolute difference instead of difference because there are no negative booleans. – Erik the Outgolfer – 2017-03-19T14:56:41.230

@EriktheOutgolfer B must equal C to comply with the challenge spec, but D can be anything as the input is guaranteed to be an array. – Dennis – 2017-03-19T14:59:10.663

But, if I understand correctly, you seem to say that B ≠ C in your case ("deals different with the edge case of an empty array.") – Erik the Outgolfer – 2017-03-19T15:02:49.337

Sorry, I didn't see this when I posted my own answer using ^ instead of _, otherwise I would've just commented here. – hvd – 2017-03-19T20:01:25.027

@hvd Oh, no problem. I'm still wondering what the more obscure solution is (my guess is there is no in it). – Jonathan Allan – 2017-03-19T20:12:30.880

@JonathanAllan I did find two others with , one with length 4, and one with length 3. I don't know if either of those is what @Dennis had in mind. – hvd – 2017-03-19T22:54:06.963

7

Mathematica, 17 bytes

#!={}&&#~FreeQ~0&

FreeQ does the check against 0 for us, but of course it would return True for input {}, so we need to check that case separately.

Martin Ender

Posted 2017-03-18T17:25:56.480

Reputation: 184 808

7

APL (Dyalog), 21 12 7 bytes

Golfed 5 bytes thanks to Adám by using forks

⍬∘≡⍱0∊∊

Try it online!

This is my first try at Dyalog. Golfing tips are welcome!

Explanation

⍬∘≡                   Fork; Is the argument a null set
   ⍱                  Nor
    0∊∊               0 belongs to the enlisted form of the argument
                      For example, (1 (2 (3 (0)))) would become
                      1 2 3 0 using the ∊ monad
                      Then we check if zero belongs to this vector

user41805

Posted 2017-03-18T17:25:56.480

Reputation: 16 320

+1 Note that you are combining the results of two tests. This is perfect for a fork. ⍬∘≡ is the left test (empty-set bound-to identical-to), and 0∊∊ is the right test (itself a fork; zero member-of enlisted-form). Put it together: ⍬∘≡⍱0∊∊. Try it online!

– Adám – 2017-03-18T21:42:15.223

Also, you may want to use the name "APL (Dyalog)" so people can find what it is you are using. – Adám – 2017-03-18T21:43:44.487

@Adám Thanks for the tips! – user41805 – 2017-03-19T07:10:06.497

6

Operation Flashpoint scripting language, 199 188 bytes

A={f={private["_i","_r"];_r=1;_i=0;while{_i<count _this}do{o=_this select _i;if(o in [o])then{if(o==0)then{_r=0}}else{_r=o call f};_i=_i+1};_r};if(count _this==0)then{0}else{_this call f}}

Call with:

[3,6,4,[4,6],[3,6,[],[2,4,[0],3]]] call A

or with:

hint format["%1", [3,6,4,[4,6],[3,6,[],[2,4,[0],3]]] call A]

Explanation:

In the game's scripting language, any string containing code can be called. The curly braces {} denote the beginning and the end of a string. (Quotation marks work too, but that gets messy when they are nested.) So, A={...} assigns a string to variable A, and the variable can then be called like a function with: <argument> call A. Basically any string can be treated as a block of code.

Then, inside the "function" A, we define another function f. private declares the two variables _i and _r local to function f. A local variable's name has to start with an underscore.

while {} do {} is a loop, where the first string (denoted by {}) contains the code for the loop condition and the second one for the loop body.

_this is the argument that was passed with the call function. _this can be of any type, but here we assume it is an array.

In the loop, o=_this select _i accesses the _i:th element of the array and assigns it to variable o. if (o in [o]) is a trick to determine if the o is another array or not. If o is a number (or anything other than an array), o in [o] will evaluate to true, because the in function finds a value matching o from the array [o]. If o is an array, the expression yields false, because the in refuses to compare arrays.

If o is not an array, we check if it equals zero, and if it does, we'll set the variable _r, which we'll use as the return value, to zero. Otherwise, if o is an array, we assign to _r the return value of the recursive call to f with the new array o as the argument.

After the loop, at the end of function f, we evaluate the expression _r, which yields the value of _r, and as this is the last expression to be evaluated, this is what the call to function f returns.

Now that we have defined f (f need not be inside A, but this way we could have declared it a local variable/function (no difference really) of A if we didn't want to save some bytes), let's get back A. if (count _this == 0) checks if A's input array is empty, and if it is, A returns 0. Otherwise the function f is called and its return value will be A's return value.

One might notice that it seems that a semicolon would be missing from a few of places, but this is not the case, because a semicolon is only needed after a statement if another statement follows it inside the same block of code (i.e. string).

Steadybox

Posted 2017-03-18T17:25:56.480

Reputation: 15 798

Wait, what?! Operation Flashpoint? – Brain Guider – 2017-03-20T13:23:04.833

wat how? what??? confuzed – Christopher – 2017-03-20T15:30:57.133

@DownChristopher Added an explanation. – Steadybox – 2017-03-20T16:24:48.200

1@AnderBiguri Yep, why not? The game's scripting language fits the definition of programming language given in the meta post linked in the question. – Steadybox – 2017-03-20T16:25:53.683

1@Steadybox I am confused about the existence of the thing, not its validity!! – Brain Guider – 2017-03-20T16:35:07.537

5

Perl 5, 15 bytes

Saved 2 bytes by using the same technique as Doorknob's Ruby answer.

14 bytes of code + -p flag

$_=!/\b0|^..$/

Try it online!

/.../ ensures that the array isn't empty (it will match on any array but [].
/\b0/ will only match if there is a 0 in the array. (the \b ensures that the 0 isn't a part of another number but a whole number).

Dada

Posted 2017-03-18T17:25:56.480

Reputation: 8 279

5

Haskell, 48 bytes

f x=or[elem c"[,"|c:'0':_<-scanr(:)[]x]<(x<"[]")

Try it online!

Thanks to Lynn for the test cases and the x<"[]" trick.

The outer inequality requires (x<"[]") to be True (nonempty list) and or[elem c"[,"|c:'0':_<-scanr(:)[]x] to be False (no zeroes).

Characters of 0 are detected as following a , or [, as opposed to a number like 20. The expression scanr(:)[]x generates all suffices of l, and c:'0':_<- captures those whose second character is '0'. Then, elem c"[," checks whether the first character is , or [.

I assume here that Haskell-style lists don't have spaces, but if so ',' can just be replaced by ' '.

Here's a more direct 48-byte method, though it produces 0's and 1's which aren't Truthy/Falsey in Haskell.

f"[]"=0
f(c:'0':_)|elem c"[,"=0
f(_:t)=f t
f _=1

xnor

Posted 2017-03-18T17:25:56.480

Reputation: 115 687

5

Jelly, 4 bytes

F;LẠ

Try it online!

How it works

F;LẠ  Main link. Argument: A (array)

F     Flatten A.
  L   Yield A's length.
 ;    Concatenate.
   Ạ  All; Tell if all elements of the resulting 1D array are truthy.

Note that the Ạ atom behaves like Python's all and thus is rather different from the banned Ȧ.

Dennis

Posted 2017-03-18T17:25:56.480

Reputation: 196 637

8Heads-up: This isn't the only 4-byte solution Jelly, apart from the obvious L;FẠ. Who can find another one? – Dennis – 2017-03-18T19:54:50.590

4

JavaScript (ES6), 34 bytes

a=>a.length&&+!~`,${a}`.search`,0`

Test cases

let f =

a=>a.length&&+!~`,${a}`.search`,0`

console.log('Truthy:')
console.log(f([1]))
console.log(f([10]))
console.log(f([[]]))
console.log(f([[[[1]]]]))
console.log(f([[], [1], [1, 2]]))
console.log(f([[1], [1, [2]], [1, [2, [3]]]]))
console.log(f([[8], [8, [9]], [8, [9, [10]]]]))

console.log('Falsy:')
console.log(f([]))
console.log(f([0]))
console.log(f([0, -1]))
console.log(f([-1, 0]))
console.log(f([[[[0]]]]))
console.log(f([[0], [1, 2], [3, 4, 5]]))
console.log(f([[8], [8, [9]], [8, [9, [1, 0]]]]))

Arnauld

Posted 2017-03-18T17:25:56.480

Reputation: 111 334

You can probably use !!a[0] instead of a.length. (You don't have to worry about a[0] being zero as the result must be false in this case anyway.) – Neil – 2017-03-18T23:54:30.623

Never mind, I saw Qwerty already got there. – Neil – 2017-03-18T23:57:11.270

4

Grime, 16 14 11 bytes

Thanks to Zgarb for saving 5 bytes.

e`s\0v#|!..

Try it online!

The e tells Grime to try and match the entire input and print 0 or 1 depending on whether that's possible.

The |! is effectively a "neither" operator, because x|!y is shorthand for (x|y)!. So we make sure that the input neither contains a zero preceded by a symbol nor is a string of only two characters ([]).

A note about the second half: P# matches a rectangle that contains at least one match of P. However, in our case P consists of both s and \0 so that would normally require parentheses: (s\0)# (because the precedence of # is too high). But Grime has a really neat feature where you can modify the precedence of operators with ^ and v. So by using v# we lower #'s precedence so that it's lower than that of any other operator (including concatenation), which lets us save a byte on the parentheses.

Martin Ender

Posted 2017-03-18T17:25:56.480

Reputation: 184 808

4

Julia, 45 bytes

a(x)=all(a,x);a(x::Int)=x!=0;g(x)=x!=[]&&a(x)

This creates a function g that indicates whether Ȧ would be 1 or 0 by calling a recursive function a. To make a suitable a, we use multiple dispatch:

# A method for scalar values
a(x::Int) = x != 0

# A recursive fallback for arrays
a(x) = all(a, x)

The function all takes a function argument, so we're calling a on each element of the input. Then we simply define the function for the submission as

g(x) = x != [] && a(x)

Basically we just need a but with a check to correctly handle [].

Try it online!

Alex A.

Posted 2017-03-18T17:25:56.480

Reputation: 23 761

can you define the function a(x) or g(x) as !x instead? – Cyoce – 2017-03-20T18:24:21.600

3

Haskell, 62 bytes

import Data.List
(%)=isInfixOf
f x=not(",0"%x||"[0"%x)&&x<"[]"

Try it online!

This is a function String -> Bool. Haskell’s lists are heterogenous, so there’s no built-in way to represent lists like [0, [0]].

Lynn

Posted 2017-03-18T17:25:56.480

Reputation: 55 648

Based on the re-worded rules, the inputs should not have spaces because Haskell arrays don't by default. At least, I think that's the interpretation even though Haskell doesn't allow jagged arrays. But it looks like your code would work the same with , for . – xnor – 2017-03-18T18:40:11.833

2As I'm quibbling up in the question comments, Haskell does have jagged arrays (and lists) - it's just that it's not really enough for what this question requires. – Ørjan Johansen – 2017-03-18T23:44:26.513

3

Wonder, 15 bytes

@&#0! iO0flat#0

Usage:

(@&#0! iO0flat#0)[1;2;3;[8;9;0]]

Flatten input, get all occurrences of 0, logical NOT, logical AND with input.

Mama Fun Roll

Posted 2017-03-18T17:25:56.480

Reputation: 7 234

3

Python 2, 45 39 38 bytes

lambda a:(a>[])^(' 0'in`a`or'[0'in`a`)

Try it online!

-6 thanks to @BenFrankel


previous version, without converting list to string repr, 68 bytes:

lambda a:(len(a)and g(a))*1
g=lambda b:all(map(g,b))if b>[]else b!=0

ovs

Posted 2017-03-18T17:25:56.480

Reputation: 21 408

This gives a false positive on []. The following saves 6 bytes and succeeds on []: lambda a:bool(a)^(' 0'in\a`or'[0'in`a`)` – Ben Frankel – 2017-03-18T23:30:29.390

3

Pip, 12 bytes

#Va&`\b0`NIa

Takes the array as a command-line argument in Pip's repr form, like [1;[2;3]]. Returns 1 for truthy, 0 for falsey. Try it online or verify all test cases.

Explanation

              a is 1st cmdline arg (implicit)
 Va            Eval a (converting from a string to a list)
#              Take the length (0 if empty, nonzero if nonempty)
   &          Logical AND
    `\b0`      Regex pattern: word boundary followed by 0 (avoids things like 10)
         NIa   Not in a (0 if `\b0` matches in a, 1 if it doesn't)
              Autoprint

Bonus answer, 12 bytes

Here's a function that takes a list instead:

#_>0=0N_Js^s

#_            Len(arg)
  >0          is greater than 0
    =         which also equals the following (comparison operators chain like Python's):
     0N       Count of 0's in
       _Js^s  arg, joined on space and then split on space (a hacky way to flatten)

TIO

DLosc

Posted 2017-03-18T17:25:56.480

Reputation: 21 213

3

Röda, 59 44 bytes

f a{[1]if{g(a)[[]!=a]}}g a{[a!=0];try a|g _}

Try it online!

f takes the input from its stream as a list that can contain other lists and integers. It returns 1 if a is truthy and nothing otherwise. The helper function g checks if a contains zeros.

Explanation:

f a{[1]if{g(a)[[]!=a]}}
f a{                  } /* Function declaration */
          g(a)          /* Call g -> pushes some booleans to the stream */
              [[]!=a]   /* Push value []!=a to the stream */
       if{           }  /* If all booleans in the stream are true: */
    [1]                 /*   Push 1 to the stream */
                        /* Otherwise return nothing */

g a{[a!=0];try a|g _}   /* Helper function */
g a{                }   /* Function declaration */
    [a!=0];             /* Push value a!=0 to the output stream */
           try          /* Ignore errors in the following if a is not a list */
               a        /* Push values in a to the stream */
                |g _    /* Pull values from the stream and */
                        /*   call g for each of them */
                        /*   (pushes boolean values to the output stream) */

A solution that makes use of regexes could very likely be shorter.

This answer could have been shorter if it were allowed to return multiple values. This has been discussed in one of my answers before, and it was concluded that it is allowed in the default rules to return different truthy and falsy values for different inputs, but for some reason OP forbid it here and there. :(

fergusq

Posted 2017-03-18T17:25:56.480

Reputation: 4 867

2

MATLAB, 49 bytes

As MATLAB (as well as Octave) does not allow these kind of nested arrays, we interpret it as a string.

First we replace all non-digit characters with a space. Then we use str2num to convert it to an (1D) array, on which we can apply all (which is allowed, as it does not completely solve this task by itself.)

s=input('');s(s<45|s>57)=32;disp(all(str2num(s)))

flawr

Posted 2017-03-18T17:25:56.480

Reputation: 40 560

2

egrep, 7+3=10 bytes

\<0|^.]

+3 bytes for the required -v flag to invert the result.

Grep doesn't have any concept of arrays, so this uses a string representation as given in the question. Takes input on one line from stdin, returns via the exit code (ignore stdout).

(Now using a version which doesn't account for 01 and similar, since word-of-god is that it's OK)

Original bash/grep entry:

grep -Ev '\<0+\>|^.]'

Finds 0s anywhere (using the word boundary checks \< and \> to discount things like 10 or a1), or a whole string matching [], then inverts the match.

Breakdown:

grep
     -E \    # extended regular expression syntax
     -v \    # invert match
     \<0+\>  # a number of 0s with alphanumeric boundaries on both sides
     |^.\]   # or ']' as the second character (implies '[]')

Dave

Posted 2017-03-18T17:25:56.480

Reputation: 7 519

Not cheating, just good golfing. :) Btw, grep is capable for primality testing, so it is a programming language as far as PPCG is concerned. \<0\|^.] plus -v would count as an 11 byte solution. – Dennis – 2017-03-18T21:44:35.093

1@Dennis cool, thanks! (I switched to egrep rather than grep to save an additional byte; language name doesn't count towards byte-count!) – Dave – 2017-03-18T22:09:05.330

2

Javascript ES6, 24 chars

Works with array, returns 1 or 0:

a=>!!a[0]&!/\b0/.test(a)

Test:

f=a=>!!a[0]&!/\b0/.test(a)

console.log([
  [1],
  [10],
  [[]],
  [[[[1]]]],
  [[], [1], [1, 2]],
  [[1], [1, [2]], [1, [2, [3]]]],
  [[8], [8, [9]], [8, [9, [10]]]],
].every(x => f(x)===1))

console.log([
  [],
  [0],
  [0, -1],
  [-1, 0],
  [[[[0]]]],
  [[0], [1, 2], [3, 4, 5]],
  [[8], [8, [9]], [8, [9, [1, 0]]]],
].every(x => f(x)===0))

Qwertiy

Posted 2017-03-18T17:25:56.480

Reputation: 2 697

Since the return value can be truthy/falsy, you can drop the !! (though then you must change & to &&). Saves one byte. – Brian McCutchon – 2017-03-21T03:19:22.017

@BrianMcCutchon, no as there is a binary &. In case of && without !! consistent output will be broken: undefined for [],0 for [0] and [0,1,2] and false for others. – Qwertiy – 2017-03-21T04:30:05.180

I don't see how breaking consistent output is bad in this challenge. My point with switching to && is that you would need to if you take my first suggestion, since 2 & 1 == 0. – Brian McCutchon – 2017-03-21T04:41:46.633

@BrianMcCutchon, the first point of the question: "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." – Qwertiy – 2017-03-21T04:44:07.397

Ah, I skimmed that too quickly. Never mind. – Brian McCutchon – 2017-03-21T04:45:41.933

2

√ å ı ¥ ® Ï Ø ¿ , 12 4 bytes

i0Bu

Explanation

i            › Take input as a list and automatically flatten it. If empty, push 0.
 0           › Push 0 to the stack
  B          › Pop 0 and push the number of times it appears
   u         › convert top value to its boolean 

If result needs to be outputted ...

i0Buo        › same as above; o outputs the top value on the stack

Previous solution

I had posted this before realising that stack based languages could leave the value on the stack as a form of output

i0B¿0oP?!¿o?

Explanation

i            › Take input as a list and automatically flatten it. If empty, push 0.
 0           › Push 0 to the stack
  B          › Pop 0 and push the number of times it appears
   ¿         › If the top value is true ...
    0        › Push 0
     o       › Output the top value on the stack
      P      › Pop the top value from the stack
       ?     › End if statement
        !    › Boolean opposite of top value
         ¿   › If the top value is true ...
          o  › Output the top value
           ? › End if statement

caird coinheringaahing

Posted 2017-03-18T17:25:56.480

Reputation: 13 702

2

Haskell, 45

As Lynn and xnor remarked, Haskell does not come with a heterogeneously-nested list type. But it's easy to add them as a custom data type and let the function operate on that type, and this is much preferrable to operating on (urgh!) strings.

data L=L Int|T[L]
f(L n)=n/=0
f(T l)=all f l

To actually be able to write out such lists as literals with [1, [2]] syntax, you also need some typeclass fu. Full test case:

{-# LANGUAGE OverloadedLists, TypeFamilies #-}
import GHC.Exts (IsList(..))

instance Num L where
  fromInteger = L . fromInteger
  negate (L n) = L $ negate n
instance IsList L where
  type Item L = L
  fromList = T
main = mapM_ (print . f) (
                    [ [1]
                    , [[[[0]]]]
                    , [[8], [8, [9]], [8, [9, [1, 0]]]]
                    ] :: [L])

Try it online!

ceased to turn counterclockwis

Posted 2017-03-18T17:25:56.480

Reputation: 5 200

2

Vim, 23 bytes

:g/0\|^..$/d
:s/.\+/1/<CR>

Try it online!

Outputs an empty string for false, or 1 for true. This could be shorter if I can output an empty string or [] for false (both of which are falsy values in vim).

user41805

Posted 2017-03-18T17:25:56.480

Reputation: 16 320

1

Stacked, 20 bytes

:size\flat,0 eq none

Try it online!

Alternatively, using a string:

:tostr'\d+'match'0'has¬\size¬>

Try it online!

Conor O'Brien

Posted 2017-03-18T17:25:56.480

Reputation: 36 228

1

Lithp, 74 bytes

(def f #L::((foldl(flatten L)(?(>(length L)0)1 0)#N,A::((?(== N 0)0 A)))))

Try it online!

Well, this turned out longer than I'd hoped. The [] case tripped me up and added a few bytes. It simply flattens the list and does a fold left over it, and if it finds a 0 it sets the accumulator to 0.

Andrakis

Posted 2017-03-18T17:25:56.480

Reputation: 361

1

Ruby, 24 22 bytes

->a{a[0]&&a*?!!~/\b0/}

Try it online!

Yes I know there's a better solution in Ruby but I wanted to find one taking the array in input instead of a string.

G B

Posted 2017-03-18T17:25:56.480

Reputation: 11 099

1

tinylisp, 70 64 bytes

(load library
(d _(q((X)(i(c()X)(all(map _ X))X
(q((L)(i L(_ L)0

The last line is an unnamed lambda function that takes a list and returns 1 for "truthy-under-Ȧ" and 0 for falsey. Try it online!

Ungolfed

(load library)

(def _Ȧ
 (lambda (val)
  (if (equal? (type val) List)
   (all (map _Ȧ val))
   val)))

(def Ȧ
 (lambda (ls)
  (if ls
   (_Ȧ ls)
   0)))

The recursive helper function does most of the work. If its argument is a list, we map to its elements and return 1 if they are all truthy, 0 if any are falsey. (Conveniently, all returns 1 when given the empty list.) Otherwise, the argument must be an integer; we return it as-is (0 is falsey and all other integers are truthy in tinylisp).

The main function Ȧ checks if the list is nonempty. If so, it calls ; if not, it returns 0.

The golfed version takes advantage of some undefined behavior: rather than using (e(type X)List) to test whether X is an integer or a list, it does (c()X), which attempts to cons (prepend) the empty list onto X. If X is a list, this results in a nonempty list, which is truthy. If X is an integer, tinylisp outputs an error message and returns an empty list, which is falsey. Since stderr is ignored, this approach is valid.

DLosc

Posted 2017-03-18T17:25:56.480

Reputation: 21 213

0

PHP, 63 54 bytes

9 bytes saved by @user63956

function a($a){return$a&&!strpos(print_r($a,1)," 0");}

takes an array as input; returns true or false: If $a is not empty,
check if print_r output contains a 0 value.

array solution, 83 bytes

function b($a,$r=0){$r|=$a;foreach($a as$v)$r|=is_array($v)?b($v,1):!!$v;return$r;}

recursive function returns 1 or 0.

breakdown

function b($a,$r=0)
{
    $r|=$a;         # if $a is not empty, set $r (no effect in recursion)
    foreach($a as$v)    # loop through elements:    
        $r&=is_array($v)    # 2. if test failed, clear $r
            ?b($v,1)        # 1. if array, recurse
            :!!$v;          #    else test element <>0
    return$r;           # return $r
}

Titus

Posted 2017-03-18T17:25:56.480

Reputation: 13 814

1You can save a few bytes with strpos(print_r($a,1)," 0") instead of preg_match(...). – user63956 – 2017-03-19T12:03:16.087

@user63956 ... and it also solves the 0-index problem. I wasn´t aware of the second print_r parameter. Great! – Titus – 2017-03-20T12:06:48.010

0

Scala, 53 47 44 bytes

A lambda (assign to a variable or value of type String=>Boolean, and call that variable/value).

x=> !(x=="[]"|x.split("\\D+") contains "0")

I collapsed the filtering logic by splitting the input string at any non-digit character, and then checking if the resulting sequence has a 0 anywhere. We do this via string manipulation because Scala (or Java) cannot represent arbitrarily jagged arrays uniformly at the type level.

I do not handle the case of an empty string "" as that is an illegal representation for a jagged list, the legal empty jagged list would be "[]", which has to be special-cased.

The input format is exactly the OP's input format.

Tamoghna Chowdhury

Posted 2017-03-18T17:25:56.480

Reputation: 373

I think you can leave off the parameter type: https://codegolf.meta.stackexchange.com/questions/11223/untyped-functions-in-static-langauges

– Brian McCutchon – 2017-03-21T15:49:55.773

@BrianMcCutchon You can't, because it doesn't otherwise work in my REPL. – Tamoghna Chowdhury – 2017-03-21T18:01:43.117

You don't need it to be a standalone REPL line. Assign it to a variable, with the type specified in the variable declaration. – Brian McCutchon – 2017-03-21T21:01:34.327

You can remove the parens around the parameter two save two bytes, and by using operator notation (dropping the dot before contains and the parens around "0") al least two more. – corvus_192 – 2017-03-22T20:52:10.540

0

CJam, 12 bytes

l~a_e_+:e&!!

Try it online! or verify all test cases

Explanation

l~            e# Read and eval a line of input
  a           e# Wrap the array in an array
   _          e# Duplicate it
    e_        e# Flatten it
      +       e# Concatenate; results in adding the input array to the beginning of itself
       :e&    e# Reduce the array using logical AND: if any element of the array is falsy,
              e#   the result will be that falsy element, otherwise a truthy element
          !!  e# Double negation (coerce to boolean)

Business Cat

Posted 2017-03-18T17:25:56.480

Reputation: 8 927

0

VIM: 16 keystrokes 28 keystrokes 26 keystrokes

Had to add a bunch of keystrokes to take care of multiple zeros and the empty array [].

i[] 0<enter><esc>A 0<esc>h#dd0%r602x<C-x>$hd0<C-a>

Expects the array to be in the first and only line of the file and the cursor to be on the first character. Leaves 0 for false and a 1 for true.

Explanation

  1. i[] 0<Enter><esc> Writes some stuff preceding the array
  2. A 0<esc> Writes some stuff preceding the array
  3. #dd search for the word "0", going backwards in the document and delete the line it's containing.
    • If 0 is in the array, this will stop on the second line.
      1. This leaves [] 0 on the first line, which is an empty array.
    • Else, this will stop on the first line, leaving the line (array) 0

At this point, all that's left to do is to identify the empty array and output 0 if it's present, otherwise ignore it.

The possible way for a 0 to be at the third position is if it is the empty array. Thus, it's just a problem for identifying if the third character is a 0.

  1. 3|a01<esc> Writes 01 after the third character
  2. 0df0 Deletes up to the first zero.
  3. lD saves the next character, then deletes the rest of the line.

Try it online!

Dominic A.

Posted 2017-03-18T17:25:56.480

Reputation: 533

The program doesn't work for [-1, 0, 0, 0] – user41805 – 2017-03-23T09:41:41.667

I was able to golf it smaller and detect multiple zeros, but then discovered it also currently fails for []. – Dominic A. – 2017-03-25T03:18:56.340

0

QBIC, 41 bytes

;?-(_lA|<3)+instr(A,@[0`)+instr(A,@,0`)=0

Maybe it's time to put instr on the to-do list... Explanation:

;                   Get a string from the cmd line
 -(_lA|<3)          Check the length ( _lA| ) and yield 0 for 
                    strings longer than 2, 1 for shorter ones;
                    this checks for the empty array []
 +instr(A,@[0`)     Then check if the string "[0" is present,
 +instr(A,@,0`)     or the string ",0"
                    If any of the above is true, these statements give a 1.
                    Those are summed: invalid arrays have a sum > 0
?              =0   Print a -1 for 'true' arrays, and '0' for false.

QBIC cannot do a list-based version of this challenge.

steenbergh

Posted 2017-03-18T17:25:56.480

Reputation: 7 772

0

Pyth, 9 Bytes

!|/.nQ0!l

Takes an array and outputs True or False.

The double ! (Not) is needed so that the output is consistently true or false.

Explanation:

!|/.nQ0!l
        l     - The length of the input array (input implicit on the right)
       !l     - Negate it: True if 0 else False
     Q        - Evaluate the input string to a array
   .nQ        - flatten the array
  /   0       - How often does 0 appear?
 |            - logical OR: If zero does appear return True else the length of the array
!             - Negate the result

Try it online!

more boring same length alternative:

!|}0.nQqY

KarlKastor

Posted 2017-03-18T17:25:56.480

Reputation: 2 352