XOR reduction bools

-7

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.

Muhammad Salman

Posted 2018-04-13T13:35:55.433

Reputation: 2 361

1Your 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? – Luis Mendo – 2018-04-13T13:44:18.387

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. – Muhammad Salman – 2018-04-13T13:45:09.927

14Use 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. – Olivier Grégoire – 2018-04-13T13:49:30.457

3Hi, 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. – AdmBorkBork – 2018-04-13T14:07:46.953

Can the list of booleans be empty ? – Ton Hospel – 2018-04-13T14:12:06.550

@Adám : done . Ton : that is up to you – Muhammad Salman – 2018-04-13T15:48:25.880

Answers

1

JavaScript (ES6), 18 bytes

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

Try it online!

Arnauld

Posted 2018-04-13T13:35:55.433

Reputation: 111 334

3

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.

Olivier Grégoire

Posted 2018-04-13T13:35:55.433

Reputation: 10 647

Wow. I feel kinda stupid now ;) – O.O.Balance – 2018-04-13T15:29:17.267

2

Python 2, 17 bytes

lambda a:sum(a)%2

Try it online!

TFeld

Posted 2018-04-13T13:35:55.433

Reputation: 19 246

This answer is not valid. given a string of space separated binary digits. and not an array – Muhammad Salman – 2018-04-13T13:41:04.833

Also may I ask why did you downvote the question ? is there something wrong – Muhammad Salman – 2018-04-13T13:41:32.967

7@MuhammadSalman ... I guess TFeld didn't downvote it. How do you know that? – user202729 – 2018-04-13T13:42:09.550

You can rollback to your original answer, the spec has been changed to allow arrays – caird coinheringaahing – 2018-04-13T13:51:45.613

@cairdcoinheringaahing, Thanks :) – TFeld – 2018-04-13T13:53:28.510

2

05AB1E, 2 bytes

OÉ

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.

Kevin Cruijssen

Posted 2018-04-13T13:35:55.433

Reputation: 67 575

2

JavaScript (Node.js), 23 bytes

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

Try it online!

user79855

Posted 2018-04-13T13:35:55.433

Reputation:

Welcome to PPCG! – Martin Ender – 2018-04-14T12:04:24.397

1

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

Luis Mendo

Posted 2018-04-13T13:35:55.433

Reputation: 87 464

heck, this would work on a string, since space=32 and doesn't change the parity. – Giuseppe – 2018-04-13T14:01:46.303

Oh, good idea! My previous version used U to convert from string to numeric vector, but indeed it can be removed – Luis Mendo – 2018-04-13T15:14:17.617

1

SNOBOL4 (CSNOBOL4), 44 bytes

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

Try it online!

Giuseppe

Posted 2018-04-13T13:35:55.433

Reputation: 21 077

1

Perl 5 -p040, 10 bytes

Assumes the input list can't be empty

$\^=0+$_}{

Try it online!

Ton Hospel

Posted 2018-04-13T13:35:55.433

Reputation: 14 114

1

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.

O.O.Balance

Posted 2018-04-13T13:35:55.433

Reputation: 1 499

0

Jelly, 5 3 2 bytes

^/

Try it online!

-2 bytes thanks to user202729!

caird coinheringaahing

Posted 2018-04-13T13:35:55.433

Reputation: 13 702

OSḂ also works. – user202729 – 2018-04-13T13:43:26.537

@user202729 So it does! Thanks! – caird coinheringaahing – 2018-04-13T13:44:21.227

0

Add++, 8 bytes

L~,€Os2%

Try it online!

caird coinheringaahing

Posted 2018-04-13T13:35:55.433

Reputation: 13 702

0

(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:

≠/⎕

Jeff Zeitlin

Posted 2018-04-13T13:35:55.433

Reputation: 213

HI. Can you kindly provide a TIO link. Thanks – Muhammad Salman – 2018-04-13T13:53:52.037

I've never been able to get quad-input in APL to work at TIO, that's why I didn't do so here. – Jeff Zeitlin – 2018-04-13T13:54:43.103

Ah I see. Oh well. Btw you can update your answer, it will be more easier now – Muhammad Salman – 2018-04-13T13:55:52.073

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! – Adám – 2018-04-13T14:14:50.650

@Adám - Thank you. I'm not sure why I was never able to get it to work. – Jeff Zeitlin – 2018-04-13T14:18:14.403

0

ovs

Posted 2018-04-13T13:35:55.433

Reputation: 21 408

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) – MildlyMilquetoast – 2018-04-21T18:44:55.533

0

Japt, 2 bytes

r^

Try it

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

Shaggy

Posted 2018-04-13T13:35:55.433

Reputation: 24 623

0

Cubix, 11 bytes

i?+<^<@Oa1<

Try it online!

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

Giuseppe

Posted 2018-04-13T13:35:55.433

Reputation: 21 077

0

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

caird coinheringaahing

Posted 2018-04-13T13:35:55.433

Reputation: 13 702

0

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!

cleblanc

Posted 2018-04-13T13:35:55.433

Reputation: 3 360

1Can s=r&1 not be r&=1? – Jonathan Frech – 2018-08-06T03:52:29.557

0

Ruby, 42 bytes

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

Try it online!

lfvt

Posted 2018-04-13T13:35:55.433

Reputation: 121

0

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.

Adám

Posted 2018-04-13T13:35:55.433

Reputation: 37 779

0

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!

sundar - Reinstate Monica

Posted 2018-04-13T13:35:55.433

Reputation: 5 296