84
\$\begingroup\$

Me thinks there aren't enough easy questions on here that beginners can attempt!

The challenge: Given a random input string of 1's and 0's such as:

10101110101010010100010001010110101001010

Write the shortest code that outputs the bit-wise inverse like so:

01010001010101101011101110101001010110101
\$\endgroup\$
0

141 Answers 141

1
2 3 4 5
92
\$\begingroup\$

J, 5 bytes

Assumes the input string is in variable b.

b='0'

This does not do what it would do in most languages...

The J comparison operator is just = (=: and =. are global and local assignment, respectively). However, = doesn't work like the normal == operator: it compares item-by-item. Keep in mind that an array is formed like this: 0 2 3 2 3 1 2 3 4. 2 = 0 2 3 2 3 1 2 3 4 gives 0 1 0 1 0 0 1 0 0 for example. This is similar for a string: 'a'='abcadcadda' doesn't just return 0, it returns 1 0 0 1 0 0 1 0 0 1 (This can be extrapolated to mean 0 with */, which basically means all.) In this case however, this behavior is excelent, since we want a string of ones and zeros, or true's and false's. Since J's bools are 1 and 0, this results in an array of 1's and 0's (They aren't strings, and every other character other than 1 would also result in 0 in this array.) This doesn't need printing: J automatically prints the result of an expression. I hope this was adequate explanation, if not, please ask for something that isn't yet clear in the comments. This answer also could've been '0'&= (or =&'0'), but I felt that b='0' was clearer.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ I have tried J. Can't bend my mind to it. Guess I haven't found good docs. Here, have a +1 for being a madman. \$\endgroup\$
    – seequ
    Jun 9, 2014 at 20:29
  • 1
    \$\begingroup\$ And this is why I will never use J. \$\endgroup\$ Jun 9, 2014 at 23:49
  • 2
    \$\begingroup\$ @Qix As unreadable as J is, this one actually makes sense to me, and other languages that have operators that take a vector LHS and a scalar RHS do behave similarly. \$\endgroup\$
    – hvd
    Jun 10, 2014 at 9:02
  • 1
    \$\begingroup\$ What? It doesn't return the right result, does it? The result should have been a text string (without spaces), but with my limited knowledge of J, I think this will return a boolean list with a display form of 0 1 0 1 0 ... \$\endgroup\$
    – Adám
    Sep 25, 2015 at 15:14
  • 5
    \$\begingroup\$ This isn't allowed because you can't assume the input is stored in a certain variable. =&'0' works for the same number of bytes. \$\endgroup\$ Jun 13, 2017 at 8:21
46
\$\begingroup\$

GolfScript, 5 bytes

{1^}%

Try it online.

How it works

  • GolfScript reads the entire input from STDIN and places it on the stack as a string.

  • {}% goes through all characters in the string and executes the code block for all of them.

  • 1^ computes the exclusive OR of the characters ASCII code with 1. “0” corresponds to the ASCII code 48, “1” to ASCII code 49.

    Since 48 ^ 1 = 49 and 49 ^ 1 = 48, this turns 0's into 1's and 1's into 0's.

  • Once finished, GolfScript prints the modified string.

\$\endgroup\$
6
  • 6
    \$\begingroup\$ Wait, golfscript? \$\endgroup\$ Jun 7, 2014 at 20:28
  • \$\begingroup\$ I had misinterpreted your question. Fixed now. \$\endgroup\$
    – Dennis
    Jun 7, 2014 at 20:44
  • 1
    \$\begingroup\$ @tolos: I've edited my answer. \$\endgroup\$
    – Dennis
    Jun 8, 2014 at 1:25
  • 5
    \$\begingroup\$ @ToonAlfrink Golfing languages such as GolfScript are accepted in all challenges, as long as they are 'general-purpose' meaning that they are not designed for specific challenges. \$\endgroup\$
    – kitcar2000
    Jun 8, 2014 at 13:05
  • 4
    \$\begingroup\$ @kitcar2000 I think he was more surprised that such a language existed, rather than shock of someone daring to use GolfScript in a code golf question ;) \$\endgroup\$ Jun 10, 2014 at 14:56
36
\$\begingroup\$

CJam - 4

q1f^

This xor's every character with 1.
Unlike the other CJam answer, I'm not assuming the input is already on the stack.

Try it at http://cjam.aditsu.net/

\$\endgroup\$
2
  • 2
    \$\begingroup\$ So that's how you use f. \$\endgroup\$
    – Dennis
    Jun 8, 2014 at 13:44
  • \$\begingroup\$ @Dennis Indeed. You can use the sf forum to ask questions btw :) \$\endgroup\$ Jun 8, 2014 at 15:47
33
\$\begingroup\$

x86 machine code on DOS - 14 13 11 bytes

Well, it did get shorter again! After writing a solution for an unrelated challenge, I noticed that the same trick could be applied even here. So here we go:

00000000  b4 08 cd 21 35 01 0a 86  c2 eb f7                 |...!5......|
0000000b

Commented assembly:

    org 100h

section .text

start:
    mov ah,8        ; start with "read character with no echo"
lop:
    ; this loop runs twice per character read; first with ah=8,
    ; so "read character with no echo", then with ah=2, so
    ; "write character"; the switch is performed by the xor below
    int 21h         ; perform syscall
    ; ah is the syscall number; xor with 0x0a changes 8 to 2 and
    ; viceversa (so, switch read <=> write)
    ; al is the read character (when we did read); xor the low
    ; bit to change 0 to 1 and reverse
    xor ax,0x0a01
    mov dl,al       ; put the read (and inverted character) in dl,
                    ; where syscall 2 looks for the character to print
    jmp lop         ; loop

Previous solution - 13 bytes

I think it doesn't get much shorter than this. Actually, it did! Thanks to @ninjalj for shaving off one more byte.

00000000  b4 08 cd 21 34 01 92 b4  02 cd 21 eb f3           |...!4.....!..|
0000000d

This version features advanced interactivity™ - after running it from the command line, it spits out the "inverted" characters as long as you write the input digits (which are not echoed); to exit, just do a Ctrl-C.

Unlike the previous solution, this has some trouble running in DosBox - since DosBox doesn't support Ctrl-C correctly, you are forced to close the DosBox window if you want to exit. In a VM with DOS 6.0, instead, it runs as intended.

NASM source:

org 100h

section .text

start:
    mov ah,8
    int 21h
    xor al,1
    xchg dx,ax
    mov ah,2
    int 21h
    jmp start

Old solution - 27 25 22 bytes

This accepted its input from the command line; runs smoothly as a .COM file in DosBox.

00000000  bb 01 00 b4 02 8a 97 81  00 80 f2 01 cd 21 43 3a  |.............!C:|
00000010  1e 80 00 7c f0 c3                                 |...|..|

NASM input:

    org 100h

section .text

start:
    mov bx, 1
    mov ah, 2
loop:
    mov dl, byte[bx+81h]
    xor dl, 1
    int 21h
    inc bx
    cmp bl, byte[80h]
    jl loop
exit:
    ret
\$\endgroup\$
3
  • 2
    \$\begingroup\$ +1 for code probably not many understand. \$\endgroup\$
    – Knerd
    Jun 10, 2014 at 15:41
  • 1
    \$\begingroup\$ xchg dx,ax is 1 byte shorter than mov dl,al \$\endgroup\$
    – ninjalj
    Nov 11, 2016 at 19:35
  • \$\begingroup\$ @ninjalj: woa, thank you! It did get shorter after all! \$\endgroup\$ Nov 14, 2016 at 22:30
26
\$\begingroup\$

Bash+coreutils, 8 bytes

tr 01 10

Takes input from STDIN.


Or

sed, 8 bytes

y/01/10/
\$\endgroup\$
11
  • 2
    \$\begingroup\$ I recently made a golfing library/alias set for Bash github.com/professorfish/bash-shelf. you can shave off one char with that: y 01 10 \$\endgroup\$
    – user16402
    Jun 8, 2014 at 6:56
  • 1
    \$\begingroup\$ Where is BASH involved here? What is BASH specific? Every shell can call tr... \$\endgroup\$
    – user19214
    Jun 8, 2014 at 10:48
  • 1
    \$\begingroup\$ @yeti Not every shell calls commands like bash or zsh. In some shells that code alone is a syntax error \$\endgroup\$
    – mniip
    Jun 8, 2014 at 11:20
  • 5
    \$\begingroup\$ It's probably safe to assume that "shell" means "POSIX-compatible shell" here... \$\endgroup\$
    – FireFly
    Jun 8, 2014 at 11:56
  • \$\begingroup\$ @professorfish you shave off one char, but then add 48 by including the function. How is that a win? \$\endgroup\$
    – Zombo
    Jun 11, 2014 at 0:50
21
\$\begingroup\$

CJam, 4 bytes

:~:!

Assumes the original string is already on the stack. Prints the modified string.

Try it online by pasting the following Code:

"10101110101010010100010001010110101001010":~:!

How it works

  • :~ evaluates each character of the string, i.e., it replaces the character 0 with the integer 0.

  • :! computes the logical NOT of each integer. This turns 0's into 1's and 1's into 0's.

\$\endgroup\$
21
\$\begingroup\$

Brainfuck (70 71)

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

Explanation:

>,[>,]                       Read characters until there are none left.
<[<]                         Return to start
>[<                          Loop as long as there are characters to invert
  +++++++[>-------<-]        Subtract 49 (ASCII value of 1)
  >[++<]                     If not 0, add 2
  +++[<++++>-]<[>>++++<<-]>> Add 48
  .                          Print
  [-]                        Set current cell to 0
>]                           Loop
\$\endgroup\$
4
  • 1
    \$\begingroup\$ ++++++++[<++++++>-] why not this for 48? 8*6 vs. 4*4*3 \$\endgroup\$
    – Cruncher
    Jun 10, 2014 at 13:30
  • \$\begingroup\$ @Cruncher Added. \$\endgroup\$
    – kitcar2000
    Jun 10, 2014 at 19:32
  • \$\begingroup\$ Why did it get longer? is that because of the "bug fixing"? \$\endgroup\$
    – Cruncher
    Jun 10, 2014 at 19:52
  • 1
    \$\begingroup\$ @Cruncher Yes, I had to fix a bug where it would output a for 11. \$\endgroup\$
    – kitcar2000
    Jun 10, 2014 at 20:15
17
\$\begingroup\$

PHP - 19 bytes

<?=strtr($s,[1,0]);

Yea, not really original, I guess!

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

Pancake Stack, 532 bytes

Put this tasty pancake on top!
[]
Put this delicious pancake on top!
[#]
Put this  pancake on top!
How about a hotcake?
If the pancake is tasty, go over to "#".
Eat all of the pancakes!
Put this supercalifragilisticexpialidociouseventhoughtheso pancake on top!
Flip the pancakes on top!
Take from the top pancakes!
Flip the pancakes on top!
Take from the top pancakes!
Put this supercalifragilisticexpialidociouseventhoughthes pancake on top!
Put the top pancakes together!
Show me a pancake!
If the pancake is tasty, go over to "".

It assumes the input is terminated by a null character. The strategy is as follows:

  • Take a character of input
  • Subtract the ascii value of 1 from it.
  • Subtract that from 0 (yielding a 1 if we had 0, or a 0 if we had 1)
  • Add the ascii value of 0 to it
  • Print the char.
  • Repeat
\$\endgroup\$
10
\$\begingroup\$

C: 29

i(char*s){*s^=*s?i(s+1),1:0;}

Try it online here.

Thanks for pointing out the XOR trick, Dennis.

\$\endgroup\$
7
  • 8
    \$\begingroup\$ Simpler & shorter: i(char*s){while(*s)*s++^=1;} \$\endgroup\$
    – edc65
    Jun 8, 2014 at 9:45
  • 1
    \$\begingroup\$ Thanks, @edc65! I'm not going to use that, though, since it's an iterative solution rather than a recursive one. I wouldn't want to take credit for it. It's worth noting that replacing your while with a for still results of a length of 28 characters. \$\endgroup\$
    – millinon
    Jun 9, 2014 at 13:47
  • 6
    \$\begingroup\$ As you prefer. A recursive solution is not requested, and in my opinion, any time it is possibile, an iterative solution is better than a recursive one. Have fun applying this recursive call to a 10k string. \$\endgroup\$
    – edc65
    Jun 9, 2014 at 14:24
  • 1
    \$\begingroup\$ Since every call except for the last one is a tail recursive call, I'll bet that a compiler will convert it into a loop in order to re-use the stack frame. \$\endgroup\$
    – millinon
    Jun 9, 2014 at 15:28
  • 1
    \$\begingroup\$ @millinon Proof! \$\endgroup\$
    – deed02392
    Jun 10, 2014 at 13:50
9
\$\begingroup\$

Python 2.7 – 34*

Oh how much this first one sucks. Pretty ugly, this one is. 63 chars.

print''.join([bin(~0)[3:] if x == '0' else bin(~1)[4:] for x in ''])

This one is a bit better but still not that fancy. 44 chars.

print''.join([str(int(not(int(x)))) for x in ''])

Since int(x) and 1 returns int(x) if it's not 0 and otherwise False. The solution can be further reduced to 36 chars.

print''.join([str(1-int(x)) for x in ''])

Since join() takes a generator the brackets can be removed. 32 chars.

print''.join(str(1-int(x))for x in'')

And backticks can be used instead of str()

print''.join(`1-int(x)`for x in'')

Reduced to 44 from 34 thanks to pointers from @TheRare

Finding one's complement is difficult in python since bin(-int) returns -0bxxx hence the above.

\$\endgroup\$
14
  • 3
    \$\begingroup\$ Ya'know, (int(x) and 1) == int(x) \$\endgroup\$
    – seequ
    Jun 8, 2014 at 10:47
  • \$\begingroup\$ @TheRare I didn't, thanks for that :) \$\endgroup\$
    – Bassem
    Jun 8, 2014 at 11:33
  • 1
    \$\begingroup\$ For the record: zero is false and nonzero is true. For any kind of sequence (list, string...) the same rule applies, but it's checked from the length of the sequence. Thus '' == False and 'hi' == True \$\endgroup\$
    – seequ
    Jun 8, 2014 at 11:45
  • 1
    \$\begingroup\$ Seems like you missed some spaces. Also, backticks can be used to replace repr(). ''.join(`1-int(x)`for x in'') \$\endgroup\$
    – seequ
    Jun 8, 2014 at 11:53
  • 1
    \$\begingroup\$ Fyi, repr(x) for x < maxint is equal to str(x) \$\endgroup\$
    – seequ
    Jun 8, 2014 at 12:03
8
\$\begingroup\$

Perl, 9 characters

'y/10/01/'

The 9th character is the 'p' flag

Usage:

$ echo '10101001' | perl -pe 'y/10/01/'
\$\endgroup\$
3
  • 2
    \$\begingroup\$ also works as a sed script y/10/01/ but one char shorter because it doesn't need any flags \$\endgroup\$
    – user16402
    Jun 8, 2014 at 6:52
  • 4
    \$\begingroup\$ You don't need single quotes here. \$\endgroup\$ Jun 8, 2014 at 7:12
  • 1
    \$\begingroup\$ You can save 1 extra byte, using ; as the separator: Try it online! \$\endgroup\$ Feb 3, 2022 at 9:35
7
\$\begingroup\$

Javascript (ES6) 36

alert(prompt().replace(/./g,x=>x^1))
\$\endgroup\$
7
  • \$\begingroup\$ Assuming input in s, s.replace(/./g,x=>x^1) are 22 chars. \$\endgroup\$
    – Oriol
    Jun 8, 2014 at 18:37
  • 2
    \$\begingroup\$ I like to actually output and input. \$\endgroup\$
    – nderscore
    Jun 8, 2014 at 19:38
  • \$\begingroup\$ @nderscore Save 2 chars: p=prompt(p().replace(/./g,x=>x^1)) \$\endgroup\$ Jun 9, 2014 at 5:21
  • \$\begingroup\$ @GaurangTandon it would have to be (p=prompt)(p().replace(/./g,x=>x^1)) and that's the same length. \$\endgroup\$
    – nderscore
    Jun 9, 2014 at 13:19
  • \$\begingroup\$ @nderscore I too thought it to be that way, but it worked without the parenthesis too, strangely. \$\endgroup\$ Jun 9, 2014 at 13:39
6
\$\begingroup\$

Labyrinth, 6 bytes

(Labyrinth is newer than this challenge, so this answer doesn't compete - not that it's winning anyway...)

1,
.$@

This code assumes that STDIN contains only the digits (in particular, no trailing newline).

The instruction pointer (IP) starts in the top left corner going right. While there are digits to read it will cycle in a tight loop through the left-hand 2x2 block: 1 push a 1, , read a digit, $ XOR it with 1 to toggle the last bit, . print the result. The IP takes this loop because the top of the stack is positive after the XOR, such that it will take a right-turn. When we hit EOF, , returns -1 instead. Then the XOR will yield -2 and with this negative value the IP takes a left-turn onto the @ and the program ends.

This solution should be optimal for Labyrinth: you need , and . for an I/O loop and @ to terminate the program. You need at least two characters (here 1 and $) to toggle the last bit. And you need at least one newline for a loop which can be terminated.

Unless... if we ignore STDERR, i.e. allow terminating with an error we can save the @ and we also don't need any way to switch between two paths. We just keep reading and printing until we accidentally try to print a negative value (the -2). This allows for at least two 5-byte solutions:

1,
.$
,_1$.
\$\endgroup\$
0
5
\$\begingroup\$

Ruby: 23

p $<.read.tr("01","10")
\$\endgroup\$
0
5
\$\begingroup\$

Turing Machine Code, 32 bytes (1 state - 3 colors)

Using the rule table syntax required by this online TM simulator. Borrowed from a post I made to my Googology Wiki user blog a few months back.

0 0 1 r *
0 1 0 r *
0 _ _ * halt

You may also test this using this java implementation.

\$\endgroup\$
4
\$\begingroup\$

R, 27 characters

chartr("01","10",scan(,""))

Usage:

> chartr("01","10",scan(,""))
1: 10101110101010010100010001010110101001010
2: 
Read 1 item
[1] "01010001010101101011101110101001010110101"
\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog Unicode), 7 bytesSBCS

Full program. Prompts stdin.

∊⍕¨~⍎¨⍞

Try it online!

 prompt stdin

⍎¨ execute each character

~ logical NOT

⍕¨ format each character as text

ϵnlist (flatten)

\$\endgroup\$
3
\$\begingroup\$

PHP > 5.4 -- 37 characters

foreach(str_split($s) as $v)echo 1^$v

$s is the input

Try it online

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Clever abuse of the <kbd> tag. \$\endgroup\$
    – alexia
    Jun 9, 2014 at 11:04
3
\$\begingroup\$

Python 2.x - 44 bytes

print''.join(`1-int(x)`for x in raw_input())

Why make it complex, or use some cheaty variables?

\$\endgroup\$
5
  • \$\begingroup\$ Its possible to save some extra chars like this: print''.join('1-int(x)'for x in'input()'). I couldn't get the backticks in the comment code so substituted them by '. \$\endgroup\$
    – Willem
    Jun 9, 2014 at 9:10
  • \$\begingroup\$ @willem For future reference, you can escape them with a backslash: `a\`b` -> a`b. \$\endgroup\$
    – alexia
    Jun 9, 2014 at 11:03
  • \$\begingroup\$ @willem That doesn't work for input beginning with 0 or input which is bigger than maxint (in base10). \$\endgroup\$
    – seequ
    Jun 9, 2014 at 14:01
  • \$\begingroup\$ Thanks @nyuszika7h and didn't think about those cases TheRare so your solution is good \$\endgroup\$
    – Willem
    Jun 9, 2014 at 14:18
  • \$\begingroup\$ @willem Real easy to forget about stuff like that. :) \$\endgroup\$
    – seequ
    Jun 9, 2014 at 14:26
3
\$\begingroup\$

TI-BASIC, 7 bytes

This is a function that takes a binary string (through Ans) as input and returns the output as an inverted (not reversed) string, as specified. For more help, you can read through list application by not( on the TI-BASIC wiki. I'm using the compiled version because it is smaller:

»*r>Õ¸r

In hex:

BB 2A 72 3E D5 B8 72

Explanation

»*r - Take function input as string and convert to list

> - Pipe given list to the next operators

Õ¸r - Return the inverse of the list

\$\endgroup\$
3
  • \$\begingroup\$ what are all of the spaces at the end of »*r>Õ¸r ? \$\endgroup\$
    – kitcar2000
    Jul 3, 2014 at 18:28
  • \$\begingroup\$ @kitcar2000 Oops, I used to have the HEX after that. But after I moved it, I forgot to remove the spaces... I have done it now. \$\endgroup\$
    – Timtech
    Jul 4, 2014 at 14:05
  • \$\begingroup\$ It should be useful to note that: 1. On the calculator this is displayed as expr(Ans:Returnnot(Ans; 2. Because the string is not separated by commas, and it does not start with a {, it will evalutate to an integer like 1000010011, not a list; 3. Return does not work the way you wrote it; 4. This gives output as a list, not a string. \$\endgroup\$
    – lirtosiast
    May 26, 2015 at 23:41
3
\$\begingroup\$

Haskell, 22 bytes

map(\c->"10"!!read[c])

I was surprised by the lack of Haskell solutions to this challenge, so here's one. It evaluates to a function that takes a string and returns its inverse.

Explanation

Nothing fancy here.

map(\c->             )  -- For each character c in the input string:
                  [c]   -- wrap c into a string,
              read      -- convert to integer,
        "10"!!          -- and index the string "10" with it.
\$\endgroup\$
3
\$\begingroup\$

Befunge 93, 25 bytes

0>~1+:#v_$>:#,_@
 ^   -1<

Assuming empty stack and EOF both read -1.

0 pushes a \0 as a null terminator

>~1+:#v_ is an input loop, it reads ascii, adds 1, checks for EOF+1=0,

^ -1< else subtracts 1 and leaves the pushed ascii value on the stack.

$>:#,_@ drops the extra copy of zero on top of the stack, then prints the binary string top to bottom

If empty stack reads 0, save 2 bytes with

>~:1+#v_$>:#,_@
^   -1<

A version around 15 bytes is possible using this same algorithm if EOF = 0, but I don't have such an implementation handy to test with.

\$\endgroup\$
3
\$\begingroup\$

Javascript ES6, 26 chars

s=>s.replace(/\d/g,x=>x^1)
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Why /\d/g not /./g \$\endgroup\$
    – l4m2
    Dec 15, 2017 at 18:40
3
\$\begingroup\$

Befunge-98 (PyFunge), 7 bytes

'a#@~-,

For every character, c, in the input, it prints the character with an ascii value of 94 - c, where 94 is the value of ‘0’ +’1’, or ‘a’

Try it online!

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

brainfuck, 24 bytes or 19 bytes

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

Try it online!

or

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

(this one requires an interpreter which will noop on < if the data-index is 0)

Both programs use 8-bit wrapping cells. The main part of the programs are the >++[->++[<]>-]>- and +[<[-->>]<--]< which do some rather convoluted things to flip the last bit of the number.

I'm happy I managed to beat some more 'real' languages like C and Python, not something that happens every day with bf ;)

\$\endgroup\$
2
  • \$\begingroup\$ If you added an initial > to that first one, so it wouldn't go out of bounds, that'd be a very nice solution. One byte shorter than mine, congrats! Works with EOF->0 or EOF->no change, and doesn't assume anything about cell size, with the specified inputs. Good stuff. \$\endgroup\$ Jan 20, 2020 at 7:24
  • \$\begingroup\$ @DanielCristofani Yeah I thought it was cool that we had two such different looking solutions of such similar size. The reason I didn't add a > at the beginning was just because I usually just use tio as my target implementation and it works there. \$\endgroup\$
    – KSab
    Jan 20, 2020 at 14:40
3
\$\begingroup\$

brainfuck, 26 bytes

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

This flips the last bit of each byte of input. (Assumes EOF -> 0, assumes nothing about cell size, doesn't go out of bounds to the left.)

\$\endgroup\$
2
\$\begingroup\$

Python3, 39

Methinks Python is not the best language for this. :)

for i in input():print(1-int(i),end='')

If you care about having a newline after the output, here's a 43-character alternative:

print(''.join("01"[i<"1"]for i in input()))
\$\endgroup\$
3
  • \$\begingroup\$ Code will work without the end='' just a , will do :) - unless you care about there being no spaces \$\endgroup\$ Jun 8, 2014 at 1:29
  • \$\begingroup\$ @BritishColour, no, that's Python2. Python3's print function requires tweaking the end parameter to suppress a newline at the end of each print. Also, according to OP's specification, I think I do care about there being no spaces. :) Thanks for the comment, though! \$\endgroup\$
    – DLosc
    Jun 8, 2014 at 1:46
  • \$\begingroup\$ Aww, cool, I did't know that! I mainly work in 2.7 :/ \$\endgroup\$ Jun 8, 2014 at 1:47
2
\$\begingroup\$

J - 11 chars

Boolean values in J are represented as the integers 0 and 1, which of course are also valid indices into arrays (in this case, the 2-character array '01')

'01'{~'0'&=
\$\endgroup\$
3
  • \$\begingroup\$ I think this answer is technically more correct than the top J answer, which outputs a boolean array instead of a string. \$\endgroup\$
    – gar
    Jun 10, 2014 at 11:49
  • \$\begingroup\$ I actually didn't notice the top-ranked J solution when I posted (don't know how I missed it, but I did). To be fair to that solution, it does explicitly say "this doesn't do what the other solutions do". BTW, another way to express this (literal output) solution in 11 chars is [:,":@=&''0'' . \$\endgroup\$
    – Dan Bron
    Jun 10, 2014 at 14:05
  • \$\begingroup\$ Hi, thanks for another code! I will be learning more J from you guys. About that statement, I think he was referring to the equal sign, that it compares each item instead of assignment. \$\endgroup\$
    – gar
    Jun 10, 2014 at 16:23
2
\$\begingroup\$

C#, 131 bytes

A little late to the party, but here's mine. :)

using System;class R{static void Main(string[]a){foreach(var v in a[0].ToCharArray()){Console.Write(int.Parse(v.ToString())^1);}}}
\$\endgroup\$
1
2 3 4 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.