forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from MIT-Emerging-Talent/specific_calculator
adding my spacific gravity calculator
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for calculating the specific gravity of a substance. | ||
Module contents: | ||
- calculate_specific_gravity: calculates the specific gravity of a substance given its density. | ||
Created on 06/01/2025 | ||
@author: Novel Yonas | ||
Specific Gravity Calculator: | ||
This module provides functionality to calculate the specific gravity | ||
of a substance. | ||
Specific gravity is a dimensionless quantity that compares the density of a substance | ||
to the density of a reference substance (typically water at 4°C, which has a density | ||
of 1000 kg/m³). | ||
The specific gravity is calculated using the formula: | ||
Specific Gravity = Density of substance / Density of reference substance | ||
In this module, we use water at 4°C as the reference substance, with a density of 1000 kg/m³. | ||
Usage: | ||
The main function in this module is 'calculate_specific_gravity', which takes | ||
the density of a substance as input and returns its specific gravity. | ||
""" | ||
|
||
|
||
def calculate_specific_gravity(a, w): | ||
""" | ||
Calculates the specific gravity of a rock mass. | ||
Args: | ||
a: Float, weight of the rock mass measured in air. | ||
w: Float, weight of the rock mass measured in water. | ||
Returns: | ||
Float: The specific gravity of the rock mass. | ||
None: If the weight in air is less than or equal to the weight in water. | ||
Raises: | ||
ValueError: If invalid inputs are provided or the calculation cannot proceed. | ||
Examples: | ||
>>> calculate_specific_gravity(4, 2) | ||
2.0 | ||
>>> calculate_specific_gravity(5, 2) | ||
1.6666666666666667 | ||
>>> calculate_specific_gravity(2, 2) | ||
""" | ||
if a <= w: | ||
return None # Return None to indicate an error | ||
return a / (a - w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Unit test class for the `calculate_specific_gravity` function. | ||
This class contains test cases to validate the functionality | ||
and edge cases of the | ||
`calculate_specific_gravity` function, ensuring it handles valid inputs, invalid inputs, | ||
and edge conditions gracefully. | ||
Test cases include: | ||
- Valid inputs where the specific gravity is correctly calculated. | ||
- Cases where the weight in air equals or is less than the weight in water. | ||
- Inputs with negative values to check for proper error handling. | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.specific_gravity_calculator import calculate_specific_gravity | ||
|
||
|
||
class TestCalculateSpecificGravity(unittest.TestCase): | ||
"""specific_gravity_calculator""" | ||
|
||
def test_valid_input(self): | ||
"""Test with valid inputs.""" | ||
self.assertAlmostEqual(calculate_specific_gravity(4, 2), 2.0) | ||
self.assertAlmostEqual(calculate_specific_gravity(5, 2), 1.6666666666666667) | ||
|
||
def test_equal_weights(self): | ||
"""Test when weight in air is equal to weight in water.""" | ||
self.assertIsNone(calculate_specific_gravity(2, 2)) | ||
|
||
def test_weight_in_air_less_than_water(self): | ||
"""Test when weight in air is less than weight in water.""" | ||
self.assertIsNone(calculate_specific_gravity(1, 2)) | ||
|
||
def test_invalid_inputs(self): | ||
"""Test with invalid inputs like negative values.""" | ||
self.assertIsNone(calculate_specific_gravity(-4, 2)) | ||
self.assertIsNone(calculate_specific_gravity(-4, -2)) |