Feb-02-2023, 04:33 AM
(This post was last modified: Feb-02-2023, 04:33 AM by deanhystad.)
Which is it?
When solving something like this I perform the operation by hand and look for a pattern.
values = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
result = [0, 0] # result for even, odd
The operator has a period 4 cycle: +, +, *, *
Using that information I might write the function like this:
And about using "O" as a variable. Don't do it. Capital o looks like zero, just like lower case L looks like one. Avoid using either to avoid any confusion.
Reven = ((((A[0]*A[2])+A[4])*A[6])+A[8])Reven = (0 * 2 + 4) * 6 + 8 = 32
0 +2 2 *4 8 +6 14 *8Reven = ((0 + 2) * 4 + 6) * 8 = 112
When solving something like this I perform the operation by hand and look for a pattern.
values = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
result = [0, 0] # result for even, odd
Output:index value operator result Description
0 0 + [0, 0] result[0] += 0
1 1 + [0, 1] result[1] += 1
2 2 * [0, 1] result[0] *= 2
3 3 * [0, 3] result[1] *= 3
4 4 + [4, 3] result[0] += 4
5 5 + [4, 8] result[1] += 5
6 6 * [24, 8] result[0] *= 6
7 7 * [24, 56] result[1] *= 7
8 8 + [32, 56] result[0] += 8
9 9 + [32, 65] result[1] += 9
The index for result[index] has a period 2 cycle: 0, 1The operator has a period 4 cycle: +, +, *, *
Using that information I might write the function like this:
from operator import add, mul def add_mul(numbers): result = [0, 0] for i, number in enumerate(numbers): result[i % 2] = (add, add, mul, mul)[i % 4](result[i % 2], number) return resultI don't quite understand the even/odd evaluation at the end, but this code:
E = (Reven%2) O = (Rodd%2) if(O > E): return "ODD" elif(E > O): return "EVEN" elif(O == E): return "NEUTRAL"Can be written like this:
even, odd = (result % 2 for result in add_mul(range(10))) print("NEUTRAL" if (even == odd) else "EVEN" if even else "ODD")In Python, 0 is falsey and 1 is truey. If you know the only possible values are 0 and 1 you don't need to do comparisons like E > O.
And about using "O" as a variable. Don't do it. Capital o looks like zero, just like lower case L looks like one. Avoid using either to avoid any confusion.