문제출처: 프로그래머스 런타임 에러 때문에 디버깅이 쉽지 않았다. 런타임 에러 발생 상황을 사전에 인지하여 검토하는 시간이 필요해보인다. 다음과 같이 크게 4가지 경우가 있다. ValueError: 부적절한 값을 인자로 받았을 경우 ZeroDivisionError: 0으로 나누었을 경우 IndexError: 인덱스 범위를 벗어나는 경우 NameError: 선언하지 않은 변수를 사용하는 경우 from collections import deque def solution(priorities, location): dq_i = deque(list(range(0, len(priorities)))) dq_p = deque(priorities) completed = 0 while dq_i: index = dq_i.po..
Algorithm/practice
문제 출처: 프로그래머스 from collections import deque import math def solution(progresses, speeds): answer = [] # 소요시간 계산 progresses = deque(progresses) speeds = deque(speeds) prev_time = 0 # 이전 작업 소요시간 dist_cnt = 0 # 배포 수 while progresses: extra_prog = 100 - progresses.popleft() time = math.ceil(extra_prog/speeds.popleft()) if time
문제출처: 프로그래머스 def solution(ingredient): answer = 0 s = [] for i in ingredient: s.append(i) if (len(s) >= 4) & (s[-4:] == [1, 2, 3, 1]): s = s[:-4] answer += 1 return answer ingredient 에서 요소를 pop(0) 하는 것 보다 for문을 통해 앞에서부터 훓으면서 원소를 stack에 저장하는 방식을 택했다. 그런 후 stack의 마지막 4개의 원소가 버거 포장 순서 1-2-3-1 과 일치하면 count 한다. Test 케이스 18개 중 4,5,12번 테스트 케이스에서 시간 초과로 실패했다. 그래서 if문에 추가로 stack 길이가 4이상인 경우를 추가하여 시간을 단축시..
def solution(s): answer = True tmp_s = [] s = list(s) while s != []: e = s.pop() if e == ")": tmp_s.append(e) elif tmp_s == []: return False else: tmp_e = tmp_s.pop() if tmp_e != ")": return False if tmp_s == []: return True else: return False string 형식에는 pop 메소드가 적용되지 않는 점을 간과하고 의사코드를 짰다.
문제출처: 프로그래머스 def solution(wallpaper): min_x, min_y = len(wallpaper), len(wallpaper[0]) max_x, max_y = 0, 0 for i in range(len(wallpaper)): for j in range(len(wallpaper[i])): if wallpaper[i][j] == '#': if i max_x: max_x = i if j > max_y: max_y = j answer = [min_x, min_y, max_x + 1, max_y + 1] return answer 되도록이면 이중 for문을 쓰지 않으려고 했지만, 결국 가장 쉽게 구현할 ..