# Check whether there are more even than odd numbers on any given nested list [closed]

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.

Example:

[[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


## closed as unclear what you're asking by Peter Taylor, WolframH, John Dvorak, Johannes Kuhn, manatworkDec 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? – Ry- 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.

x=input()
i=0
for o in x:
try:x+=o
except:i+=o%2-.5
print i


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

• Wouldn’t list be shorter than type([])? – Ry- Dec 11 '13 at 0:47
• @minitech yes it would :) – user8777 Dec 11 '13 at 0:48
• @minitech not type checking is even shorter! – user8777 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. – user8777 Dec 11 '13 at 1:17

# Befunge 98 - 19

&2%6*j\1+\2j1+3j@.


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:

&2%6*j\1+\2j1+3j@.-


# R, 64

### three possible outputs

function(x)c("even","same","odd")[sign(mean(unlist(x)%%2)-.5)+2]


Examples:

(function(x)c("even","same","odd")[sign(mean(unlist(x)%%2)-.5)+2])(
list(list(1, 2, 3), 4, list(5, 6), list(list(7, 8), 9)))
[1] "odd"

(function(x)c("even","same","odd")[sign(mean(unlist(x)%%2)-.5)+2])(
list(list(2, 3), 4))
[1] "even"

(function(x)c("even","same","odd")[sign(mean(unlist(x)%%2)-.5)+2])(
list(list(2, 3), list(4, 5)))
[1] "same"


## R 58

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

function(x)c("even","odd")[which.max(table(unlist(x)%%2))]


Example usage:

(function(x)c("even","odd")[which.max(table(unlist(x)%%2))])(
list(list(1, 2, 3), 4, list(5, 6), list(list(7, 8), 9)))
# [1] "odd"


# k

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}


Example:

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


## Golf-Basic 84, 17

iAdfpart(A/2)*2


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

Old version, 30 characters:

iA@fpart(A/2)*2tODD"#tEVEN"


Explanation (for old version)

iA


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

@fpart(A/2)*2tODD"#tEVEN"


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

• “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. – Ry- 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 – Ry- 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.

• 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

c=l=>l.reduce((a,b)=>a+(+b?b%2-.5:c(b)),0),l=>c(l)<0?"even":"odd"


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:

c=l=>l.reduce((a,b)=>a+(+b?b%2-.5:c(b)),0)


## 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

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