23
\$\begingroup\$

Inspired by this code review question, I am curious to see what happens if we turn that into a code-golf challenge!

The description is easy, the input is an array or any similar data structure with only unsigned integers, the output is a boolean if the number of odd numbers is equal to the number of even numbers inside (true or false doesn't matter, as long as the opposite is used in case the number doesn't match)

Here are some examples with true as output for matching (thanks to the original OP)

[5, 1, 0, 2]      ->  true 
[5, 1, 0, 2, 11]  ->  false
[]                ->  true 

Usual code-golf rules, shortest code wins.

\$\endgroup\$
11
  • \$\begingroup\$ Challenges should be self contained. References to external resources are welcome, but you should not rely on them. Please post some test cases here. \$\endgroup\$
    – manatwork
    May 22, 2020 at 10:39
  • 2
    \$\begingroup\$ @ElPedro I could say no if I wanted to be strict but, why not, maybe something interesting comes out of it. I'll edit the question. \$\endgroup\$
    – bracco23
    May 22, 2020 at 12:31
  • 3
    \$\begingroup\$ @ElPedro sorry not empty lists exemptions, you should always consider edge cases to avoid bugs :P \$\endgroup\$
    – bracco23
    May 22, 2020 at 12:36
  • 2
    \$\begingroup\$ @DomenicoModica A similar assumption has been made for the C solution. Since the input is only unsigned numbers, using a sentinel of -1 for end I think is fair. Go ahead ;) \$\endgroup\$
    – bracco23
    May 22, 2020 at 22:24
  • 1
    \$\begingroup\$ Does it need to be a Boolean or falsy/truthy is fine as usual? \$\endgroup\$
    – Luca Citi
    May 22, 2020 at 23:57

70 Answers 70

2
\$\begingroup\$

Keg, 13 10 bytes

÷⑷0;$Ë⑸⅀0=

Try it online!

Transpiled Code

item_split(stack)
keg_map(stack, '0;$Ë')
summate(stack)
integer(stack, 0)
comparative(stack, '=')

0;$Ë turns into:

integer(stack, 0)
decrement(stack)
swap(stack)
exponate(stack)
\$\endgroup\$
2
\$\begingroup\$

Thunno, \$ 4 \log_{256}(96) \approx 3.29 \$ bytes

1n@S

Attempt This Online!

Outputs 0 (which is falsy in Thunno) for true and something else (which is truthy in Thunno) for false (meta consensus).

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes

1n@S0=

Attempt This Online!

Outputs True for true and False for false.

Explanation

Port of xnor's Haskell answer.

1n@S0=  # Implicit input
1n      # Push -1
  @     # Get -1 ** each element of the input
   S    # The sum of the resulting list
    0=  # is equal to 0?
        # Implicit output
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 29 bytes

a=>a.map(x=>i+=x%2-.5,i=0)|!i

Try it online!

Combined some js and py solution

If input.length==1 then a.map returns 0.5 or -0.5, treated as 0 on bool operation

+1B if returning bool required

a=>a.map(x=>i+=x%2-.5,i=0)&&!i

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I thought how to optimize 1-x%2*2 and you did it! But you cheated a little :) You need to return boolean not a number \$\endgroup\$
    – EzioMercer
    Jan 26, 2023 at 2:28
  • 1
    \$\begingroup\$ @EzioMercer Comes from here \$\endgroup\$
    – l4m2
    Jan 26, 2023 at 2:32
2
\$\begingroup\$

Arturo, 25 23 bytes

$=>[0=∑map&=>[^0-1&]]

Try it

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

jq, 24 characters

map(.%2)|add+0==length/2

Just a write of the question owner's Python solution.

(Grr! 2 characters wasted because []|add results null.)

Sample run:

bash-5.0$ jq 'map(.%2)|add+0==length/2' <<< '[5, 1, 0, 2]'
true

Try it online! / Try all test cases online!

jq, 21 characters

map(.%2*2-1)|add+0==0

The other most efficient solution from Code Review.

Sample run:

bash-5.0$ jq 'map(.%2*2-1)|add+0==0' <<< '[5, 1, 0, 2]'
true

Try it online! / Try all test cases online!

\$\endgroup\$
3
  • \$\begingroup\$ Does jq not have a logical negation operator that can return true for both null and 0, so that you don't have to use +0==0? \$\endgroup\$
    – Neil
    May 22, 2020 at 12:34
  • \$\begingroup\$ @Neil If jq has one, I'd be able to golf my Erlang solution. \$\endgroup\$
    – user92069
    May 22, 2020 at 12:35
  • \$\begingroup\$ @Neil, in jq the only negation is the not filter, but as jq is quite strictly typed, all numbers are truthy: jq -c 'map(not)' <<< '[null,0]' results [true,false]. Even that + is working as an exception as the other arithmetic operators throw error on null operand. \$\endgroup\$
    – manatwork
    May 22, 2020 at 13:05
1
\$\begingroup\$

Husk, 6 bytes

Why does need so many bytes... help!

EmLk%2

Try it online!

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

Julia 18 bytes

Try it!

l->sum((-1).^l)==0
\$\endgroup\$
1
\$\begingroup\$

Octave, 19 bytes

@(x)(~sum((-1).^x))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I don't think you need the () around the ~sum() statement. \$\endgroup\$
    – Giuseppe
    Dec 30, 2020 at 22:57
1
\$\begingroup\$

Golfscript, 18 bytes

~.{2%},\{2%!},,\,=

Try it online!

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

Japt -!xm, 3 bytes

JpU

Try it

Explanation

The Flags:

  • ! Outputs the logical NOT of the value returned by the program
  • x Reduces output of program on plus
  • m Runs the program on each element in the first input, outputting an array of the results.
J    // The number -1
 p   // power
  U  // Input

Japt, 7 6 bytes

!Ux!pJ

Try it

Same thing as above but without flags

Explanation

   !pJ  // Function that does -1 ** arg
 Ux     // Map input on function then sum
!       // Logical not
\$\endgroup\$
1
\$\begingroup\$

Raku, 15 bytes

{!sum -1 X**$_}

Try it online!

The usual not of the sum of negative one to the power of each element in the input.

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

Factor + math.unicode, 22 bytes

[ -1 swap n^v Σ 0 = ]

Try it online!

Explanation:

  • -1 push -1 to the data stack (e.g.) { 5 1 0 2 } -1
  • swap swap top two objects on the data stack -1 { 5 1 0 2 }
  • n^v vectorized exponentiation { -1 -1 1 1 }
  • Σ sum a sequence 0
  • 0 push 0 to the data stack 0 0
  • = check top two objects on the data stack for equality t
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 5 bytes

∷Ċvt≈

Try it Online!

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

Go, 62 bytes

func(L[]int)bool{n:=0
for _,i:=range L{n+=-i%2|1}
return n==0}

Attempt This Online!

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

QBasic, 38 bytes

1s=s+(-1)^n
INPUT n
IF n>=0GOTO 1
?s=1

Enter the numbers one at a time with -1 as a sentinel value at the end. The program then outputs either -1 (true, same number of evens and odds) or 0 (false, different number of evens and odds).

Try it at Archive.org!

Explanation

The same "sum of \$-1\$ to the power of each number" approach as a lot of answers, with a twist. The straightforward approach is to write a loop like this:

  • Input a number
  • If it's -1, output the sum and quit
  • Else, add \$\pm1\$ to the sum accordingly and loop

But it's golfier if we can write a simple loop with the condition at the end (1 ... IF n >= 0 GOTO 1) rather than breaking out of the middle. So our actual loop looks like this:

  • Add \$\pm1\$ to the sum
  • Input a number
  • If it's nonnegative, loop
  • Else, output the sum and quit

Since QBasic initializes numeric variables to 0, the first s = s + (-1) ^ n before any n has been input adds 1 to s. Thus, a balanced array at the end of the loop is indicated by s being 1, not 0.

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

Pyt, 6 bytes

1∧2*⁻Ʃ

Try it online!

Outputs 0 (which is falsy in Pyt) for True and something else (which is truthy) for False

1∧        implicit print; bitwise-AND with 1 (equivalent to mod 2)
  2*⁻     double and decrement each element
     Ʃ    get sum; implicit print
\$\endgroup\$
1
\$\begingroup\$

Zsh, 31 bytes

for i;((i%2?j++:k++));<<<$[j-k]

Try it online!

Outputs 0 (truthy) if odds = evens, non-zero otherwise.

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

Pip, 21 bytes

b:0Wa{%POa?++i++b}i=b

Try It Online!

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

TI-Basic, 10 bytes

dim(Ans)=4sum(fPart(Ans/2

Takes input in Ans.

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

Thunno 2 S!, 2 bytes

u@

Attempt This Online!

Thunno 2, 4 bytes

ñEḷạ

Attempt This Online!

Explanation

u@  # Implicit input
u   # Push -1
 @  # Exponentiation
    # Check if the sum is 0
    # Implicit output
ñEḷạ  # Implicit input
ñ     # Group by:
 E    #  Is even?
  ḷ   # Length of each
   ạ  # Equal?
      # Implicit output
\$\endgroup\$
2
1
\$\begingroup\$

Desmos, 23 bytes

f(l)=0^{(-1)^l.total^2}

Try It On Desmos!

Try It On Desmos! - Prettified

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

Vyxal, 15 bitsv2, 1.875 bytes

Ǎ∑

Try it Online!

Uses inverted booleans (0 for truthy, any other value for falsey)

Explained

Ǎ∑
 ∑  # Sum of
Ǎ   # -1 to the power of each number in the input
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 13 bytes

~.,2/\{2%},,=

Try it online!

~.,2/\{2%},,=­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌­
~              # ‎⁡Evaluate the input string.
 .             # ‎⁢Duplicate the list on the stack.
  ,            # ‎⁣Push the length of the list.
   2/          # ‎⁤Divide it by 2.
     \         # ‎⁢⁡Swap so the original list is on top.
      {  },    # ‎⁢⁢Filter, only keeping items that
       2%      # ‎⁢⁣aren’t divisible by 2.
           ,   # ‎⁢⁤The length of the filtered list.
            =  # ‎⁣⁡Is this equal to the length of the original divided by 2?
💎

Created with the help of Luminespire.

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

APL+WIN, 15 bytes

Prompts for input of array. Can be anything from scalar, vector or multi dimensional array. TIO gives examples up to a 4 dimensional array:

(+/~n)=+/n←2|,⎕

Try it online! Coutesy of Dyalog Classic

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

Erlang (escript), 40 bytes

Port of the Python answer on Code Review.

f(I)->lists:sum([X rem 2*2-1||X<-I])==0.

Try it online!

Explanation

f(I)->    % Takes an input argument
||X<-I]   % For every inputted item:
[X rem 2*2-1 % Convert X % 2 into 1's and -1's, respectively
lists:sum()  % Sum the list
==0.          % Is it 0?
\$\endgroup\$
0
\$\begingroup\$

Befunge-93, 27 26 bytes

0v   @.$<
+>&:1+!#^_2%2*1-

Try it online!

Prints 0 if the number of odd and even numbers are equal, something else if they aren't. Keeps track of the difference between the number of even and odd numbers seen, adding 1 for an odd number, and -1 for an even number. Prints out this difference.

Edit: Moved the + to the left, saving a byte.

\$\endgroup\$
1
  • \$\begingroup\$ The initial stack in Befunge is implicitly a bunch of 0s, so I don't think the first 0 is necessary in the code. Removing it and shifting everything else to the left should get it down to 25 bytes. \$\endgroup\$ Jun 19, 2020 at 3:53
0
\$\begingroup\$

Charcoal, 5 bytes

¬ΣX±¹

Try it online! Port of @xnor's Haskell answer. Corresponds to the verbose code Print(Not(Sum(Power(Negate(1), Input())))); except that the input is implicit. Works even for zero-length arrays whose sum is Null rather than 0. Note: Zero-length input arrays seem to confuse Charcoal, so input that case as [[],[]].

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

Retina 0.8.2, 49 bytes

\d+
$*
+`(^|(,))(11)*(,1|1,)(11)*($|(\2)|,)
$7
^$

Try it online! Link includes test cases. Explanation:

\d+
$*

Convert to unary.

(^|(,))(11)*(,1|1,)(11)*($|(\2)|,)
$7

Match an even number of characters that includes a single , and is delimited on either side by either the end of the string or a ,, thus corresponding to a pair of numbers of opposite parity. Remove both numbers and at most one adjacent ,.

+`

Continue to remove pairs of numbers until all remaining numbers have the same parity.

^$

Check that there are no numbers left.

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

R, 28 bytes

!sd(table(c(0,1,scan())%%2))

Try it online!

table gets the counts of the unique values in the array. By appending c(0,1) to the array, we take care of the empty case. The standard deviation is equal to zero if and only if all the values from table() are equal, so we take the logical negation of that to get the result.

Alternately:

R, 28 bytes

!any(diff(table(scan()%%2)))

Try it online!

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

Icon, 52 bytes

procedure f(a)
(s:=0)+:=-1^!a&\z
return(s=0&1)|0
end

Try it online!

\$\endgroup\$

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.