Notice
Recent Posts
Recent Comments
Link
코드로 우주평화
Sum of Odd Cubed Numbers 본문
반응형
Description:
Find the sum of the odd numbers within an array, after cubing the initial integers. The function should return undefined/None/nil/NULL if any of the values aren't numbers.
Note: Booleans should not be considered as numbers.
Solution:
1. If the element in the array is not of type 'int', None is returned.
2. If the cube of the remaining elements is odd, the values are added.
3. Returns the added value.
def cube_odd(arr):
result = 0
for i in arr:
if type(i) != int:
return None
if i**3&1:
result += i**3
return result
There is also logic implemented using the number of types.
However, in this case, an error occurs when an array whose elements are all str is entered.
def cube_odd(arr):
if len(set(map(type,arr))) < 2:
return sum(n**3 for n in arr if n%2)
This code is hard to see as good logic.
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Regex validate PIN code (0) | 2022.04.16 |
---|---|
Get the mean of an array (0) | 2022.04.15 |
Century From Year (0) | 2022.04.13 |
Isograms (0) | 2022.04.12 |
Student's Final Grade (0) | 2022.04.11 |