25
\$\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\$
4
  • \$\begingroup\$ How about arrays of 0 (false, all 0 bits) and -1 (true, all 1 bits)? \$\endgroup\$
    – Lynn
    Sep 15, 2016 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\$ Sep 15, 2016 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\$ Sep 15, 2016 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, 2016 at 23:32

78 Answers 78

1 2
3
0
\$\begingroup\$

 Common Lisp, 7

bit-not

Negate bits on a bit array.

Example

(bit-not #*00101010010100101001010010101)
=> #*11010101101011010110101101010
\$\endgroup\$
0
\$\begingroup\$

Swift(2.2) 44 bytes

I am using x as the input variable here. For example,

let x = [true,false]

¯\_(ツ)_/¯

Golfed

let y = {let b = $0.map({!$0});print(b);}(x)

unGolfed

let y = {
   let b = $0.map({!$0})
   print(b);
}(x)
\$\endgroup\$
0
\$\begingroup\$

PHP (231 Bytes)

function flip_bool_array($array) {
  $newarray = array();
  foreach ($array as $bool) {

   if ($bool === true) {
    $bool = false;
   } else {
    $bool = true;
   }

  $newarray[] = $bool;

}

return $newarray;

}

Explanation

The function accepts an array and loops over it testing each value for a true value and setting it to false, if the value is not true then it is set to true. Each boolean is added to a new array and returned.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site! You could take a ton of bytes off if you remove the extra whitespace, and shorten your variable names down to one letter each. Since the goal is just short code, readability is not important. Also, there's a great thread here for tips on golfing PHP code down further. \$\endgroup\$
    – DJMcMayhem
    Sep 16, 2016 at 21:28
0
\$\begingroup\$

Ruby, 30 bytes

gets.split(?,).map{|e|!eval e}

"true,false" outputs false,true.

\$\endgroup\$
1
  • \$\begingroup\$ @EasterlyIrk Sorry didn't know about that. Newbie here \$\endgroup\$ Sep 16, 2016 at 15:53
0
\$\begingroup\$

dc, 44 bytes

Append this to a line of space-delimited input and echo it into dc. Output is delimited with newlines and punctuated with error messages (due to the blatant abuse of k). If you quote the stuff, it'll give you even more errors. (When we re-vamp dc in the next version, we should probably do something about that.) I suggest pairing with a 2>nul or other system-specific hack for suppressing whiny programs.

0sz[1r-SAlz1+szz0<a]dsax[lzd1-szLApk0<a]dsax

Explained:

              # "ToS" := "top of stack"
0sz           # Initialise register `z' with 0: this will hold the length of our input
[             # Open macro definition
 1r-          #  Replace ToS with !(ToS): 1-1==0, 1-0==1
 SA           #  Store this result on the top of stack `A'
 lz1+sz       #  Increment z, our length counter
 z0<a         #  If the stack depth is positive, repeat (we still have input to negate)
]dsax         # Store a copy of macro in register `a' and execute it
              # At this point, our input has been negated and stored in A,
              #  first-in-first-out orientation
[             # Open macro definition
 lzd1-sz      #  Decrement z and keep a copy of it for later
 LA           #  Push ToS(A) onto stack; pop from A
 p            #  Peek at ToS
 k            #  `k' is for KILL THE TOP OF STACK BWAHAHAHA
 0<a          #  The value from register `z' is now on top: if positive, we still have
              #   values to print, so repeat macro
]dsax         # I'm in a chaotic mood, so let's just re-use `a'
\$\endgroup\$
1
  • \$\begingroup\$ If you prefer space-delimited output: s/pk/n32P/ \$\endgroup\$
    – Joe
    Sep 17, 2016 at 8:26
0
\$\begingroup\$

Emacs Lisp, 25 bytes

(lambda(x)(mapcar'not x))

Not very creative.

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

Racket 11 bytes

(map not l)

Testing:

(define l '(#t #f #t)) ; define a list of booleans

(map not l)

Output:

'(#f #t #f)
\$\endgroup\$
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\$

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
  • 1
    \$\begingroup\$ Yes, it is :) ...# \$\endgroup\$
    – Shaun Wild
    Sep 23, 2016 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\$
1
0
\$\begingroup\$

Zsh, 14 bytes

Assumes input is given as arguments. Try it Online!

for z;<<<$[!z]

Or without bending the rules so much, this uses array I/0 (26B):

for z ($A)B+=($[!z])
<<<$B
\$\endgroup\$
0
\$\begingroup\$

MMIX, 24 bytes (6 instrs)

00000000: 25010101 82ff0001 73ffff01 a2ff0001  %¢¢¢²”¡¢s””¢ɱ”¡¢
00000010: 5901fffc f8000000                    Y¢”‘ẏ¡¡¡

Requires at least one element. Pass array as first argument, length as second.

invarr  SUB  $1,$1,1        // loop: i--
        LDBU $255,$0,$1
        ZSZ  $255,$255,1
        STBU $255,$0,$1     // a[i] = !a[i]
        PBNN $1,invarr      // if(i >= 0) goto loop
        POP  0,0
\$\endgroup\$
0
\$\begingroup\$

Erlang, 43 38 bytes

n(Z)->lists:map(fun(X)->not X end,Z).

Many thanks to @Redwolf Programs for saving 5 bytes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't know Erlang, but can any of that whitespace be removed? \$\endgroup\$ May 14, 2021 at 21:46
0
\$\begingroup\$

Python 3, 26 bytes

lambda s:[~x+2 for x in s]

Try it online!

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

Lexurgy, 12 bytes

Input is a string of as for truthy and bs for falsy. These can be replaced with any two distinct characters, as a "proper" falsy value in Lexury would be an empty string, however since empty string is empty string, and infinite number of empty strings can be anywhere within a string, so two distinct characters are required.

a:
a=>b
b=>a

If you want numbers (16 bytes):

a:
\1=>\0
\0=>\1
\$\endgroup\$
1 2
3

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.