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

1 2
3
0
\$\begingroup\$

Racket, 44 bytes

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

Try it online!

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

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

SQLite, 28 bytes

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

Inspired by this comment.

Try it online!

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

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

\$\endgroup\$
3
  • \$\begingroup\$ From what I read, this is not a valid Rust suggestion since the type can never be inferred. See codegolf.meta.stackexchange.com/a/25164/109946. \$\endgroup\$
    – leo848
    Oct 13, 2023 at 3:05
  • \$\begingroup\$ @leo848 Maybe I'm misunderstanding how meta works, but doesn't this say that types can be inferred? codegolf.meta.stackexchange.com/questions/11223/… The post you linked was closed, referencing this post instead, and anyway was posted well after my answer was posted. \$\endgroup\$
    – TehPers
    Dec 12, 2023 at 0:35
  • \$\begingroup\$ In that case, you do seem to be right. I thought that as this code can never compile as-is and requires explicit type annotations, it might not be valid. \$\endgroup\$
    – leo848
    Dec 12, 2023 at 8:01
0
\$\begingroup\$

Scala, 20 18 bytes

_.map(_%2*2-1).sum

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.

Also 18 bytes:

_.:\(0)(_%2*2-1+_)

Try it online!


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!

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

Prolog (SWI), 84 72 bytes

Saved 12 bytes thanks to this tip that user41805 suggested!

[]*A*A.
[H|T]*E*O:-M is mod(H,2),F is E+1-M,P is O+M,T*F*P.
f(X):-X*0*0.

Try it online!

g accepts a list, the number of even elements found so far, and the number of odd elements found so far. The base case is an empty list where the second and third parameters are the same. The second case calls g on the tail T, and increments E (number of even elements) or O (number of odd elements) depending on the head of the list, H. f is just for convenience, and starts g off with 0 elements of both parities.

\$\endgroup\$
2
  • \$\begingroup\$ You have to put queries in the input like so, tio.run/… \$\endgroup\$
    – user41805
    Mar 1, 2021 at 15:40
  • \$\begingroup\$ @user41805 Thanks, I always wondered why it didn't work in the footer! \$\endgroup\$
    – user
    Mar 1, 2021 at 15:47
0
\$\begingroup\$

Rust, 43 bytes

|a:&[i8]|0==a.iter().fold(0,|a,n|a+n%2*2-1)

Attempt This Online!

Simple functional solution. Works by first calculating the parity of the number, replacing each value by -1 if even and +1 if odd using a linear function of the remainder, and then summing the results. The sum is then compared to 0 to output a boolean.

\$\endgroup\$
1
  • \$\begingroup\$ Based on our other discussion (in my solution) - you can save a few bytes by dropping the type in the closure. Technically .iter() isn't necessary either, but you might want to specify that the expected type is an iterator rather than a slice or vec. Also, I love the approach of using fold. \$\endgroup\$
    – TehPers
    Dec 15, 2023 at 6:51
1 2
3

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.