<aside> 💬 lv1, 자료 구조

응급환자 발생

문제 설명

실리콘벨리로 출장을 가는 도중 응급환자가 다수 발생했습니다. 현재 위치는 바다 한 가운데이며, 가장 빠른 시간 내에 환자를 치료할 수 있는 공항으로 이동해야 합니다. 각 공항과의 거리와 그 공항에서 치료할 수 있는 병명이 포함된 JSON 형태의 데이터 airportData와 현재 환자 목록을 담은 patientData가 주어집니다. 가장 최적의 경로를 찾아 방문하는 공항의 이름을 출력하는 코드를 작성하세요.


제한 사항


입출력 예

입력

[
    [
        # airportData
        {
            "name": "Airport A",
            "distance": 500,
            "treatableDiseases": ["Disease A", "Disease B"]
        },
        {
            "name": "Airport B",
            "distance": 300,
            "treatableDiseases": ["Disease C"]
        },
        {
            "name": "Airport C",
            "distance": 400,
            "treatableDiseases": ["Disease B", "Disease C"]
        }
    ],
    ["Disease A", "Disease C"] # patientData
]

출력

"Airport B", "Airport A"

입출력 설명:

😺 풀이 1. 정렬, 조건문

def solution(data):
    airportData, patientData = data **#1 언패킹**
    sorted_airports = sorted(airportData, key=lambda x: x["distance"]) **#2 정렬**
    treatable_airports = []
    for disease in patientData: **#3 공항 찾기**
        for airport in sorted_airports:
            if disease in airport["treatableDiseases"]:
                if airport["name"] not in treatable_airports:
                    treatable_airports.append(airport["name"])
                break

    return treatable_airports **#4 결과 반환**

단계별 풀이 전략

  1. 언패킹

    airportDatapatientDatadata를 언패킹합니다.

  2. 정렬

    airportData를 거리에 따라 정렬합니다.

  3. 공항 찾기

    질병과 공항 데이터를 반복문을 통해 반복하여 질병이 공항 데이터에 등록되어 있는지 하나씩 확인합니다. 질병을 치료할 수 있는 공항을 찾는 다면 treatable_airports 리스트에 추가한 후 루프를 종료합니다.

  4. 결과 반환

    결과를 반환합니다.

알아둬야 할 개념

append

풀이 2. 정렬, 조건문

def solution(data):
    airports, patientData = data **#1 언패킹**

    n_dict = {i["name"]:(i["distance"], i["treatableDiseases"]) for i in airports}
    air_list = list() **#2 딕셔너리 및 리스트 생성**

    n_d = sorted(n_dict.items() ,key = lambda x: x[1][0]) **#3 정렬**

    for i in n_d: **#4 공항 찾기**
        if i[1][1] == patientData:
            return [i[0]]

    for i in n_d:
        for treat in i[1][1]:
            if treat in patientData:
                patientData.remove(treat)
                air_list.append(i[0])

    return air_list **#5 결과 반환**

단계별 풀이 전략

  1. 언패킹

    입력 받은 데이터를 언패킹합니다.

  2. 딕셔너리 생성

    공항 정보의 값을 딕셔너리로 생성합니다.

  3. 정렬

    공항 딕셔너리를 공항까지의 거리를 기준으로 정렬합니다.

  4. 공항 찾기

    정렬된 공항들을 순회하며 치료 가능한 질병과 일치하는 공항을 찾습니다. 위의 과정에서 일치하는 공항을 찾지 못한다면 환자 데이터와 일치하는 질병이 있는 공항을 찾아 리스트로 만들어 반환합니다.