나는 이렇게 학습한다/Algorithm & SQL
Odd or Even?
daco2020
2022. 4. 30. 23:29
Task:
Given a list of integers, determine whether the sum of its elements is odd or even.
Give your answer as a string matching "odd" or "even".
If the input array is empty consider it as: [0] (array with a zero).
Examples:
Input: [0]
Output: "even"
Input: [0, 1, 4]
Output: "odd"
Input: [0, -1, -5]
Output: "even"
Have fun!
Solution:
1. Check if the sum of the array is odd.
2. If true, return "odd", otherwise return "even".
def odd_or_even(arr):
return sum(arr)&1 and "odd" or "even"