-7
\$\begingroup\$

Description :

Given a string of space separated binary digits or space separated booleans or an array of binary digits or array of booleans . Your job is to find the xor of each until you end up with one answer either 0 or 1. The inputs will always be valid and will only be either 0 or 1.

Example :

1 0 0 1 0 --> 0
1 0 1 1 1 0 0 1 0 0 0 0 --> 1

This is code golf so shortest code will win. Good luck.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Your job is to find the xor of each until you end up with one answer either 0 or 1 Can you clarify a bit more what that means? \$\endgroup\$
    – Luis Mendo
    Apr 13, 2018 at 13:44
  • \$\begingroup\$ given a string find the xor of first two then the xor of next two and so on. keep at it till you end with one digit. \$\endgroup\$ Apr 13, 2018 at 13:45
  • 15
    \$\begingroup\$ Use the Sandbox! That's 3 challenges in a row that you propose that have troubles. Go to the sandbox, expose your challenge and update it based on community feedback. Then only post it on this site. \$\endgroup\$ Apr 13, 2018 at 13:49
  • 3
    \$\begingroup\$ Hi, I've downvoted this question because it is very trivial. Most answers are going to be one or two variations, with very little room for creative golfing. \$\endgroup\$ Apr 13, 2018 at 14:07
  • \$\begingroup\$ Can the list of booleans be empty ? \$\endgroup\$
    – Ton Hospel
    Apr 13, 2018 at 14:12

20 Answers 20

3
\$\begingroup\$

Java (JDK 10), 12 bytes

s->s.sum()%2

Try it online!

If a string is really required, then 20 bytes:

s->s.chars().sum()%2

Try it online!

The "string" answer uses the fact that a space is codepoint 32, which mod 2 returns 0.

\$\endgroup\$
1
  • \$\begingroup\$ Wow. I feel kinda stupid now ;) \$\endgroup\$ Apr 13, 2018 at 15:29
2
\$\begingroup\$

JavaScript (ES6), 18 bytes

s=>eval(s.join`^`)

Try it online!

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

Python 2, 17 bytes

lambda a:sum(a)%2

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ This answer is not valid. given a string of space separated binary digits. and not an array \$\endgroup\$ Apr 13, 2018 at 13:41
  • \$\begingroup\$ Also may I ask why did you downvote the question ? is there something wrong \$\endgroup\$ Apr 13, 2018 at 13:41
  • 7
    \$\begingroup\$ @MuhammadSalman ... I guess TFeld didn't downvote it. How do you know that? \$\endgroup\$
    – DELETE_ME
    Apr 13, 2018 at 13:42
  • \$\begingroup\$ You can rollback to your original answer, the spec has been changed to allow arrays \$\endgroup\$ Apr 13, 2018 at 13:51
  • \$\begingroup\$ @cairdcoinheringaahing, Thanks :) \$\endgroup\$
    – TFeld
    Apr 13, 2018 at 13:53
2
\$\begingroup\$

05AB1E, 2 bytes

Try it online.

Explanation:

  • O: Take the sum of the input-array
  • É: Evaluates sum % 2 == 1, returning 1 if the sum is odd, 0 otherwise

3 bytes:

A leading Ç can be added if the space-delimited string input was still mandatory, instead of a boolean-array.

Try it online.

  • Ç: Push the ASCII values of all characters, and implicitly convert it to a list. 0 1 would become [48, 32, 49] in that case. The O (sum) and É (is odd?) will still act the same.
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 23 bytes

a=>a.reduce((c,d)=>c^d)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Apr 14, 2018 at 12:04
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 44 bytes

A	X =X + INPUT	:S(A)
	OUTPUT =REMDR(X,2)
END

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -p040, 10 bytes

Assumes the input list can't be empty

$\^=0+$_}{

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MATL, 3 2 bytes

so

Input can be a numeric vector of the form [1 0 0 1 0], or a string such as '10010' (thanks to @Giuseppe for noticing!).

Try it online!

Explanation

The code is so simple that it hardly needs an explanation, but here it goes.

s    % Implicit input: numeric vector (or string). Sum of the numbers. (For string
     % input, the ASCII codes are summed. Character '1' is odd, '0' is even, and
     % space is even too, so the parity is the same as with numeric vector input)
o    % Parity. Implicit display
\$\endgroup\$
2
  • \$\begingroup\$ heck, this would work on a string, since space=32 and doesn't change the parity. \$\endgroup\$
    – Giuseppe
    Apr 13, 2018 at 14:01
  • \$\begingroup\$ Oh, good idea! My previous version used U to convert from string to numeric vector, but indeed it can be removed \$\endgroup\$
    – Luis Mendo
    Apr 13, 2018 at 15:14
1
\$\begingroup\$

Java 10, 41 bytes

b->{var r=1<0;for(var c:b)r^=c;return r;}

The variable r is initially set to false, since we are XORing b[0] with it. Try it online here.

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

Add++, 8 bytes

L~,€Os2%

Try it online!

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

Jelly, 5 3 2 bytes

^/

Try it online!

-2 bytes thanks to user202729!

\$\endgroup\$
2
  • \$\begingroup\$ OSḂ also works. \$\endgroup\$
    – DELETE_ME
    Apr 13, 2018 at 13:43
  • \$\begingroup\$ @user202729 So it does! Thanks! \$\endgroup\$ Apr 13, 2018 at 13:44
0
\$\begingroup\$

Befunge-98 (FBBI), 8 bytes

#.~+2%#@

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can use #q~+2% instead of #.~+2%#@ and output via exit code to save 2 bytes. (In TIO you need to expand the debug tab if you want to see the exit code) \$\endgroup\$ Apr 21, 2018 at 18:44
0
\$\begingroup\$

Japt, 2 bytes

r^

Try it

Does exactly what it says on the tin, reduce the array by XORing.

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

Cubix, 11 bytes

i?+<^<@Oa1<

Try it online!

It's been a while, so it feels good to write a Cubix answer!

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

Whispers v2, 38 bytes

> Input
> 2
>> ∑1
>> 3%2
>> Output 4

Try it online!

I feel like 3 answers is too many, but I wanted to get Whispers out again. I'm still working on an XOR approach

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

(Traditional) APL, 3 or 4 bytes

TIO, courtesy Adám

≠/⍎⎕

Analysis:

- Accept input.
- "unquote" it (if it's a quoted string, this will convert it to a numeric vector. If it's already a numeric vector, this is effectively a null op)
/ - reduction - apply the operator to the left to each successive item in the vector to the right
- "not equal" - when applied strictly to boolean arguments, this is functionally identical to XOR (which is not implemented as a separate operator in APL)

If it may be assumed that the input will be a numeric vector instead of a quoted string, then the 'unquote' can be removed, saving one byte:

≠/⎕
\$\endgroup\$
5
  • \$\begingroup\$ HI. Can you kindly provide a TIO link. Thanks \$\endgroup\$ Apr 13, 2018 at 13:53
  • \$\begingroup\$ I've never been able to get quad-input in APL to work at TIO, that's why I didn't do so here. \$\endgroup\$ Apr 13, 2018 at 13:54
  • \$\begingroup\$ Ah I see. Oh well. Btw you can update your answer, it will be more easier now \$\endgroup\$ Apr 13, 2018 at 13:55
  • \$\begingroup\$ Input is essentially the APL session (except that and default output happens in Output and output happens in Debug). For convenience, you can define your workspace in Header, Code and Footer, but only Code is counted into the char/byte count: Try it online! \$\endgroup\$
    – Adám
    Apr 13, 2018 at 14:14
  • \$\begingroup\$ @Adám - Thank you. I'm not sure why I was never able to get it to work. \$\endgroup\$ Apr 13, 2018 at 14:18
0
\$\begingroup\$

Ruby, 42 bytes

->s{s.split(' ').reduce(0){|a,b|a^b.to_i}}

Try it online!

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

APL (Dyalog Unicode), 2 bytesSBCS

Tacit prefix function.

≠/

Try it online!

/ is reduction and is XOR because XOR only gives 1 if its arguments are unequal.

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

Julia 0.6, 12 bytes

b->⊻(b...)

Try it online!

is the xor symbol, and b... distributes an array as individual elements before sending it to the xor function (since xor needs multiple arguments passed separately, not a single array argument).

A bit more interestingly, a version that accepts space separated (/comma-separated/unseparated) boolean values as a string, and returns the xor result :

Julia 0.6, 22 bytes

b->sum(Int.([b...]))%2

Try it online!

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

C (gcc), 41 39 38 bytes

Saved a few bytes with inspiration from ceilingcat.

Saved another byte thanks to Jonathan Frech

r;f(char*s){for(r=0;*s;)r^=*s++;r&=1;}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can s=r&1 not be r&=1? \$\endgroup\$ Aug 6, 2018 at 3:52

Your Answer

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

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