-
[PCCP 기출문제] 1번 / 붕대 감기코딩 테스트/Level 1 2024. 4. 3. 11:14반응형
https://school.programmers.co.kr/learn/courses/30/lessons/250137
def solution(bandage, health, attacks): max_health = health success = 0 attacks_dict = {time: attack for time, attack in attacks} for time in range(1, max(attacks_dict) + 1): if time in attacks_dict: health -= attacks_dict[time] success = 0 elif health < max_health: health = min(max_health, health + bandage[1]) success += 1 if success == bandage[0]: health = min(max_health, health + bandage[2]) success = 0 if health <= 0: return -1 return health
논리적으로는 이것도 나쁘진 않은데
코딩에서 if 문이 과하게 깊어지는 건 피하는 것이 좋다. (가독성)def solution(bandage, health, attacks): max_health = health success = 0 attacks_dict = {time: attack for time, attack in attacks} for time in range(1, max(attacks_dict) + 1): if time in attacks_dict: health -= attacks_dict[time] success = 0 if health <= 0: return -1 elif health < max_health: # 어택이 없고, 풀(full)피(health)가 아닐 때 health = min(max_health, health + bandage[1]) success += 1 if success == bandage[0]: health = min(max_health, health + bandage[2]) success = 0 return health
반응형