forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #90 from MIT-Emerging-Talent/divide-two-numbers
Adding divide two numbers
- Loading branch information
Showing
3 changed files
with
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on 2024-01-06 | ||
@author: Henry Ogoe | ||
""" | ||
|
||
|
||
def convert_salary(hourly: float, hours_per_week: float = 40) -> tuple: | ||
""" | ||
Convert a hourly salary to monthly and yearly assuming full time (40 hours per week). | ||
Parameters: | ||
hourly (float): Hourly salary in dollars. | ||
hours_per_week: Amount of hours per week is set to 40. Added for flexibility. | ||
Returns: | ||
float: Monthly and yearly salary. | ||
Raises: | ||
AssertionError: If the input is not a float or salary is negative. | ||
>>> convert_salary(16.5) | ||
2857.8 34320.0 | ||
>>> convert_salary(25) | ||
4333.0 51960.0 | ||
>>> convert_salary(-40) | ||
Traceback (most recent call last): | ||
AssertionError: Salary cannot be negative. | ||
>>> convert_salary("7") | ||
Traceback (most recent call last): | ||
AssertionError: Inputs must be float. | ||
""" | ||
|
||
assert isinstance(hourly, float), "Inputs must be float." | ||
assert hourly > 0, "Salary can not be negative." | ||
|
||
monthly_salary = hourly * hours_per_week * 4.33 | ||
yearly_salary = hourly * hours_per_week * 52 | ||
|
||
return (round(monthly_salary, 2), round(yearly_salary, 2)) |
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,45 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module to divide two numbers and return the result. | ||
Created 2025-01-04 | ||
@author: Henry Ogoe | ||
""" | ||
|
||
|
||
def divide_numbers(numerator: int, denominator: int) -> int: | ||
"""Divides numerator by denominator and returns the quotient. | ||
Parameters: | ||
numerator(int): The first parameter | ||
denominator(int): The second parameter | ||
Returns: | ||
int: Result of division of numerator by denominator. | ||
Raises: | ||
TypeError: If the input contains non-integer elements. | ||
ValueError: If denominator is 0. | ||
Examples: | ||
>>> divide_numbers(4, 2) | ||
2 | ||
>>> divide_numbers(64, 4) | ||
16 | ||
>>> divide_numbers("4", 2) | ||
Traceback (most recent call last): | ||
TypeError: Inputs must be integers. | ||
>>> divide_numbers(8,0) | ||
Traceback (most recent call last): | ||
ValueError: Please dont divide by zero. | ||
""" | ||
|
||
if not isinstance(numerator, int) or not isinstance(denominator, int): | ||
raise TypeError("Inputs must be integers.") | ||
if denominator == 0: | ||
raise ValueError("Please dont divide by zero.") | ||
return numerator // denominator |
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,65 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for dividing two numbers function. | ||
Created 2025-01-04 | ||
@author: Henry Ogoe | ||
""" | ||
|
||
import unittest | ||
from solutions.divide_two_numbers import divide_numbers | ||
|
||
|
||
class TestDivideTwoNumbers(unittest.TestCase): | ||
"""The test for testing divide_numbers function.""" | ||
|
||
def test_positive_numbers(self): | ||
"""It should return positive number 2""" | ||
self.assertEqual(divide_numbers(6, 3), 2) | ||
|
||
def test_positive_negative_numbers(self): | ||
"""It should divide negative numerator by positive denominator""" | ||
self.assertEqual(divide_numbers(-6, 2), -3) | ||
|
||
def test_both_negative_numbers(self): | ||
"""It should correctly divide negative numbers""" | ||
self.assertEqual(divide_numbers(-8, -2), 4) | ||
|
||
def test_large_numbers(self): | ||
"""It should correctly divide large numbers""" | ||
self.assertEqual(divide_numbers(2000000, 1000000), 2) | ||
|
||
def test_zero_and_number(self): | ||
"""It should return zero if numerator is zero""" | ||
self.assertEqual(divide_numbers(0, 5), 0) | ||
|
||
def test_zero_denominator(self): | ||
"""It should return Value error for zero in denominator(b)""" | ||
with self.assertRaises(ValueError): | ||
divide_numbers(46, 0) | ||
|
||
def test_no_arguments(self): | ||
"""It should raise an error when no argument are provided""" | ||
with self.assertRaises(TypeError): | ||
divide_numbers() | ||
|
||
def test_float_input(self): | ||
"""It should raise an error for float input""" | ||
with self.assertRaises(TypeError): | ||
divide_numbers(6.25, 3) | ||
|
||
def test_non_integer_input(self): | ||
"""It should raise TypeError for a non-integer input""" | ||
with self.assertRaises(TypeError): | ||
divide_numbers("4", 2) | ||
|
||
def test_one_argument(self): | ||
"""It should raise TypeError when only one argument is provided""" | ||
with self.assertRaises(TypeError): | ||
divide_numbers(2) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |