15
\$\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.

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

39 Answers 39

1
2
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\$

[Excel], 28 bytes

Paste this into a cell outside of column A, the array goes in column A.

=0=SUM(IF((A:A<>""),-1^A:A))

An example:

An example

| 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\$

Java (JDK), 28 bytes

a->a.map(n->-n%2|1).sum()==0

Try it online!

| 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\$
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.