728x90
반응형
문제출처 : https://programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
똑같은 백준 문제 : https://www.acmicpc.net/problem/1966
1966번: 프린터 큐
첫 줄에 test case의 수가 주어진다. 각 test case에 대해서 문서의 수 N(100이하)와 몇 번째로 인쇄되었는지 궁금한 문서가 현재 Queue의 어떤 위치에 있는지를 알려주는 M(0이상 N미만)이 주어진다. 다음
www.acmicpc.net
def solution(priorities, location):
answer = 0
priorities_copy = [0 for _ in range(len(priorities))]
priorities_copy[location] = 1
while True:
if priorities[0] == max(priorities):
answer += 1
if not priorities_copy[0]:
del priorities[0]
del priorities_copy[0]
else:break
else:
priorities.append(priorities[0])
priorities_copy.append(priorities_copy[0])
del priorities_copy[0]
del priorities[0]
return answer
다른 사람의 풀이 보기를 해봤더니
python 내장함수인 any함수를 이용해서 푼 신기한 풀이가 있길래 공부가 되었다.
any함수를 이용한 풀이코드
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
python any 함수는 반복 가능한(iterable) 자료형 x를 입력 인수로 받으며 이 x의 요소 중 하나라도 참이 있으면 True를 돌려주고, x가 모두 거짓일 때에만 False를 돌려주는 함수이다. 파이썬의 내장 함수이다.
>>> any([0, ""])
False
>>> any([])
False
>>> any([1,2,3,0])
True
728x90
반응형
'Computer > PS' 카테고리의 다른 글
[프로그래머스] 레벨2 주식가격(스택/큐) - 파이썬 python (0) | 2020.11.16 |
---|---|
[프로그래머스] 레벨2 스킬트리(Summer/Winter Coding(~2018) - 파이썬 python (0) | 2020.11.16 |
[프로그래머스] 레벨2 124 나라의 숫자 (0) | 2020.11.15 |
[프로그래머스] 레벨2 위장 - 해시 파이썬 python (0) | 2020.11.14 |
[프로그래머스] 레벨2 올바른 괄호- 파이썬 python (0) | 2020.11.13 |