알고리즘/코드워

[python]Find the first non-consecutive number

(ㅇㅅㅎ) 2020. 3. 28. 19:11
728x90
반응형

https://www.codewars.com/kata/58f8a3a27a5c28d92e000144/train/python

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

더보기

내 코드

# My Code
def first_non_consecutive(arr):
    if len(arr) > 2:
        for i in range(1len(arr)-1):
            if arr[i] - arr[i - 1!= arr[i + 1- arr[i]:
                if arr[i + 1!= arr[-1and 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([4678911])         # 6
    print(answer)
    answer = first_non_consecutive([45678911])      # 11
    print(answer)
    answer = first_non_consecutive([-3-201])              # 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]
반응형