Introduction
In this project, you will learn how to calculate the damage values of two high-performance laser weapons developed by the Galactic Empire's weapon research institute. The weapon equipment lab is responsible for testing the weapon values, and you will help them by implementing the damage calculation process.
$ python calculate_weapon_damage.py
[3, 2]
🎯 Tasks
In this project, you will learn:
- How to set up the project environment and create the necessary files
- How to implement the
calculate_weapon_damage()function to calculate the damage values of the two laser weapons - How to test the
calculate_weapon_damage()function with the provided example data
🏆 Achievements
After completing this project, you will be able to:
- Understand the problem statement and the requirements for the laser weapon damage calculation
- Implement a function to solve a system of linear equations and calculate the damage values of the laser weapons
- Test the implemented function with different input data to ensure it works correctly
- Apply your problem-solving and programming skills to a real-world scenario
Set Up the Project Environment
In this step, you will set up the project environment and modify the necessary files for the project.
- Open the
calculate_weapon_damage.pyfile in a text editor. - At the top of the file, add the following import statement:
from typing import List
This will allow you to use the List type annotation in your code.
Implement the calculate_weapon_damage() Function
In this step, you will implement the calculate_weapon_damage() function to calculate the damage values of the two laser weapons.
- Inside the
calculate_weapon_damage.pyfile, define thecalculate_weapon_damage()function with the following signature:
def calculate_weapon_damage(test1: List[int], test2: List[int]) -> List[int]:
## Your code here
pass
Implement the logic to calculate the damage values of Weapon A and Weapon B. You can use the following steps as a guide:
- Check if the input arrays
test1andtest2have the correct length (3 elements each). If not, return an empty list[]. - Unpack the values from the input arrays into variables
a1,b1,total1anda2,b2,total2. - Solve the system of linear equations to find the damage values
x(for Weapon A) andy(for Weapon B). You can use the following equations:
a1 * x + b1 * y = total1 a2 * x + b2 * y = total2- Convert the calculated
xandyvalues to integers and return them as a list[x, y]. - If there is a
ZeroDivisionErrorwhile solving the equations, return an empty list[].
The code is as follows:
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)]- Check if the input arrays
Add the following code at the end of the file to test the
calculate_weapon_damage()function:
if __name__ == "__main__":
print(calculate_weapon_damage([4, 5, 22], [3, 2, 13]))
This will print the calculated damage values when you run the script.
Test the calculate_weapon_damage() Function
In this step, you will test the calculate_weapon_damage() function with the provided example data.
- Save the
calculate_weapon_damage.pyfile. - Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the
calculate_weapon_damage.pyscript:
python calculate_weapon_damage.py
You should see the output [3, 2], which is the expected result for the given example data.
- Try testing the function with other input data to ensure it works correctly. For example, you can try passing in
[4, 5, 22]and[3, 2, 14]to see if the function returns an empty list[]due to the invalid input.
Congratulations! You have successfully implemented the calculate_weapon_damage() function to calculate the damage values of the two laser weapons.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



