Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- decorator
- split
- STR
- map
- Sorted
- len
- FOR
- sum
- range
- Algorithm
- join
- 위코드
- If
- or
- SQL
- DART
- MAX
- list
- WECODE
- enumerate
- js
- iNT
- lambda
- 파이썬
- count
- LOWER
- slice
- Python
- and
- index
Archives
- Today
- Total
코드로 우주평화
1204. Odd or Even? 본문
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 |