| Bytes | Lang | Time | Link |
|---|---|---|---|
| 005 | Jalapeño | 250204T215148Z | ATaco |
| 007 | Husk | 250203T175141Z | int 21h |
| 003 | Thunno 2 | 230810T085309Z | The Thon |
| nan | Fig | 230223T003512Z | south |
| 024 | Arturo | 230222T230731Z | chunes |
| 009 | TIBasic | 211218T190402Z | Youserna |
| 032 | Python 2 | 211214T151514Z | Larry Ba |
| 027 | Factor + math.unicode | 211214T160352Z | chunes |
| 017 | Julia 1.0 | 190531T180845Z | gggg |
| 003 | 05AB1E legacy | 170502T183425Z | Riley |
| 006 | Japt | 190530T223555Z | Shaggy |
| 045 | Axiom | 170503T131420Z | user5898 |
| 055 | Swift | 170503T061152Z | Mr. Xcod |
| nan | 170504T183440Z | Brad Gil | |
| nan | 170503T133904Z | auhmaan | |
| 031 | Ruby | 170503T171724Z | Cyoce |
| 064 | AWK | 170503T185425Z | Robert B |
| 007 | APL Dyalog Unicode | 170503T101316Z | user4180 |
| 006 | Pyth | 170502T210549Z | KarlKast |
| 078 | Java | 170503T025638Z | Leaky Nu |
| 004 | Ohm | 170503T030559Z | Nick Cli |
| 021 | R | 170502T184258Z | Giuseppe |
| 038 | Retina | 170502T215533Z | Neil |
| 062 | C | 170502T223355Z | Khaled.K |
| 050 | PHP | 170502T200408Z | Jör |
| 020 | Haskell | 170502T194004Z | nimi |
| 052 | Wise | 170503T051233Z | Wheat Wi |
| 066 | BrainFlak | 170503T035631Z | Wheat Wi |
| 006 | J | 170503T041726Z | Leaky Nu |
| 036 | ClojureScript | 170503T031622Z | madstap |
| 019 | Scala | 170502T234529Z | Brian Mc |
| 010 | CJam | 170502T222738Z | Luis Men |
| 031 | Perl 5 | 170502T214221Z | Chris |
| 009 | k | 170502T211425Z | zgrep |
| 028 | Alice | 170502T203408Z | Martin E |
| 030 | Clojure | 170502T200935Z | NikoNyrh |
| 004 | MATL | 170502T191716Z | DJMcMayh |
| 013 | Mathematica | 170502T192343Z | Martin E |
| 032 | JavaScript ES6 | 170502T183213Z | Luke |
| 003 | Jelly | 170502T184230Z | Dennis |
| 007 | Japt | 170502T183651Z | ETHprodu |
| 031 | Python 2 | 170502T183403Z | Rod |
Jalapeño, 5 bytes
Σ-I₀%2
Explained
Σ-I₀%2
Σ # Sum of input
-I₀ # Subtract, vectorized over the input
%2 # Modulo 2, get pairity.
Outputs 1 for odd, 0 for even.
Hex-Dump of Bytecode
0 1 2 3 4 5 6 7 8 9 A B C D E F
0000: e9 51 90 54 32
Husk, 7 bytes
Mo%2-¹Σ
Outputs 0 for even, 1 for odd.
Explanation:
-- implicit input
Σ -- sum up the input list
M -- map each element of the list:
o -- (via a function combinator o)
-¹ -- | subtract the element from the total sum
%2 -- | and modulo 2
Thunno 2, 3 bytes
S+ɗ
Explanation
S+ɗ # Implicit input
S # Sum the input
+ # Add to input
ɗ # Parity of each
# Implicit output
Fig, \$4\log_{256}(96)\approx\$ 3.292 bytes
%2-S
1 for odd, 0 for even.
%2-S
S # sum
- # minus each item of input array
%2 # mod each by 2
TI-Basic, 9 bytes
fPart(.5(sum(Ans)+Ans
Takes input in Ans. Output is stored in Ans and displayed. Outputs .5 for odd numbers and 0 for even ones.
Factor + math.unicode, 32 27 bytes
[ dup Σ v-n [ odd? ] map ]
Outputs t for odd and f for even.
! { 1 2 3 1 }
dup ! { 1 2 3 1 } { 1 2 3 1 }
Σ ! { 1 2 3 1 } 7
v-n ! { -6 -5 -4 -6 }
[ odd? ] map ! { f t f f }
05AB1E (legacy), 4 3 bytes
O^È
O # Sum
^ # XOR with (implicit) input
È # Print 1 for even / 0 for odd
Japt, 6 bytes
®nUx)v
®nUx)v :Implicit input of array U
® :Map
n :Subtract from
Ux : Sum of U
) :End subtraction
v :Divisible by 2?
Axiom, 45 bytes
f(a)==[(reduce(+,a)-a.j)rem 2 for j in 1..#a]
no check for input type, possible recalculation of sum "a" each element...tests
(27) -> [[a,f(a)] for a in [[1,2,3,1], [1,2,3,2,1], [2,2], [100, 1001] ]]
(27)
[[[1,2,3,1],[0,1,0,0]], [[1,2,3,2,1],[0,1,0,1,0]], [[2,2],[0,0]],
[[100,1001],[1,0]]]
Swift - 55 bytes
Finally beats C! Also, 0 for even, 1 for odd
func g(a:[Int]){for i in a{print((a.reduce(0,+)-i)%2)}}
A function, with usage: g(a: [1,2,3,2,1] // => 0 1 0 1 0
Perl 6, 16 bytes
{(.sum X-$_)X%2}
This results in a list where 0 represents even and 1 represents odd.
Expanded:
{ # bare block lambda with implicit parameter $_
(
.sum # the sum of the input array
X[-] # crossed using &infix:« - »
$_ # the input
# this produces the sum of the other elements
)
X[%] # cross that using &infix:« % » the modulus operator
2 # with 2
}
C#, 142 121 bytes
Data
- Input An array of 32-byte signed integers
- Output An array of 32-byte signed integers where
0representsevenand1representsodd
Golfed
(int[] i)=>{var l=i.Length;var o=new int[l];var s=0;foreach(int x in i)s+=x;for(int x=0;x<l;x++)o[x]=s-i[x]&1;return o;};
Ungolfed
( int[] i ) => {
var l = i.Length;
var o = new int[ l ];
var s = 0;
foreach( int x in i )
s += x;
for( int x = 0; x < l; x++ )
o[ x ] = s - i[ x ] & 1;
return o;
};
Ungolfed readable
// Takes an array of Int32 objects ( 32-byte signed integers )
( int[] i ) => {
// Save the length of the array to save on byte count
var l = i.Length;
// Initialize an array with the same size the the input array
var o = new int[ l ];
// Initialize a var to store the sum of all numbers
var s = 0;
// Cycle through every number from the input
foreach( int x in i )
// Sum all of them
s += x;
// Cycle again through every number
for( int x = 0; x < l; x++ )
// Subtracts the number for the sum
// and performs a bitwise and
o[ x ] = s - i[ x ] & 1;
// Return the array with odds and evens
return o;
};
Full code
using System;
using System.Collections.Generic;
namespace Namespace {
class Program {
static void Main( String[] args ) {
Func<Int32[], Int32[]> f = ( int[] i ) => {
var l = i.Length;
var o = new int[ l ];
var s = 0;
foreach( int x in i )
s += x;
for( int x = 0; x < l; x++ )
o[ x ] = s - i[ x ] & 1;
return o;
};
List<Int32[]>
testCases = new List<Int32[]>() {
new Int32[] { 1, 2, 3, 1 },
new Int32[] { 1, 2, 3, 2, 1 },
new Int32[] { 2, 2 },
new Int32[] { 100, 1001 },
};
foreach( Int32[] testCase in testCases ) {
Console.WriteLine( $" Input: {String.Join( ",", testCase )}\nOutput: {string.Join( ",", f( testCase ) )}\n" );
}
Console.ReadLine();
}
}
}
Releases
- v1.1 -
-21 bytes- Changed the output to an array ofints to reduce the byte count thanks to Arthur Rey's comment. Changed the ternary verification to a bitwise operation, thanks to Cyoce's suggestion - v1.0 -
142 bytes- Initial solution.
Notes
- None
Ruby, 47 33 31 Bytes
->a{a.map{|x|a.inject(:+)-x&1}}
It maps through each element, determining the parity of the sum of all elements minus the current element (thus making it 0).
AWK, 64 bytes
{for(i=0;++i<=NF;print s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}
Outputs a 0 for even sums and 1 for odd sums separated by newlines. The only even slightly out-of-the-box thinking was placing the print command inside the for "increment" step. I tried a few "clever" ways to print, but they didn't save bytes.
Just for giggles, if you don't want newlines:
{for(i=0;++i<=NF;m=m,s[i]%2)for(j=0;++j<=NF;)if(i!=j)s[i]+=$j}1
which has the same byte-count as above, but is a bit more obtuse.
Pyth, 7 6 bytes
mi2-sQ
-1 Byte thanks to @KZhang
Outputs 1 for odd, 2 for even.
Explanation
m%-sQd2
m Q # For each element in the (implicit) input list
sQ # Take the sum of all the elements
- d # subtract that element, so that we now have the sum of the other elements
% 2 # modulo 2; 1=off, 0=even
Java, 81 78 bytes
3 bytes thanks to Kevin Cruissen
void f(int[]a){int s=0,i=a.length;for(int x:a)s+=x;for(;i-->0;a[i]=s-a[i]&1);}
Modifies the array in-place.
Ohm, 4 bytes
DΣ-é
Basically a direct port of the MATL and 05AB1E answers. Uses true for even and false for odd.
R, 21 bytes
(sum(n<-scan())-n)%%2
reads the list from stdin and returns 0 for even, 1 for odd. binds the input to the variable n inside the call to sum instead of calling it outside, i.e., n=scan();(sum(n)-n)%%2
Retina, 40 38 bytes
\d+
¶$` $'
^¶
\d+
$*
+` |11
%M`1
¶
Try it online! Outputs 1 for odd and 0 for even. Explanation: The first two lines duplicate the input once for each number in the input, but without the element itself. This creates an extra blank line which is then deleted. The input is then converted from decimal to unary, the spaces are deleted and the parity computed. Even parity is then converted to zero and the results joined back in to one line. Edit: Saved 2 bytes thanks to @FryAmTheEggman. I tried some other versions which are conceptually more pleasing but which take far too many bytes to express:
\d\B
T`2468O`00001
T`d`10`^([^1]*1[^1]*1)*[^1]*1[^1]*$
Changes all inputs to their parity, then flips all their parities if the total has odd parity.
\d+
$*
^.*
$&¶$&
(?=.*$)
11
\B
0
T`d`10`.*¶1
¶0
Sums a duplicate of the input, then takes the parity of everything, then inverts the parities if the sum is odd, then deletes the sum again.
C, 68 62 bytes
i;s;f(c,l)int*l;{while(i<c)s+=l[i++];while(i)l[--i]=s-l[i]&1;}
1 for odd, 0 for even
Detailed Try Online
f(int c, int * l)
{
int i = 0, s = 0;
while(i < c)
{
s = s + l[i];
i = i + 1;
}
// assert(i == c)
while(i > 0)
{
i = i - 1;
l[i] = s - l[i]&1;
}
}
PHP, 50 Bytes
1 for odd , 0 for even
Output as string separated with _
<?foreach($_GET as$v)echo array_sum($_GET)-$v&1,_;
PHP, 72 Bytes
Output as array use array_map
<?print_r(array_map(function($v){return array_sum($_GET)-$v&1;},$_GET));
Haskell, 20 bytes
f x=odd.(sum x-)<$>x
Uses True for odd values and Falsefor even values.
Subtract each element from the sum of the list and test if it's odd.
f turned to pointfree has also 20 bytes: map=<<(odd.).(-).sum.
Wise, 54 52 bytes
::^:??[:!^:?^:!^:?^?]|!::^??[!:?^:><^!:?^:!^:?^?]!&|
Explanation
This code would be a lot shorter if it didn't take so many bytes to swap the top two elements. The current record is
:?^:!^:?^!
This unfortunately constitutes the majority of the code.
First we take the XOR sum of the stack
::^:??[:!^:?^:!^:?^?]|!
We then XOR this with every element and the element with it last bit zeroed
::^??[!:?^:><^!:?^:!^:?^?]!&|
Brain-Flak, 94 68 66 bytes
({({}<>)<>})<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}
This seems a little long for the task. There might be a more convenient way to do this.
Explanation
First we calculate the sum of the stack with:
({({}<>)<>})
The we go through the entire stack adding that result to each element and determine the pairity
<>{<>(({})<({}<>{}<>(())){({}[()]<([{}])>)}{}>)<>}<>{}
This uses a pretty cool mod 2 algorithm I came up with for this challenge.
({}(())){({}[()]<([{}]())>)}{}
This pushes 1 under the input decrements until the input reaches zero each time performing 1-n to the 1 we placed earlier, it then removes the input.
Clojure(Script), 36 bytes
Output is true for odd and false for even. Both output and input are sequences.
(fn[l](map #(odd?(-(apply + l)%))l))
CJam, 10 bytes
{_:+f+1f&}
This is an anonymous block (function) that takes the input from the stack and replaces it by the output.
Explanation
Consider input [1 2 3 1].
{ e# Begin block
e# STACK: [1 2 3 1]
_ e# Duplicate
e# STACK: [1 2 3 1], [1 2 3 1]
:+ e# Fold addition over the array: compute its sum
e# STACK: [1 2 3 1], 7
f+ e# Map addition over the array with extra parameter
e# STACK: [8 10 11 8]
1 e# Push 1
e# STACK: [8 10 11 8], 1
f& e# Map bit-wise "and" over the array with extra parameter
e# STACK: [0 0 1 0]
} e# End block
Perl 5, 31 bytes
sub{map$x+=$_,@_;map$x-$_&1,@_}
Outputs 1 for odd and 0 for even.
k, 9 bytes
{2!x-+/x}
The output is a 1 for odd, and a 0 for even. Try it online.
Converted to pseudocode, it would be:
for number in x:
yield (number - sum(x)) % 2
Alice, 31 28 bytes
/O.HQ\d$K@
\i#\ /d2-&+!w?+2%
The input format doesn't matter as long as the integers are separated. The output format is linefeed-separated.
The layout is probably still not optimal but I haven't found a way to shorten this further yet.
Explanation
/ Reflect to SE. Switch to Ordinal.
i Read all input as a string.
Reflect off bottom boundary. Move to NE.
. Duplicate the input string.
Reflect off top boundary. Move to SE.
\ Reflect to N. Switch to Cardinal.
H Implicitly convert the top copy of the input to the integers it
contains and take the absolute value of the top-most one. (Taking
the absolute value doesn't do anything, but we need the implicit
conversion to integers.)
The IP wraps back to the second line.
\ Reflect to SE. Switch to Ordinal.
Immediately reflect off bottom boundary. Move to NE.
Q Reverse the stack (this converts the integers back to strings but
that's irrelevant). After this, we end up with all the individual
integers on the bottom of the stack in reverse order and the other
copy of the input string with all integers on top.
Reflect off top boundary. Move to SE.
/ Reflect to E. Switch to Cardinal.
d Push the stack depth, which is one more than the number of list
elements (due to the other input string on the stack).
2- Subtract 2.
&+ Run + that many times, which implicitly converts the second string
to the integers it contains and then adds up all the list elements.
! Store the sum on the tape.
w Push the current IP position to the return address stack. This
lets us return here repeatedly to implement a loop.
?+ Retrieve the input sum from the tape and add it to the current
element.
2% Compute its parity. Other ways to do this are 1A and 0x. 2F would
also work but it gives 0/2 instead of 0/1.
The IP wraps the first column of the grid.
\ Reflect to NE. Switch to Ordinal. The IP bounces diaginally up
and down until it hits the next \.
O Implicitly convert the current parity to a string and print it
with a trailing linefeed.
# Skip the next command (the H).
\ Reflect to E. Switch to Cardinal.
d Push the stack depth. This is zero when we're done.
$ Skip the next command if the stack depth is indeed zero, which
exits the loop.
K Jump to the return address on top of the return address stack without
popping it (so that the next K will jump there again).
@ Terminate the program.
Clojure, 30 bytes
#(for[i %](odd?(apply - i %)))
Substracts all values from each value in turn, for example with input [a b c d] the 2nd calculated value is b - a - b - c - d = -(a + c + d). Output is false for even and true for odd.
But you might as well use + and calculate each subsequent term twice so it doesn't affect the parity.
MATL, 5, 4 bytes
ts-o
One byte saved thanks to Dennis!
This gives '1' for odd and '0' for even. Explanation:
t % Duplicate the input
s % Get the sum of the input
- % Subtract it from the original input
o % Get the parity of each element
Mathematica, 13 bytes
#+Tr@#&/*OddQ
or
OddQ[#+Tr@#]&
JavaScript (ES6), 38 36 32 bytes
a=>a.map(b=>eval(a.join`+`)-b&1)
Uses 0 for even and 1 for odd.
Test
f=
a=>a.map(b=>eval(a.join`+`)-b&1)
console.log(f([1, 2, 3, 1]))
console.log(f([1, 2, 3, 2, 1]))
console.log(f([100, 1001]))
Jelly, 3 bytes
+SḂ
How it works
+SḂ Main link. Argument: A (array)
S Compute the sum of A.
+ Add the sum to each element of A.
Using _ (subtraction) or ^ (bitwise XOR) would also work.
Ḃ Bit; compute the parity of each resulting integer.
Japt, 7 bytes
£x +X&1
Explanation
£ x +X&1
mX{ x +X&1} // Ungolfed
UmX{Ux +X&1} // Variable introduction
UmX{ } // Replace each item in the input array with
&1 // the parity of
Ux // the sum of the input array
+X // plus X.
// Implicit: output result of last expression