Calculate Defective Rate for Clone Human

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to calculate the defective rate of clone humans produced by the clone factories in the Galactic Empire.

👀 Preview

defective_rate=0.04

🎯 Tasks

In this project, you will learn:

  • How to set up the project environment
  • How to understand the project requirements
  • How to implement the calculate_defective_rate function
  • How to test the calculate_defective_rate function
  • How to refine and optimize the solution

🏆 Achievements

After completing this project, you will be able to:

  • Set up a Python project environment
  • Understand and interpret project requirements
  • Implement a function to solve a specific problem
  • Test and validate the correctness of your implementation
  • Explore ways to optimize and refine your solution

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/numeric_types -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/strings -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/conditional_statements -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/for_loops -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/lists -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/tuples -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/sets -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/function_definition -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/importing_modules -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/using_packages -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/data_collections -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/python_shell -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} python/build_in_functions -.-> lab-302695{{"`Calculate Defective Rate for Clone Human`"}} end

Understand the Project Requirements

In this step, you will understand the requirements of the project.

The goal of this project is to calculate the defective rate of clone humans produced by the clone factories in the Galactic Empire.

The key requirements are:

  1. Complete the calculate_defective_rate function in calculate_defective_rate.py.
  2. The function takes a 2D matrix representing the production of clone IDs and returns the defect rate of the cloned humans produced by the factories.
  3. The function should handle the case where the input array is empty.
  4. The function should return a floating-point number.

Implement the calculate_defective_rate Function

In this step, you will implement the calculate_defective_rate function in the calculate_defective_rate.py file.

  1. Open the calculate_defective_rate.py file in your preferred code editor.
  2. Locate the calculate_defective_rate function and replace the existing code with the following implementation:
def calculate_defective_rate(matrix: List[List[str]]) -> float:
    """
    Calculate the defective rate of clone production based on the clone ID matrix.

    Args:
        matrix (List[List[str]]): A 2D matrix representing the clone ID production.

    Returns:
        float: The defective rate, which is the ratio of duplicate clones to total clones produced.
    """
    if not matrix:
        return 0.0

    total_clones = 0
    duplicates = 0

    clone_set = set()

    for row in matrix:
        for clone in row:
            total_clones += 1
            if clone in clone_set:
                duplicates += 1
            else:
                clone_set.add(clone)

    return round(duplicates / total_clones, 2)

This implementation first checks if the input matrix is empty, and if so, returns a defective rate of 0.0.

Then, it iterates through the matrix, keeping track of the total number of clones and the number of duplicate clones. It uses a set clone_set to store the unique clone IDs.

Finally, the function calculates the defective rate by dividing the number of duplicate clones by the total number of clones, and rounds the result to two decimal places.

Test the calculate_defective_rate Function

In this step, you will test the calculate_defective_rate function with the provided example.

  1. Open a new terminal window and navigate to the /home/labex/project directory.
  2. Run the following command to execute the calculate_defective_rate.py script:
python3 calculate_defective_rate.py
  1. When prompted, enter the 2D matrix example provided in the challenge:
python3 calculate_defective_rate.py
>>> Enter a 2D matrix:[['1000ffd8', '1000ffc8', '1000ffdb', '1000ffbc', '1000ffba'],
                       ['1000ffd2', '1000ffbd', '1000ffd7', '1000ffcb', '1000ffd6'],
                       ['1000ffc9', '1000ffab', '1000ffb8', '1000ffd5', '1000ffad'],
                       ['1000ffc7', '1000ffcf', '1000ffb7', '1000ffae', '1000ffc1'],
                       ['1000ffb9', '1000ffce', '1000ffc3', '1000ffd1', '1000ffc8'],
                       ['1000ffd1', '1000ffb2', '1000ffcd', '1000ffda', '1000ffcc'],
                       ['1000ffc2', '1000ffc4', '1000ffca', '1000ffb3', '1000ffaa'],
                       ['1000ffbf', '1000ffbe', '1000ffc5', '1000ffc0', '1000ffd0'],
                       ['1000ffb4', '1000ffc6', '1000ffd9', '1000ffb1', '1000ffb5'],
                       ['1000ffb6', '1000ffb0', '1000ffd3', '1000ffd4', '1000ffbb']]
  1. The output should display the calculated defective rate:
defective_rate=0.04

This matches the expected result from the challenge, which means the calculate_defective_rate function is working correctly.

Refine and Optimize the Solution

In this final step, you can optionally refine and optimize the calculate_defective_rate function further. Here are some ideas:

  1. Improve Efficiency: The current implementation has a time complexity of O(n^2), where n is the total number of clones. You can explore ways to optimize the algorithm, such as using a more efficient data structure to keep track of the unique clone IDs.
  2. Add Error Handling: Enhance the function to handle invalid inputs, such as matrices with non-hexadecimal clone IDs or matrices with different row lengths.
  3. Enhance Readability: You can improve the readability of the code by adding more comments, using more descriptive variable names, and following best practices for Python code style.
  4. Implement Unit Tests: Write unit tests to ensure the correctness of the calculate_defective_rate function and to make it easier to maintain the code in the future.

Feel free to explore these ideas and make any improvements you deem necessary to the solution.

Summary

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

Other Python Tutorials you may like