나는 이렇게 학습한다/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"

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Are You Playing Banjo?  (0) 2022.05.02
Find the vowels  (0) 2022.05.01
Sort array by string length  (0) 2022.04.29
Sum of odd numbers  (0) 2022.04.28
Mumbling  (0) 2022.04.27