32
\$\begingroup\$

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]
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – xnor Mar 18 '17 at 17:35
  • \$\begingroup\$ Yes, anywhere. I'll try to clarify that. \$\endgroup\$ – Dennis Mar 18 '17 at 17:36
  • \$\begingroup\$ What exactly do you mean "you cannot assume that the string representation will have a particular format"? \$\endgroup\$ – Dada Mar 18 '17 at 17:40
  • 2
    \$\begingroup\$ These are not jagged arrays - jagged arrays would have all numbers at the same depth, because only sizes vary, not element types. \$\endgroup\$ – Ørjan Johansen Mar 18 '17 at 18:41
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Ørjan Johansen Mar 18 '17 at 23:41

35 Answers 35

0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I think you can leave off the parameter type: codegolf.meta.stackexchange.com/questions/11223/… \$\endgroup\$ – Brian McCutchon Mar 21 '17 at 15:49
  • \$\begingroup\$ @BrianMcCutchon You can't, because it doesn't otherwise work in my REPL. \$\endgroup\$ – Tamoghna Chowdhury Mar 21 '17 at 18:01
  • \$\begingroup\$ You don't need it to be a standalone REPL line. Assign it to a variable, with the type specified in the variable declaration. \$\endgroup\$ – Brian McCutchon Mar 21 '17 at 21:01
  • \$\begingroup\$ 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. \$\endgroup\$ – corvus_192 Mar 22 '17 at 20:52
0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ The program doesn't work for [-1, 0, 0, 0] \$\endgroup\$ – Kritixi Lithos Mar 23 '17 at 9:41
  • \$\begingroup\$ I was able to golf it smaller and detect multiple zeros, but then discovered it also currently fails for []. \$\endgroup\$ – Dominic A. Mar 25 '17 at 3:18

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.