calculate_weapon_damage() 함수 구현
이 단계에서는 두 레이저 무기의 데미지 값을 계산하기 위해 calculate_weapon_damage() 함수를 구현합니다.
calculate_weapon_damage.py 파일 내에서 다음과 같은 시그니처로 calculate_weapon_damage() 함수를 정의합니다:
def calculate_weapon_damage(test1: List[int], test2: List[int]) -> List[int]:
## Your code here
pass
-
Weapon A 와 Weapon B 의 데미지 값을 계산하는 로직을 구현합니다. 다음 단계를 가이드로 사용할 수 있습니다:
- 입력 배열
test1과 test2가 올바른 길이 (각각 3 개의 요소) 를 갖는지 확인합니다. 그렇지 않으면 빈 목록 []을 반환합니다.
- 입력 배열의 값을
a1, b1, total1 및 a2, b2, total2 변수로 언팩합니다.
- 선형 방정식 시스템을 풀어 데미지 값
x (Weapon A) 와 y (Weapon B) 를 찾습니다. 다음 방정식을 사용할 수 있습니다:
a1 * x + b1 * y = total1
a2 * x + b2 * y = total2
- 계산된
x와 y 값을 정수로 변환하고 [x, y] 목록으로 반환합니다.
- 방정식을 푸는 동안
ZeroDivisionError가 발생하면 빈 목록 []을 반환합니다.
코드는 다음과 같습니다:
def calculate_weapon_damage(test1: List[int], test2: List[int]) -> List[int]:
if len(test1) != 3 or len(test2) != 3:
return []
a1, b1, total1 = test1
a2, b2, total2 = test2
## a1 * x + b1 * y = total1
## a2 * x + b2 * y = total2
try:
y = (total1 * a2 - total2 * a1) / (b1 * a2 - b2 * a1)
x = (total1 - b1 * y) / a1
except ZeroDivisionError:
return []
return [int(x), int(y)]
-
calculate_weapon_damage() 함수를 테스트하기 위해 파일 끝에 다음 코드를 추가합니다:
if __name__ == "__main__":
print(calculate_weapon_damage([4, 5, 22], [3, 2, 13]))
이 코드는 스크립트를 실행할 때 계산된 데미지 값을 출력합니다.