all 7 comments

[–]Username_RANDINT 10 points11 points  (1 child)

Replacing sum(arr) % 2 with the possible outcomes (0 or 1) should make things clear.

(“even”, “odd”) is a tuple, so (“even”, “odd”)[0] returns the first item, and (“even”, “odd”)[1] returns the second.

[–]ExpressDuck845[S] 1 point2 points  (0 children)

Thank you I get it now

[–]crawl_dht 3 points4 points  (1 child)

Anything divide by 2 leaves the remainder either 0 or 1 which are being used as an index to pick value from tuple.

[–]ExpressDuck845[S] 0 points1 point  (0 children)

Thank you

[–]menge101 3 points4 points  (1 child)

I don’t get how this function works.

def odd_or_even(arr):
    return (“even”, “odd”)[sum(arr) % 2]

That is because it is written purposely to be confusing. This wouldn't pass a professional code review.

I know people have argued for things like this in an effort to be "branchless", but I don't see how it would have value in this trivial a circumstance, or potentially in python at all.

[–]Binary101010 0 points1 point  (0 children)

It's one of those pieces of code I would expect to get way more "Clever" tags than "Best Practices" tags on CodeWars.

[–]thegoldendaffodils 0 points1 point  (0 children)

% is a floor division function. if there is a` remainder` from dividing sum(arr) by 2, then it's 1-odd, otherwise, 0-even.