Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Take any type of nested list(s) containing numbers. Check for how many even/odd numbers are within and output even, same or odd (-1, 0, 1) in some way.


[[1,2,3],4,[5,6],[[7,8], 9]] => 5 odd > 4 even => odd or -1
[1,2,2,1,2,2,2]              => 5 even > 2 odd => even or 1
[1,2,2,1]                    => 2 even = 2 odd => same or 0
share|improve this question

closed as unclear what you're asking by Peter Taylor, WolframH, Jan Dvorak, Johannes Kuhn, manatwork Dec 11 '13 at 12:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Mind clarifying the example (and possibly provide more of them)? Your task description talks about counting odd numbers and filtering out odd numbers, but your example doesn't really show anything of that ilk at all. Also, clarifying how input/output is supposed to work would help. – FireFly Dec 10 '13 at 23:27
And if there are the same number? – Ryan O'Hara Dec 11 '13 at 0:44
Then say same or 0 – BoollumBits Dec 11 '13 at 2:29
-1 for changing important requirements after people posted their answers. Try to avoid that next time. – flodel Dec 11 '13 at 11:48
@Quincunx, as I can tell, the other solutions are also using that interpretation. I think the best would be to rephrase this question as “Take any type of list containing numbers separated by any noise.” – seems that would not disqualify any existing answer. Then BoollumBits could open a new question for real nested list, with good explanation and relevant example. – manatwork Dec 12 '13 at 9:05

Python - 63

This returns a negative number if its got more evens, 0 if their are an equal amount or a positive number if there are more odds.

for o in x:
print i

edit: Type checking, who needs that, lets just stab blindly.

share|improve this answer
Wouldn’t list be shorter than type([])? – Ryan O'Hara Dec 11 '13 at 0:47
@minitech yes it would :) – Lego Stormtroopr Dec 11 '13 at 0:48
@minitech not type checking is even shorter! – Lego Stormtroopr Dec 11 '13 at 0:54
This doesn't return even or odd, as required. – Timtech Dec 11 '13 at 0:58
@Timtech all making people say 'odd' or 'even' will do is at least 7 (usually more) characters to their code. This way its truer to the idea of golf. – Lego Stormtroopr Dec 11 '13 at 1:17

Befunge 98 - 19


Outputs 1 if even, 0 if odd or the same. I'm still trying to figure out how to indicate that they are the same. However, I have not tested this because I don't have a Befunge 98 compiler. It works as follows:

&   receives input as a number, all parts that aren't numbers are ignored and skipped
2%  takes that number and divides it by two, then gets the remainder (1 if odd, 0 if even)
6*j jump 6 characters if odd, 0 if even

when even:
\   swap top two numbers on stack (I'm storing evens under the odds)
1+  add one to that value
\   swap back
2j  jump 2 characters

when odd:
1+  add 1 to top number on stack

3j  jump 3 characters (to end of line ie the beginning (Befunge wraps))

When & is hit again, if there are no more values, it acts like a reflector, sending the instruction pointer back the other way and running this:


(executed in reverse order):

` compares the even and odd values, if even is greater, then push 1, else 0
. prints it out
@ ends the program

With one simple change, it can be made to print some negative number if there are more odds, some positive number if there are more evens, and 0 if there are the same amount:

share|improve this answer

R, 64

three possible outputs



   list(list(1, 2, 3), 4, list(5, 6), list(list(7, 8), 9)))
[1] "odd"

   list(list(2, 3), 4))
[1] "even"

  list(list(2, 3), list(4, 5)))
[1] "same"
share|improve this answer

R 58

This was written at a time the OP was asking for two "even" or "odd" possible outputs.


Example usage:

   list(list(1, 2, 3), 4, list(5, 6), list(list(7, 8), 9)))
# [1] "odd"
share|improve this answer


34 characters.

Outputs o for odd, e for even, s for same.

{"ose"1+{(x>0)-x<0}@+/-1 xexp,//x}

{(`s#-0w -.5 .5!"ose")@+/-1 xexp,//x}


{"ose"1+{(x>0)-x<0}@+/-1 xexp,//x}((1 3;5 10);12 2 4 6 8)
share|improve this answer

Golf-Basic 84, 17


Returns 0s for even numbers and 1s for odd numbers. Accepts nested arrays - 1D, 2D, 3D, 4D, etc.

Old version, 30 characters:


Explanation (for old version)


Store input in A. Accepts arrays, e.g. {1,2,3,4} or {{1,2},3,{4,5,{6,7}},8}


If (@) one or more numbers in array A are odd, display ODD. Else (#) display EVEN.

share|improve this answer
“If (@) one or more numbers in array A are odd, display ODD. Else (#) display EVEN.” That’s not the challenge. The idea is to check whether there are more even numbers than odd numbers. – Ryan O'Hara Dec 11 '13 at 1:07
@minitech WHAT?!?! Someone SERIOUSLY edited the question!!! – Timtech Dec 11 '13 at 1:09
@minitech Nevermind, that was the old version. The new one outputs 1s for odd and 0s for even. – Timtech Dec 11 '13 at 1:11
I think that might be taking “indicate in some way” a little far :D – Ryan O'Hara Dec 11 '13 at 1:12
@minitech I might as well, taken that Golf-Basic 84 actually runs through every digit in the array every time I put it into a mathematical equation (e.g. modulus). – Timtech Dec 11 '13 at 11:50

Ti-Basic 84, 26 characters

:Input A:Disp fpart(A/2)*2

Returns 1 for odd numbers and 0 for even numbers. Takes nested arrays - 1D, 2D, 3D, 4D, 5D, etc.

Old version, 50 characters:

:Input A:If fpart(A/2)*2:Disp"ODD":Else:Disp"EVEN"

Takes arrays like {1,2,3} and also {1,{2,3},4,{5,{6,7}},{{8,9},10},11}

Returns EVEN for all even and ODD if it contains odd.

share|improve this answer
The new version complies with the new requirements of measuring how many odd and even numbers there are (it displays an array of true and false). – Timtech Dec 11 '13 at 11:52

JavaScript, 65


Defaults to “odd” if there are equal numbers of each. Requires arrow function support, which currently means “SpiderMonkey”.

If you don’t mind output as a number, it’s just this part, at 42:

share|improve this answer

JavaScript, 51+

Variation 1, 61 or 51

Reads input via prompt, outputs via alert, encodes answer as a number with sign denoting if more evens or odds.

b=0;prompt().replace(/\d+/g,function(s){b+=s%2*2-1});alert(b) // ES5, 61
b=0;prompt().replace(/\d+/g,s=>b+=s%2*2-1);alert(b) // ES6, 51

Variation 2, 72 or 49

As above, but as a function taking an array instead of using prompt/alert.

function f(s){b=0;return s.big?b+=s%2*2-1:s.join().replace(/\d+/g,f)&&b} // ES5, 72
f=a=>b=0,a.join().replace(/\d+/g,s=>b+=s%2*2-1),b // ES6, 49
share|improve this answer

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