본문 바로가기

loop3

[Python] 6.4. Triangle https://app.codility.com/demo/results/trainingX6JCFR-MQJ/ Test results - Codility An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P A[R], A[Q] + A[R] > A[P], A[R] + A[P] > A[Q]. For example, consider array A such that: A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A app.codility.com 인풋 데이터의 사이즈가 10만 아래이기 때문에 완전탐색하기로 결정하였다. 순열을.. 2021. 3. 26.
[Python] 2.1. CyclicRotation https://app.codility.com/programmers/lessons/2-arrays/ 2. Arrays lesson - Learn to Code - Codility Rotate an array to the right by a given number of steps. app.codility.com 빈 배열이 들어올 경우에는 그대로 리턴한다 빈 배열이 아닌 경우 입력값 K에 대한 나머지를 구하고 다음 인덱스 값을 계산해준다 만약 다음 인덱스 값이 음수 일 경우, 배열의 크기를 더해서 인덱스 값을 보정해준다. def solution(A, K): if len(A) == 0: return A rotate = K % len(A) answer = [] for idx in range(len(A)): nex.. 2021. 3. 6.
[Python] 1.1. BinaryGap https://app.codility.com/programmers/lessons/1-iterations/ 1. Iterations lesson - Learn to Code - Codility Find longest sequence of zeros in binary representation of an integer. app.codility.com 자연수를 이진수로 바꾼 후 1의 인덱스를 저장한다. 이후 인덱스간 차이를 계산해서 가장 큰 값을 최대값으로 업데이트해서 답을 구한다. def solution(N): bi = str(bin(N)) bi = bi[2:] max = 0 lst = [] for idx in range(len(bi)): if bi[idx] == "1": lst.append(idx) for.. 2021. 3. 6.
728x90
반응형