2/프로그래머스
프로그래머스 연습 문제 - 프린터
하례은
2020. 9. 27. 02:45
programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린��
programmers.co.kr
스택/큐 문제
def solution(priorities, location):
index = 0
array = [a for a in range(len(priorities))]
while True:
if priorities[index] < max(priorities[index+1:]):
priorities.append(priorities.pop(index))
array.append(array.pop(index))
else:
index += 1
if priorities == sorted(priorities, reverse=True):
break
answer = array.index(location)+1
return answer
이번껀 좀 힘들었다. 유독 스택/큐 문제를 풀때 많이 힘든듯한데.,,, 처음 풀었을때 위치 저장에서 막혀서 고민하다 찾아봤는데 결국 다른분들 코드를 보고 고치게 되는거같다... 반성반성... 찾아보는것보다 오래걸리더라도 생각하는 법을 배워야겠다. 화이팅..
~다른사람의 풀이
def solution(priorities, location):
queue = [(i,p) for i,p in enumerate(priorities)]
answer = 0
while True:
cur = queue.pop(0)
if any(cur[1] < q[1] for q in queue):
queue.append(cur)
else:
answer += 1
if cur[0] == location:
return answer
~eunmerate
반복문 사용 시 몇 번째 반복문인지 확인이 필요할 수 있다. 이때 사용.
인텍스 번호와 컬렉션의 원소를 tuple형태로 반환.
참고 : wikidocs.net/16045
>>> i = [1,2,4,5,6,8,9]
>>> for p in enumerate(i):
... print(p)
...
(0, 1)
(1, 2)
(2, 4)
(3, 5)
(4, 6)
(5, 8)
(6, 9)
>>> queue = [(i,p) for i,p in enumerate(i)]
>>> print(queue)
[(0, 1), (1, 2), (2, 4), (3, 5), (4, 6), (5, 8), (6, 9)]
~any
인자 중에서 참인 값이 하나라도 있을 경우 True를 리턴하고 모든 값이 거짓인 경우 False를 리턴한다.
참고:jaeworld.github.io/2018-09-24/python_any_all
>>> x = [True,False,True]
>>> print(any(x))
True