나는 이렇게 학습한다/Algorithm & SQL

1204. Odd or Even?

daco2020 2022. 12. 4. 21:36
반응형

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"



Solution:

String oddOrEven(List<int> array) {
  int result = _sum_array(array);
  return result % 2 == 0 ? "even" : "odd";
}

int _sum_array(List<int> array) {
  int result = 0;
  for (int i in array) {
    result += i;
  }
  return result;
}
String oddOrEven(List<int> array) =>array.reduce((a, b) => a + b).isEven ? 'even' : 'odd';
import 'package:collection/collection.dart';

String oddOrEven(List<int> array) {
  return array.sum.isEven ? "even" : "odd";
}


반응형

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

1206. Sum of positive  (0) 2022.12.06
1205. Sum of Cubes  (0) 2022.12.05
1203. String repeat  (0) 2022.12.04
1202. Count by X  (0) 2022.12.02
1201. Is the string uppercase?  (0) 2022.12.01