-
카카오 신입 공채 1차 코딩 테스트 문제 4.셔틀버스(난이도: 중) (Python)공부 2017. 12. 4. 16:14반응형
2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 4.셔틀버스(난이도: 중)
Java에 이어 Python으로도 작성해 봤다.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576def question4(n, t, m, time_table):answer = ""total_min = (n - 1) * tlast_hour = 9 + total_min // 60last_min = total_min % 60index_last_crew = 0shuttle_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 = -1breakshuttle_time = shuttle_time_table[i]crew_time = time_table[index_last_crew]if get_hour(crew_time) < get_hour(shuttle_time):index_last_crew += 1elif get_hour(crew_time) == get_hour(shuttle_time):if get_min(crew_time) <= get_min(shuttle_time):index_last_crew += 1else:breakelif get_hour(crew_time) > get_hour(shuttle_time):breakif 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 answerdef 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 + ':' + melif -60 < x < 0:h = '{0:02d}'.format(8)m = '{0:02d}'.format(60 + x)return h + ':' + melif 0 <= x < 60:h = '{0:02d}'.format(9)m = '{0:02d}'.format(x)return h + ':' + melif 60 <= x:h = '{0:02d}'.format(9 + (x // 60))m = '{0:02d}'.format(x % 60)return h + ':' + mdef get_hour(time):return int(time.split(':')[0])def get_min(time):return int(time.split(':')[1])cs 반응형