Skip to content

Commit

Permalink
Merge pull request #88 from MIT-Emerging-Talent/specific_calculator
Browse files Browse the repository at this point in the history
adding my spacific gravity calculator
  • Loading branch information
WuorBhang authored Jan 9, 2025
2 parents ffe6df9 + 9ce3869 commit 9ded153
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "ET: Debug Python (unittest)",
"type": "debugpy",
Expand Down
56 changes: 56 additions & 0 deletions solutions/specific_gravity_calculator.py
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)
41 changes: 41 additions & 0 deletions solutions/tests/test_specific_gravity_calculator.py
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))

0 comments on commit 9ded153

Please sign in to comment.