17
\$\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\$
  • \$\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 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 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 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 at 22:24
  • 1
    \$\begingroup\$ Does it need to be a Boolean or falsy/truthy is fine as usual? \$\endgroup\$ – Luca Citi May 22 at 23:57

50 Answers 50

1
2
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
| improve this answer | |
\$\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.

| improve this answer | |
\$\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

| improve this answer | |
\$\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?
| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Pizgenal Filegav Jun 19 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 [[],[]].

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Husk, 6 bytes

Why does need so many bytes... help!

EmLk%2

Try it online!

| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Icon, 52 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Racket, 44 bytes

(λ(a)(=(apply +(map(λ(x)(expt -1 x))a))0))

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Jelly, 4 bytes

-*S¬

Try it online!

Could be 3 bytes under "one consistent value, one non-consistent value" rules, with -*S outputting 0 for true and anything else for false.

   ¬    Whether or not 0 equals
  S     the sum of
-       -1
 *      to the power of every element of the input (implicitly vectorized).
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Clojure (60 bytes)

(defn q[c](reduce = (map #(count(filter % c))[odd? even?])))

Ungolfed:

(defn eq-odd-even [c]
  (reduce = (map #(count (filter % c)) [odd? even?])))

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Octave, 19 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

SQLite, 28 bytes

SELECT SUM(N%2*2-1)=0 FROM T

Inspired by this comment.

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Edit: shortened both by one byte by changing n%2*-2+1 to n%2*2-1

JavaScript (Node.js), 28 bytes with -p flag

.map((i=0,n=>i+=n%2*2-1));!i

Try it online!

Link leads to a TIO instance with console.log(!i) because I can't find out how to use the Node.js -p flag on TIO.

Less "cheating" answer:

JavaScript (Node.js), 32 bytes

a=>(a.map(x=>i+=x%2*2-1),!i);i=0

Try it online!

This link also leads to a snippet with console.log because I still can't figure out how to use the -p flag, but this one's a function.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Zpr'(h, 151 bytes

f|>\
(g ° (section p <$> @))
(g .l)|>((2 * (length (filter (section 0 == @) l))) == (length l))
(p ())|>0
(p (S ()))|>1
(p (S (S .n)))|>(p n)
<|prelude.zpr
test-cases |> ('\
        (' 5 (' 1 (' 0 (' 2 ())))) ('\
        (' 5 (' 1 (' 0 (' 2 (' 11 ()))))) ('\
        (' 2 (' 16 (' 3 (' 2 (' 11 (' 19 ())))))) ('\
        (' 2 (' 16 (' 3 (' 4 (' 2 (' 11 (' 19 ()))))))) ('\
        () \
    ())))))
main |> (zip test-cases (map f test-cases))
% ./Zprh stdlib/golf.zpr --de-peano
(' ((' 5 (' 1 (' 0 (' 2 0)))) , true) (' ((' 5 (' 1 (' 0 (' 2 (' 11 0))))) , false) (' ((' 2 (' 16 (' 3 (' 2 (' 11 (' 19 0)))))) , true) (' ((' 2 (' 16 (' 3 (' 4 (' 2 (' 11 (' 19 0))))))) , false) (' (0 , true) 0)))))
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Golfscript, 18 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Rust, 44 bytes

|a|a.iter().map(|n|(n&1)*2-1).sum::<i8>()==0

Try it online

Argument type is a: Vec<i8>.

.iter() and ::<i8> add 13 bytes to the solution which hurts the score quite a bit, but the compiler can't infer the <i8> unfortunately. If this closure accepted an iterator instead of a Vec<i8> (for example std::slice::IterMut<'static, i8>), the .iter() could be removed, but that seems a bit cheaty.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Scala, 20 bytes

a=>(a:\0)(_%2*2-1+_)

Try it online!

Outputs 0 if there as many odd numbers as even numbers, a negative number if there are more even numbers, and a positive number if there are more odd numbers.


Previous solution, 33 bytes

a=>a.count(_%2>0)==a.count(_%2<1)

Pretty straightforward solution. Just checks if the number of odd numbers equals the number of even numbers.

Try it online!

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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