본문 바로가기
코딩 테스트

[Codility] Lesson4.PermCheck (Python)

by zoodi 2021. 5. 9.
728x90

1.문제

app.codility.com/demo/results/training3ZHYPZ-UUU/

 

Test results - Codility

A non-empty array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2 is a permutation, but array A such that: A[0] =

app.codility.com

2.풀이

주어진 배열이 permutation이면 1, 아니면 0을 반환하는 문제

앞서 missingInteger문제와 아주 비슷했다.

 

3.코드

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    A.sort()
    pivot = 1
    answer = 1

    for i in A:
        if i == pivot:
            pivot +=1
        else:
            answer = 0
            break

    return answer
728x90

댓글