19
\$\begingroup\$

A nice simple one

Input

Given a boolean array (Or an acceptable alternative), you can assume the array will never be more than 32 elements long.

[false, false, true, false, false]

Output

Invert every element of the array and output it.

[true, true, false, true, true]

Rules

  • You can write a full program or just a function
  • Standard loopholes apply
  • Shortest code in bytes, per language, wins!

Test cases

Input:
[true, false]
Output:
[false, true]

Input: //Example of acceptable alternative
[0,1,1]
Output:
[1,0,0]
\$\endgroup\$
  • \$\begingroup\$ How about arrays of 0 (false, all 0 bits) and -1 (true, all 1 bits)? \$\endgroup\$ – Lynn Sep 15 '16 at 11:52
  • 5
    \$\begingroup\$ @Lynn While it's the OPs decision, I'd say it should be up to whether your language considers though truthy/falsy. \$\endgroup\$ – Martin Ender Sep 15 '16 at 11:53
  • \$\begingroup\$ Related. (Given the simplicity of the core task, I'd say the differences in format are significant enough that these aren't duplicates.) \$\endgroup\$ – Martin Ender Sep 15 '16 at 12:28
  • 6
    \$\begingroup\$ More than code golf this looks to me like: what is the not operator in your favourite language? Additional points if it works on lists. \$\endgroup\$ – licorna Sep 15 '16 at 23:32

68 Answers 68

0
\$\begingroup\$

><> Fish - 9 (16?) Bytes

My second golf and my first in ><> Fish!

l?!;r0=nr

I'm not sure how to count bytes when flags are considered, but this solution works when running the python interpreter with the -v flag and then space-separated 1's and 0's. If this doesn't match spec, I can remove the answer.

Example

Input/Run:

python fish.py --code "l?!;r0=nr" -v 0 1 0 1 1

Output:

10100

If I'm not allowed to use flags, and instead need to take the input from the input stack (such as on https://fishlanguage.com/playground), the code becomes longer as I must read from the input stack (either 1 or 0 without any separation) until there are no more values, and then convert the char value of the input (1 becomes 49 and 0 becomes 48) to their decimal values before my comparison. I'm not sure how to count bytes for ><> but I believe this would be 16 bytes.

i:1+?!;f3*3+-0=n
\$\endgroup\$
0
\$\begingroup\$

dc, 47 bytes

?zsN0sI[z:az0<A]dsAx[lI1+ddsI;a1r-n32PlN>R]dsRx

The actual code to invert a boolean (0 or 1) is 1r-. The rest is for reading the list and printing it in the same order, which is quite hard because of the (LIFO) stack that dc uses to store the input.

Explanation:

?zsN0sI         # read input, LIFO, initialize N = nr_elements and I = 0 (iterator)
[z:az0<A]dsAx   # loop A: move the stack content into array 'a' (I add a[n] first,
                #then a[n-1], ..., then a[1] and a[0] is not used)
[lI1+ddsI;a     # loop R: increment iterator and push a[I] to stack (input order)
   1r-n32P         #invert value and print it with a space separator
lN>R]dsRx       #repeat (R)

Run:

dc -f invert_boolean.dc <<< "1 0 1 1"

Output: there is a trailing separator (space) at the end, but I hope that's ok

0 1 0 0 

Note: my answer is similar in concept to that of @Joe's, but there are differences in terms of I/O handling, stack manipulation (I use arrays) and lack of warnings. Do check his answer as well.

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

Brainfuck, 24 bytes

,[>++[->++[<]>-]>-.>>>,]

Try it online!

The same as my answer to a similar question.

Relies on 8-bit wrapping cells, the cell size might not matter (untested) but wrapping definitely is. The main part the program is the >++[->++[<]>-]>- does some rather convoluted things to flip the last bit of the number.

A shorter solution of 19 bytes is

,[+[<[-->>]<--]<.,]

but this requires the < to noop if the data index is 0, instead of the more typical implementation of going into negative indeces.

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

Brainfuck, 72 69 61 bytes

,>++++++++[<------>-]<>+<[->-<]>[<+>-]<>++++++++[<++++++>-]<.

Closest i managed to get as BF dosent have Array support

Formatting is stupid, i will fix it when i get a pc... Somehow it wont let me post it as a snippet

\$\endgroup\$
  • \$\begingroup\$ You could put all the lines together into one long line, it doesn't really matter for us (because with newlines, the total byte length is 78 bytes). \$\endgroup\$ – Qwerp-Derp Sep 22 '16 at 23:14
0
\$\begingroup\$

dimwit 8 bytes (non-competing)

R1,0}0,1

Replaces all 0's with 1's, and vice versa.

Takes input through a string of 0's and 1's, and outputs similarly. Hopefully that's acceptable ;)

Try it here!

\$\endgroup\$
  • 1
    \$\begingroup\$ Yes, it is :) ...# \$\endgroup\$ – Shaun Wild Sep 23 '16 at 18:56
0
\$\begingroup\$

SmileBASIC, 25 bytes

DEF l l
ARYOP 1,l,1,l
END

Explanation:

DEF 'create a function
    l 'named "L"
      l 'with 0 outputs and 1 input named "L"
ARYOP 'array operation
      1 'mode 1 (subtract)
       ,l 'set each element in L to...
         ,1 '1 minus...
           ,l 'the corresponding element in L
END 'end function definition
\$\endgroup\$
0
\$\begingroup\$

FALSE, 13 bytes

[49^$0>][-.]#

Input is a list of 0 and 1 characters.

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

Tcl, 31 bytes

proc N L {lmap b $L {expr !$b}}

Try it online!

\$\endgroup\$

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.