Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge: subtract_two_numbers_challenge #52

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions solutions/subtract_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# python3
# coding
"""creating on 01 03 2025
@author Geehan Ali + ChatGPT(phind)"""

from typing import Union


def subtract_two_numbers(
num1: Union[int, float], num2: Union[int, float]
misik-eng marked this conversation as resolved.
Show resolved Hide resolved
) -> Union[int, float]:
"""
Module for subtract two numbers and return the difference.

Args:
num1 (Union[int, float]): The first number to subtract.
num2 (Union[int, float]): The second number to subtract.

Returns:
Union[int, float]: The difference between num1 and num2.

Examples:
>>> subtract_two_numbers(5, 4)
1
>>> subtract_two_numbers(10, 2.5)
7.5
>>> subtract_two_numbers(-10, 10)
-20
>>> subtract_two_numbers(-12.5, -4)
-8.5
>>> subtract_two_numbers(-5.5, 2)
-7.5
>>> subtract_two_numbers(-5.5, -4.5)
-1

Raises:
TypeError: If either num1 or num2 is not a number (int or float).
misik-eng marked this conversation as resolved.
Show resolved Hide resolved

"""
assert isinstance(
misik-eng marked this conversation as resolved.
Show resolved Hide resolved
num1, (int, float)
), "First argument must be a number (int or float)"
assert isinstance(
num2, (int, float)
), "Second argument must be a number (int or float)"

return num1 - num2
60 changes: 60 additions & 0 deletions solutions/tests/test_subtract_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# python3
# coding
"""
Test module for subtract_two_numbers
creating on 01 03 2025
@author Geehan Ali + ChatGPT_phind
"""

import unittest

from ..subtract_two_numbers import subtract_two_numbers # type: ignore


class TestSubtractTwoNumbers(unittest.TestCase):
"""Test suite for subtract_two_numbers function"""

def test_normal_subtraction(self):
"""Test normal subtraction of two numbers."""
misik-eng marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(subtract_two_numbers(5, 3), 2)


def test_different_types(self):
"""Test normal subtraction of two numbers."""
self.assertEqual(subtract_two_numbers(5.5, 3), 2.5)


def test_edge_case_zero(self):
"""Test subtraction with zero as one operand."""
self.assertEqual(subtract_two_numbers(10, 0), 10)


def test_edge_case_max_value(self):
"""Test subtraction with maximum integer value."""
self.assertEqual(subtract_two_numbers(2147483647, 1), 2147483646)


def test_edge_case_min_value(self):
"""Test subtraction with minimum integer value."""
self.assertEqual(subtract_two_numbers(-2147483648, -1), -2147483647)


def test_float_and_large_numbers(self):
"""Test subtraction with floating-point numbers and large integers."""
self.assertAlmostEqual(subtract_two_numbers(0.001, 0.0009), 0.00010000000000000005)


def test_defensive_case_non_numeric_input(self):
"""Test handling of non-numeric input."""
with self.assertRaises(AssertionError):
misik-eng marked this conversation as resolved.
Show resolved Hide resolved
subtract_two_numbers("a", 5)


def test_defensive_case_invalid_type_input(self):
"""Test handling of invalid input type."""
with self.assertRaises(AssertionError):
subtract_two_numbers([10], 20)


if __name__ == "__main__":
unittest.main()
Loading