Calculating Laser Weapon Damage

PythonPythonBeginner
Practice Now

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.

  1. Open the calculate_weapon_damage.py file in a text editor.
  2. 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.

  1. Inside the calculate_weapon_damage.py file, define the calculate_weapon_damage() function with the following signature:
def calculate_weapon_damage(test1: List[int], test2: List[int]) -> List[int]:
    ## Your code here
    pass
  1. 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 test1 and test2 have the correct length (3 elements each). If not, return an empty list [].
    • Unpack the values from the input arrays into variables a1, b1, total1 and a2, b2, total2.
    • Solve the system of linear equations to find the damage values x (for Weapon A) and y (for Weapon B). You can use the following equations:
    a1 * x + b1 * y = total1
    a2 * x + b2 * y = total2
    • Convert the calculated x and y values to integers and return them as a list [x, y].
    • If there is a ZeroDivisionError while 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)]
  2. 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.

  1. Save the calculate_weapon_damage.py file.
  2. Open a terminal and navigate to the /home/labex/project directory.
  3. Run the calculate_weapon_damage.py script:
python calculate_weapon_damage.py

You should see the output [3, 2], which is the expected result for the given example data.

  1. 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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like