ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 카카오 신입 공채 1차 코딩 테스트 문제 4.셔틀버스(난이도: 중) (Python)
    공부 2017. 12. 4. 16:14
    반응형

    2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 4.셔틀버스(난이도: 중)


    Java에 이어 Python으로도 작성해 봤다.



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    def question4(n, t, m, time_table):
        answer = ""
     
        total_min = (n - 1* t
        last_hour = 9 + total_min // 60
        last_min = total_min % 60
     
        index_last_crew = 0
     
        shuttle_time_table = ["09:00"]
        for i in range(1, n):
            shuttle_time_table.append(get_time_after_x_min_from_0900(i * t))
     
        time_table.sort()
     
        for i in range(0, n):
            for j in range(0, m):
                if len(time_table) <= index_last_crew:
                    index_last_crew = -1
                    break
     
                shuttle_time = shuttle_time_table[i]
                crew_time = time_table[index_last_crew]
     
                if get_hour(crew_time) < get_hour(shuttle_time):
                    index_last_crew += 1
                elif get_hour(crew_time) == get_hour(shuttle_time):
                    if get_min(crew_time) <= get_min(shuttle_time):
                        index_last_crew += 1
                    else:
                        break
                elif get_hour(crew_time) > get_hour(shuttle_time):
                    break
     
        if index_last_crew <= 0 or index_last_crew > len(time_table):
            answer = '{0:02d}'.format(last_hour) + ':' + '{0:02d}'.format(last_min)
        else:
            temp_last_shuttle = time_table[index_last_crew - 1]
            if get_min(temp_last_shuttle) > 0:
                answer = '{0:02d}'.format(get_hour(temp_last_shuttle))
                answer += ':'
                answer += '{0:02d}'.format(get_min(temp_last_shuttle) - 1)
            if get_min(temp_last_shuttle) == 0:
                answer = '{0:02d}'.format(get_hour(temp_last_shuttle) - 1)
                answer += ':'
                answer += '{0:02d}'.format(59)
     
        return answer
     
     
    def get_time_after_x_min_from_0900(x):
        if x <= -60:
            h = '{0:02d}'.format(8 + (x // 60))
            m = '{0:02d}'.format(60 - (x % 60))
            return h + ':' + m
        elif -60 < x < 0:
            h = '{0:02d}'.format(8)
            m = '{0:02d}'.format(60 + x)
            return h + ':' + m
        elif 0 <= x < 60:
            h = '{0:02d}'.format(9)
            m = '{0:02d}'.format(x)
            return h + ':' + m
        elif 60 <= x:
            h = '{0:02d}'.format(9 + (x // 60))
            m = '{0:02d}'.format(x % 60)
            return h + ':' + m
     
     
    def get_hour(time):
        return int(time.split(':')[0])
     
     
    def get_min(time):
        return int(time.split(':')[1])
     
    cs


    반응형
Designed by Tistory.