본문 바로가기
코딩테스트 파이썬/파이썬 프로그래머스 2단계

프로세스

by 세용용용용 2023. 7. 8.

나의 코드

def solution(priorities, location):
    answer = 0
    #priorities의 값과 인덱스를 추가한 리스트 생성
    new_list = []
    for i in range(len(priorities)):
        new_list.append([priorities[i], i])
    print(new_list)

    #언제 프로세스를 꺼내는지 카운트를 하자
    count = 0

    #원하는 location의 값이 실행될떄까지 반복하자
    while True:

        #현재 우선순위가 가장 높은 값
        first_start = max(priorities)

        #우선순위가 높은 값이 앞에 올떄까지
        #낮은 우선순위의 프로세스를 뒤로 보내준다
        while True:
            if new_list[0][0] == first_start:
                break
            else:
                new_list.append(new_list.pop(0))


        count+=1
        #원하는 프로세스가 현재 실행될 프로세스면 break
        if new_list[0][1] == location:
            break
        #그렇지 않으면 프로세스를 실행시키고 다시 반복문 시작
        else:
            new_list.pop(0)
            priorities.remove(first_start)
    #print(new_list)
    answer = count
    #print(answer)
    return answer
solution([1, 1, 9, 1, 1, 1], 0)

 

'코딩테스트 파이썬 > 파이썬 프로그래머스 2단계' 카테고리의 다른 글

피로도  (0) 2023.07.11
[1차] 뉴스 클러스터링  (1) 2023.07.10
기능개발  (0) 2023.07.08
할인행사  (0) 2023.07.08
튜플  (0) 2023.07.07