728x90
반응형
https://www.codewars.com/kata/58f8a3a27a5c28d92e000144/train/python
더보기
내 코드
# My Code
def first_non_consecutive(arr):
if len(arr) > 2:
for i in range(1, len(arr)-1):
if arr[i] - arr[i - 1] != arr[i + 1] - arr[i]:
if arr[i + 1] != arr[-1] and arr[i+2] - arr[i+1] != arr[i] - arr[i-1]:
return arr[i]
return arr[i+1]
return None
if __name__=='__main__':
answer = first_non_consecutive([4, 6, 7, 8, 9, 11]) # 6
print(answer)
answer = first_non_consecutive([4, 5, 6, 7, 8, 9, 11]) # 11
print(answer)
answer = first_non_consecutive([-3, -2, 0, 1]) # 0
print(answer)
answer = first_non_consecutive([-5, -4, -3, -1]) # -1
print(answer)
|
더 간단한 코드는 아래와 같다.
def first_non_consecutive(arr):
if not arr: return 0
for i, x in enumerate(arr[:-1]):
if x + 1 != arr[i + 1]:
return arr[i + 1]
|
반응형
'알고리즘 > 코드워' 카테고리의 다른 글
[python]Beginner Series#1 School Paperwork (0) | 2020.04.01 |
---|---|
[python]Is it even? (0) | 2020.03.30 |
[python]Find the Difference in Age between Oldest and Youngest Family Members (0) | 2020.03.27 |
[python]All Star Code Challenge#18 (0) | 2020.03.26 |
[python]Will you make it? (0) | 2020.03.24 |