From c2acb43b8e7947a57bc8858431d0bc7d4d7a7896 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Sat, 28 Dec 2024 19:09:59 -0500 Subject: [PATCH 01/17] feat: create basic calculator folder to start solving challenge #14 --- solutions/basic_calculator/main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solutions/basic_calculator/main.py diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/main.py new file mode 100644 index 000000000..e69de29bb From ae216388a8a5841a7dc8a41ac55c4b6c94c31f94 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Sun, 29 Dec 2024 16:38:08 -0500 Subject: [PATCH 02/17] "add solution for performing basic arithmetic operations" --- solutions/basic_calculator/main.py | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/main.py index e69de29bb..8403bc92a 100644 --- a/solutions/basic_calculator/main.py +++ b/solutions/basic_calculator/main.py @@ -0,0 +1,70 @@ + + +""" +A module for performing basic arithmetic operations: addition, subtraction, +multiplication, and division. + +Module contents: + - calculate: Performs the specified arithmetic operation on two numbers + +Created on 28/12/2024 +Author: Ana Isabel Murillo +""" + +def calculate(operation: str, num1: float, num2: float) -> float: + """Performs the specified arithmetic operation on two numbers. + + Parameters: + operation: str, the operation to perform ('add', 'subtract', 'multiply', 'divide') + num1: float, the first number + num2: float, the second number + + Returns -> float: the result of the arithmetic operation + + Raises: + ValueError: if the operation is invalid or division by zero occurs + + Examples: + >>> calculate('add', 5, 3) + 8.0 + >>> calculate('subtract', 10, 4) + 6.0 + >>> calculate('multiply', 7, 3) + 21.0 + >>> calculate('divide', 9, 3) + 3.0 + >>> calculate('divide', 5, 0) + Traceback (most recent call last): + ... + ValueError: Cannot divide by zero. + """ + if operation == 'add': + return num1 + num2 + elif operation == 'subtract': + return num1 - num2 + elif operation == 'multiply': + return num1 * num2 + elif operation == 'divide': + if num2 == 0: + raise ValueError("Cannot divide by zero.") + return num1 / num2 + else: + raise ValueError(f"Invalid operation '{operation}'. Valid operations are: add, subtract, multiply, divide.") + +if __name__ == "__main__": + print("Welcome to the Basic Calculator!") + print("Available operations: add, subtract, multiply, divide") + + while True: + try: + operation = input("Enter the operation: ").strip().lower() + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + + result = calculate(operation, num1, num2) + print(f"The result of {operation} operation is: {result}") + break # Exit the loop if the operation is successful + + except ValueError as e: + print(f"Error: {e}") + print("Let's try again!") From 739f957bfe070dd8eac7c951b94860d41d6a36d8 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Wed, 1 Jan 2025 17:43:57 -0500 Subject: [PATCH 03/17] add the test for solution basic calculator --- __init__.py | 0 requirements.txt | Bin 0 -> 28 bytes solutions/__init__.py | 0 solutions/basic_calculator/__init__.py | 0 solutions/tests/__init__.py | 0 solutions/tests/test_basic_calculator.py | 54 +++++++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 __init__.py create mode 100644 requirements.txt create mode 100644 solutions/__init__.py create mode 100644 solutions/basic_calculator/__init__.py create mode 100644 solutions/tests/__init__.py create mode 100644 solutions/tests/test_basic_calculator.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..38743267b60f34ea8c94d5c9dd1e4f27b011cfb5 GIT binary patch literal 28 ecmezWuZW?PAq@y^fzW_KkHG?nO&E9?xEKI%6a_i} literal 0 HcmV?d00001 diff --git a/solutions/__init__.py b/solutions/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/basic_calculator/__init__.py b/solutions/basic_calculator/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/__init__.py b/solutions/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py new file mode 100644 index 000000000..58550519b --- /dev/null +++ b/solutions/tests/test_basic_calculator.py @@ -0,0 +1,54 @@ +""" +Test module for basic calculator. + +Test categories: + - Standard cases: addition, subtraction, multiplication, and division + - Edge cases: division by zero, invalid operations + - Defensive tests: ensure proper exceptions are raised for invalid inputs + +Created on 2024-12-29 +Author: Ana Isabel Murillo +""" + +from solutions.basic_calculator.main import calculate +import unittest + + +class TestCalculate(unittest.TestCase): + """Test suite for the calculate function.""" + + def test_addition(self): + """It should correctly add two numbers.""" + self.assertEqual(calculate('add', 5, 3), 8.0) + self.assertEqual(calculate('add', -5, -3), -8.0) + self.assertEqual(calculate('add', 0, 5), 5.0) + + def test_subtraction(self): + """It should correctly subtract the second number from the first.""" + self.assertEqual(calculate('subtract', 10, 4), 6.0) + self.assertEqual(calculate('subtract', -5, -5), 0.0) + self.assertEqual(calculate('subtract', 0, 5), -5.0) + + def test_multiplication(self): + """It should correctly multiply two numbers.""" + self.assertEqual(calculate('multiply', 7, 3), 21.0) + self.assertEqual(calculate('multiply', -2, 3), -6.0) + self.assertEqual(calculate('multiply', 0, 10), 0.0) + + def test_division(self): + """It should correctly divide the first number by the second.""" + self.assertEqual(calculate('divide', 9, 3), 3.0) + self.assertEqual(calculate('divide', -6, 3), -2.0) + self.assertAlmostEqual(calculate('divide', 7, 2), 3.5) + + def test_division_by_zero(self): + """It should raise a ValueError when attempting to divide by zero.""" + with self.assertRaises(ValueError) as context: + calculate('divide', 5, 0) + self.assertEqual(str(context.exception), "Cannot divide by zero.") + + def test_invalid_operation(self): + """It should raise a ValueError for an invalid operation.""" + with self.assertRaises(ValueError) as context: + calculate('invalid_op', 5, 3) + self.assertIn("Invalid operation", str(context.exception)) From 286862566027a5804ba8355e08ef770082c143ac Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Wed, 1 Jan 2025 17:55:55 -0500 Subject: [PATCH 04/17] remove manual test --- solutions/basic_calculator/main.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/main.py index 8403bc92a..7f9aacaec 100644 --- a/solutions/basic_calculator/main.py +++ b/solutions/basic_calculator/main.py @@ -50,21 +50,3 @@ def calculate(operation: str, num1: float, num2: float) -> float: return num1 / num2 else: raise ValueError(f"Invalid operation '{operation}'. Valid operations are: add, subtract, multiply, divide.") - -if __name__ == "__main__": - print("Welcome to the Basic Calculator!") - print("Available operations: add, subtract, multiply, divide") - - while True: - try: - operation = input("Enter the operation: ").strip().lower() - num1 = float(input("Enter the first number: ")) - num2 = float(input("Enter the second number: ")) - - result = calculate(operation, num1, num2) - print(f"The result of {operation} operation is: {result}") - break # Exit the loop if the operation is successful - - except ValueError as e: - print(f"Error: {e}") - print("Let's try again!") From f88df2316684d1772400681c1ab28a415dd4ad43 Mon Sep 17 00:00:00 2001 From: RamonColmenares Date: Wed, 1 Jan 2025 18:07:38 -0500 Subject: [PATCH 05/17] fix issues with ruff linter --- solutions/basic_calculator/main.py | 19 +++++++++------ solutions/tests/test_basic_calculator.py | 31 ++++++++++++------------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/main.py index 7f9aacaec..39d6bd597 100644 --- a/solutions/basic_calculator/main.py +++ b/solutions/basic_calculator/main.py @@ -1,5 +1,3 @@ - - """ A module for performing basic arithmetic operations: addition, subtraction, multiplication, and division. @@ -11,11 +9,13 @@ Author: Ana Isabel Murillo """ + def calculate(operation: str, num1: float, num2: float) -> float: """Performs the specified arithmetic operation on two numbers. Parameters: - operation: str, the operation to perform ('add', 'subtract', 'multiply', 'divide') + operation: str, the operation to perform + ('add', 'subtract', 'multiply', 'divide') num1: float, the first number num2: float, the second number @@ -38,15 +38,18 @@ def calculate(operation: str, num1: float, num2: float) -> float: ... ValueError: Cannot divide by zero. """ - if operation == 'add': + if operation == "add": return num1 + num2 - elif operation == 'subtract': + elif operation == "subtract": return num1 - num2 - elif operation == 'multiply': + elif operation == "multiply": return num1 * num2 - elif operation == 'divide': + elif operation == "divide": if num2 == 0: raise ValueError("Cannot divide by zero.") return num1 / num2 else: - raise ValueError(f"Invalid operation '{operation}'. Valid operations are: add, subtract, multiply, divide.") + raise ValueError( + f"Invalid operation '{operation}'. Valid operations are: " + "add, subtract, multiply, divide." + ) diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py index 58550519b..1e9d80b76 100644 --- a/solutions/tests/test_basic_calculator.py +++ b/solutions/tests/test_basic_calculator.py @@ -10,45 +10,46 @@ Author: Ana Isabel Murillo """ -from solutions.basic_calculator.main import calculate import unittest +from solutions.basic_calculator.main import calculate + class TestCalculate(unittest.TestCase): """Test suite for the calculate function.""" def test_addition(self): """It should correctly add two numbers.""" - self.assertEqual(calculate('add', 5, 3), 8.0) - self.assertEqual(calculate('add', -5, -3), -8.0) - self.assertEqual(calculate('add', 0, 5), 5.0) + self.assertEqual(calculate("add", 5, 3), 8.0) + self.assertEqual(calculate("add", -5, -3), -8.0) + self.assertEqual(calculate("add", 0, 5), 5.0) def test_subtraction(self): """It should correctly subtract the second number from the first.""" - self.assertEqual(calculate('subtract', 10, 4), 6.0) - self.assertEqual(calculate('subtract', -5, -5), 0.0) - self.assertEqual(calculate('subtract', 0, 5), -5.0) + self.assertEqual(calculate("subtract", 10, 4), 6.0) + self.assertEqual(calculate("subtract", -5, -5), 0.0) + self.assertEqual(calculate("subtract", 0, 5), -5.0) def test_multiplication(self): """It should correctly multiply two numbers.""" - self.assertEqual(calculate('multiply', 7, 3), 21.0) - self.assertEqual(calculate('multiply', -2, 3), -6.0) - self.assertEqual(calculate('multiply', 0, 10), 0.0) + self.assertEqual(calculate("multiply", 7, 3), 21.0) + self.assertEqual(calculate("multiply", -2, 3), -6.0) + self.assertEqual(calculate("multiply", 0, 10), 0.0) def test_division(self): """It should correctly divide the first number by the second.""" - self.assertEqual(calculate('divide', 9, 3), 3.0) - self.assertEqual(calculate('divide', -6, 3), -2.0) - self.assertAlmostEqual(calculate('divide', 7, 2), 3.5) + self.assertEqual(calculate("divide", 9, 3), 3.0) + self.assertEqual(calculate("divide", -6, 3), -2.0) + self.assertAlmostEqual(calculate("divide", 7, 2), 3.5) def test_division_by_zero(self): """It should raise a ValueError when attempting to divide by zero.""" with self.assertRaises(ValueError) as context: - calculate('divide', 5, 0) + calculate("divide", 5, 0) self.assertEqual(str(context.exception), "Cannot divide by zero.") def test_invalid_operation(self): """It should raise a ValueError for an invalid operation.""" with self.assertRaises(ValueError) as context: - calculate('invalid_op', 5, 3) + calculate("invalid_op", 5, 3) self.assertIn("Invalid operation", str(context.exception)) From 2d9e3f96c94ffcf1a51646bbb5864fe3b05f62c5 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Thu, 2 Jan 2025 17:25:19 -0500 Subject: [PATCH 06/17] create the folders and the init files --- solutions/challenge_9/__init__.py | 0 solutions/tests/challenge_9/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 solutions/challenge_9/__init__.py create mode 100644 solutions/tests/challenge_9/__init__.py diff --git a/solutions/challenge_9/__init__.py b/solutions/challenge_9/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/challenge_9/__init__.py b/solutions/tests/challenge_9/__init__.py new file mode 100644 index 000000000..e69de29bb From a14db69279e2a5c62cc72206dcc9432d9d372829 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Sat, 4 Jan 2025 21:14:23 -0500 Subject: [PATCH 07/17] add the solution to the sum_of_digits_challenge and the test --- solutions/challenge_9/sum_of_digits.py | 27 +++++++++++++++++++ .../tests/challenge_9/test_sum_of_digits.py | 19 +++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 solutions/challenge_9/sum_of_digits.py create mode 100644 solutions/tests/challenge_9/test_sum_of_digits.py diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py new file mode 100644 index 000000000..9fd89574c --- /dev/null +++ b/solutions/challenge_9/sum_of_digits.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for calculating the sum of the digits of a positive integer. +Module contents: +sum_of_digits: calculates the sum of the digits of a positive integer. +Created on January 3rd, 2025 +@author: Semira Tesfai +""" + + +def sum_of_digits(n: int) -> int: + """Calculate the sum of the digits of a positive integer. + Parameters: + n: int, the positive integer whose digits will be summed + Returns -> int: the sum of the digits of the given integer + Raises: + AssertionError: if the argument is not a positive integer + >>> sum_of_digits(123) + 6 + >>> sum_of_digits(4567) + 22 + >>> sum_of_digits(0) + 0 + """ + assert isinstance(n, int) and n >= 0, "input must be a positive integer" + return sum(int(digit) for digit in str(n)) diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py new file mode 100644 index 000000000..1594e3353 --- /dev/null +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -0,0 +1,19 @@ +import unittest + +from solutions.challenge_9.sum_of_digits import sum_of_digits + + +class TestSumOfDigits(unittest.TestCase): + def test_single_digit(self): + self.assertEqual(sum_of_digits(5), 5) + + def test_multi_digit(self): + self.assertEqual(sum_of_digits(123), 6) + self.assertEqual(sum_of_digits(4567), 22) + + def test_edge_case(self): + self.assertEqual(sum_of_digits(0), 0) + + +if __name__ == "__main__": + unittest.main() From 9b9018bbca2f2e0296aa4ee23795673455b72be7 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Fri, 10 Jan 2025 01:13:42 -0500 Subject: [PATCH 08/17] Update sum_of_digits.py Refined sum_of_digits code and tests: added docstrings, improved test cases, and ensured all linting checks passed. --- solutions/challenge_9/sum_of_digits.py | 28 +++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py index 9fd89574c..b45202a86 100644 --- a/solutions/challenge_9/sum_of_digits.py +++ b/solutions/challenge_9/sum_of_digits.py @@ -2,20 +2,34 @@ # -*- coding: utf-8 -*- """ A module for calculating the sum of the digits of a positive integer. + Module contents: -sum_of_digits: calculates the sum of the digits of a positive integer. + sum_of_digits: calculates the sum of the digits of a positive integer. + Created on January 3rd, 2025 @author: Semira Tesfai """ - def sum_of_digits(n: int) -> int: """Calculate the sum of the digits of a positive integer. - Parameters: - n: int, the positive integer whose digits will be summed - Returns -> int: the sum of the digits of the given integer - Raises: - AssertionError: if the argument is not a positive integer + + Parameters + ---------- + n : int + The positive integer whose digits will be summed. + + Returns + ------- + int + The sum of the digits of the given integer. + + Raises + ------ + AssertionError + If the argument is not a positive integer. + + Examples + -------- >>> sum_of_digits(123) 6 >>> sum_of_digits(4567) From 95d1b7b2ab9d8d4a81feda3e37b6f60180fdd5de Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Fri, 10 Jan 2025 01:15:25 -0500 Subject: [PATCH 09/17] Update test_sum_of_digits.py Refined sum_of_digits code and tests: added docstrings, improved test cases, and ensured all linting checks passed. --- .../tests/challenge_9/test_sum_of_digits.py | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py index 1594e3353..4c10939a3 100644 --- a/solutions/tests/challenge_9/test_sum_of_digits.py +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -1,19 +1,37 @@ -import unittest - -from solutions.challenge_9.sum_of_digits import sum_of_digits +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for the sum_of_digits function. +Created on January 3rd, 2025 +""" +import unittest +from sum_of_digits import sum_of_digits class TestSumOfDigits(unittest.TestCase): - def test_single_digit(self): - self.assertEqual(sum_of_digits(5), 5) + """Tests for the sum_of_digits function.""" - def test_multi_digit(self): + def test_positive_integer(self): + """Ensure that the sum of digits of a positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(123), 6) + + def test_large_integer(self): + """Ensure that the sum of digits of a large positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(4567), 22) - def test_edge_case(self): + def test_zero(self): + """Ensure that the sum of digits of zero is zero.""" self.assertEqual(sum_of_digits(0), 0) + def test_invalid_input(self): + """Ensure that the function raises an assertion error for invalid inputs.""" + with self.assertRaises(AssertionError): + sum_of_digits(-5) + with self.assertRaises(AssertionError): + sum_of_digits(4.5) + with self.assertRaises(AssertionError): + sum_of_digits('123') -if __name__ == "__main__": +if __name__ == '__main__': unittest.main() + From 1455cd130a8982483bf0214bb5d3849dd6a08619 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Fri, 10 Jan 2025 12:02:21 -0500 Subject: [PATCH 10/17] made changes to the code by adding headings to the steps --- solutions/challenge_9/sum_of_digits.py | 1 + solutions/tests/challenge_9/test_sum_of_digits.py | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py index b45202a86..587c53d13 100644 --- a/solutions/challenge_9/sum_of_digits.py +++ b/solutions/challenge_9/sum_of_digits.py @@ -10,6 +10,7 @@ @author: Semira Tesfai """ + def sum_of_digits(n: int) -> int: """Calculate the sum of the digits of a positive integer. diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py index 4c10939a3..1c7cd8393 100644 --- a/solutions/tests/challenge_9/test_sum_of_digits.py +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -6,7 +6,8 @@ """ import unittest -from sum_of_digits import sum_of_digits +from solutions.challenge_9.sum_of_digits import sum_of_digits + class TestSumOfDigits(unittest.TestCase): """Tests for the sum_of_digits function.""" @@ -30,8 +31,8 @@ def test_invalid_input(self): with self.assertRaises(AssertionError): sum_of_digits(4.5) with self.assertRaises(AssertionError): - sum_of_digits('123') + sum_of_digits("123") -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + unittest.main() From 7b91346bb080f931c07d0f17ea05ed80b19e54c6 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Sat, 11 Jan 2025 20:21:45 -0500 Subject: [PATCH 11/17] Change file name --- .../{main.py => basic_calculator.py} | 0 solutions/tests/test_basic_calculator.py | 30 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) rename solutions/basic_calculator/{main.py => basic_calculator.py} (100%) diff --git a/solutions/basic_calculator/main.py b/solutions/basic_calculator/basic_calculator.py similarity index 100% rename from solutions/basic_calculator/main.py rename to solutions/basic_calculator/basic_calculator.py diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py index 58550519b..df5fcb798 100644 --- a/solutions/tests/test_basic_calculator.py +++ b/solutions/tests/test_basic_calculator.py @@ -10,7 +10,7 @@ Author: Ana Isabel Murillo """ -from solutions.basic_calculator.main import calculate +from solutions.basic_calculator.basic_calculator import calculate import unittest @@ -19,36 +19,36 @@ class TestCalculate(unittest.TestCase): def test_addition(self): """It should correctly add two numbers.""" - self.assertEqual(calculate('add', 5, 3), 8.0) - self.assertEqual(calculate('add', -5, -3), -8.0) - self.assertEqual(calculate('add', 0, 5), 5.0) + self.assertEqual(calculate("add", 5, 3), 8.0) + self.assertEqual(calculate("add", -5, -3), -8.0) + self.assertEqual(calculate("add", 0, 5), 5.0) def test_subtraction(self): """It should correctly subtract the second number from the first.""" - self.assertEqual(calculate('subtract', 10, 4), 6.0) - self.assertEqual(calculate('subtract', -5, -5), 0.0) - self.assertEqual(calculate('subtract', 0, 5), -5.0) + self.assertEqual(calculate("subtract", 10, 4), 6.0) + self.assertEqual(calculate("subtract", -5, -5), 0.0) + self.assertEqual(calculate("subtract", 0, 5), -5.0) def test_multiplication(self): """It should correctly multiply two numbers.""" - self.assertEqual(calculate('multiply', 7, 3), 21.0) - self.assertEqual(calculate('multiply', -2, 3), -6.0) - self.assertEqual(calculate('multiply', 0, 10), 0.0) + self.assertEqual(calculate("multiply", 7, 3), 21.0) + self.assertEqual(calculate("multiply", -2, 3), -6.0) + self.assertEqual(calculate("multiply", 0, 10), 0.0) def test_division(self): """It should correctly divide the first number by the second.""" - self.assertEqual(calculate('divide', 9, 3), 3.0) - self.assertEqual(calculate('divide', -6, 3), -2.0) - self.assertAlmostEqual(calculate('divide', 7, 2), 3.5) + self.assertEqual(calculate("divide", 9, 3), 3.0) + self.assertEqual(calculate("divide", -6, 3), -2.0) + self.assertAlmostEqual(calculate("divide", 7, 2), 3.5) def test_division_by_zero(self): """It should raise a ValueError when attempting to divide by zero.""" with self.assertRaises(ValueError) as context: - calculate('divide', 5, 0) + calculate("divide", 5, 0) self.assertEqual(str(context.exception), "Cannot divide by zero.") def test_invalid_operation(self): """It should raise a ValueError for an invalid operation.""" with self.assertRaises(ValueError) as context: - calculate('invalid_op', 5, 3) + calculate("invalid_op", 5, 3) self.assertIn("Invalid operation", str(context.exception)) From 3352cdc417e75761739b73619e96980f5601ff0a Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Sat, 11 Jan 2025 21:34:26 -0500 Subject: [PATCH 12/17] Give each test only one assertion --- solutions/tests/test_basic_calculator.py | 40 +++++++++++++++++++----- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py index 70a8c4195..d9b961c2e 100644 --- a/solutions/tests/test_basic_calculator.py +++ b/solutions/tests/test_basic_calculator.py @@ -18,28 +18,52 @@ class TestCalculate(unittest.TestCase): """Test suite for the calculate function.""" - def test_addition(self): - """It should correctly add two numbers.""" + def test_addition_positive_numbers(self): + """It should correctly add two positive numbers.""" self.assertEqual(calculate("add", 5, 3), 8.0) + + def test_addition_negative_numbers(self): + """It should correctly add two negative numbers.""" self.assertEqual(calculate("add", -5, -3), -8.0) + + def test_addition_zero(self): + """It should correctly add zero to a number.""" self.assertEqual(calculate("add", 0, 5), 5.0) - def test_subtraction(self): - """It should correctly subtract the second number from the first.""" + def test_subtraction_positive_numbers(self): + """It should correctly subtract two positive numbers.""" self.assertEqual(calculate("subtract", 10, 4), 6.0) + + def test_subtraction_negative_numbers(self): + """It should correctly subtract two negative numbers.""" self.assertEqual(calculate("subtract", -5, -5), 0.0) + + def test_subtraction_zero(self): + """It should correctly subtract a number from zero.""" self.assertEqual(calculate("subtract", 0, 5), -5.0) - def test_multiplication(self): - """It should correctly multiply two numbers.""" + def test_multiplication_positive_numbers(self): + """It should correctly multiply two positive numbers.""" self.assertEqual(calculate("multiply", 7, 3), 21.0) + + def test_multiplication_negative_numbers(self): + """It should correctly multiply a negative and a positive number.""" self.assertEqual(calculate("multiply", -2, 3), -6.0) + + def test_multiplication_with_zero(self): + """It should return zero when multiplying by zero.""" self.assertEqual(calculate("multiply", 0, 10), 0.0) - def test_division(self): - """It should correctly divide the first number by the second.""" + def test_division_positive_numbers(self): + """It should correctly divide two positive numbers.""" self.assertEqual(calculate("divide", 9, 3), 3.0) + + def test_division_negative_numbers(self): + """It should correctly divide a negative and a positive number.""" self.assertEqual(calculate("divide", -6, 3), -2.0) + + def test_division_result_with_float(self): + """It should correctly handle division resulting in a float.""" self.assertAlmostEqual(calculate("divide", 7, 2), 3.5) def test_division_by_zero(self): From 76ba3627d8a75b14c18b97b461ac2c9be3181b67 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Sat, 11 Jan 2025 23:04:30 -0500 Subject: [PATCH 13/17] Update test_sum_of_digits.py adding "with" in the test names provides a bit more context about what inputs are being tested against, making the test scenarios clearer and more descriptive for people to read it --- solutions/tests/challenge_9/test_sum_of_digits.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py index 1c7cd8393..6539f4613 100644 --- a/solutions/tests/challenge_9/test_sum_of_digits.py +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -12,15 +12,15 @@ class TestSumOfDigits(unittest.TestCase): """Tests for the sum_of_digits function.""" - def test_positive_integer(self): + def test_with_positive_integer(self): """Ensure that the sum of digits of a positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(123), 6) - def test_large_integer(self): + def test_with_large_integer(self): """Ensure that the sum of digits of a large positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(4567), 22) - def test_zero(self): + def test_with_zero(self): """Ensure that the sum of digits of zero is zero.""" self.assertEqual(sum_of_digits(0), 0) From fa9d0449dd92ca71f314a224b392f8f360892773 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Sat, 11 Jan 2025 23:23:46 -0500 Subject: [PATCH 14/17] Update sum_of_digits.py Replace the assert statement with an explicit if statement and raise a ValueError in the sum_of_digits function --- solutions/challenge_9/sum_of_digits.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py index 587c53d13..97849b4e6 100644 --- a/solutions/challenge_9/sum_of_digits.py +++ b/solutions/challenge_9/sum_of_digits.py @@ -38,5 +38,7 @@ def sum_of_digits(n: int) -> int: >>> sum_of_digits(0) 0 """ - assert isinstance(n, int) and n >= 0, "input must be a positive integer" + if not isinstance(n, int) or n < 0: + raise ValueError("input must be a positive integer") + return sum(int(digit) for digit in str(n)) From 8d5f24c30832993f316f86659ec283557eaebaa8 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Sun, 12 Jan 2025 10:41:46 -0500 Subject: [PATCH 15/17] replaced the assert statement used for input validation by an explicite if statement and a valueError --- solutions/challenge_9/sum_of_digits.py | 6 ++++-- solutions/tests/challenge_9/test_sum_of_digits.py | 15 ++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py index 587c53d13..7bb4d7df2 100644 --- a/solutions/challenge_9/sum_of_digits.py +++ b/solutions/challenge_9/sum_of_digits.py @@ -26,7 +26,7 @@ def sum_of_digits(n: int) -> int: Raises ------ - AssertionError + ValueError If the argument is not a positive integer. Examples @@ -38,5 +38,7 @@ def sum_of_digits(n: int) -> int: >>> sum_of_digits(0) 0 """ - assert isinstance(n, int) and n >= 0, "input must be a positive integer" + if not isinstance(n, int) or n < 0: + raise ValueError("input must be a positive integer") + return sum(int(digit) for digit in str(n)) diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py index 1c7cd8393..a509212cf 100644 --- a/solutions/tests/challenge_9/test_sum_of_digits.py +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -6,31 +6,32 @@ """ import unittest + from solutions.challenge_9.sum_of_digits import sum_of_digits class TestSumOfDigits(unittest.TestCase): """Tests for the sum_of_digits function.""" - def test_positive_integer(self): + def test_with_positive_integer(self): """Ensure that the sum of digits of a positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(123), 6) - def test_large_integer(self): + def test_with_large_integer(self): """Ensure that the sum of digits of a large positive integer is calculated correctly.""" self.assertEqual(sum_of_digits(4567), 22) - def test_zero(self): + def test_with_zero(self): """Ensure that the sum of digits of zero is zero.""" self.assertEqual(sum_of_digits(0), 0) - def test_invalid_input(self): + def test_with_invalid_input(self): """Ensure that the function raises an assertion error for invalid inputs.""" - with self.assertRaises(AssertionError): + with self.assertRaises(ValueError): sum_of_digits(-5) - with self.assertRaises(AssertionError): + with self.assertRaises(ValueError): sum_of_digits(4.5) - with self.assertRaises(AssertionError): + with self.assertRaises(ValueError): sum_of_digits("123") From 30b0ccec21a2be9cc7b2315809884097b4e2ad72 Mon Sep 17 00:00:00 2001 From: SEMIRATESFAI Date: Sun, 12 Jan 2025 13:21:06 -0500 Subject: [PATCH 16/17] updated changes that were recomended by reviewer --- solutions/challenge_9/sum_of_digits.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py index c8b58d1cf..a758eaf37 100644 --- a/solutions/challenge_9/sum_of_digits.py +++ b/solutions/challenge_9/sum_of_digits.py @@ -17,7 +17,7 @@ def sum_of_digits(n: int) -> int: Parameters ---------- n : int - The positive integer whose digits will be summed. + The non-negative integer whose digits will be summed. Returns ------- @@ -27,7 +27,7 @@ def sum_of_digits(n: int) -> int: Raises ------ ValueError - If the argument is not a positive integer. + If the argument is not a non-negative integer. Examples -------- @@ -38,13 +38,8 @@ def sum_of_digits(n: int) -> int: >>> sum_of_digits(0) 0 """ -<<<<<<< HEAD + if not isinstance(n, int) or n < 0: - raise ValueError("input must be a positive integer") + raise ValueError("input must be a non-negative integer.") -======= - if not isinstance(n, int) or n < 0: - raise ValueError("input must be a positive integer") - ->>>>>>> fa9d0449dd92ca71f314a224b392f8f360892773 return sum(int(digit) for digit in str(n)) From 009d8568bfba8a2eb7353b6781ce6f2f95680c39 Mon Sep 17 00:00:00 2001 From: AnaiMurillo Date: Sun, 12 Jan 2025 14:28:01 -0500 Subject: [PATCH 17/17] change file name --- .../basic_calculator/{basic_calculator.py => calculate.py} | 0 solutions/tests/test_basic_calculator.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename solutions/basic_calculator/{basic_calculator.py => calculate.py} (100%) diff --git a/solutions/basic_calculator/basic_calculator.py b/solutions/basic_calculator/calculate.py similarity index 100% rename from solutions/basic_calculator/basic_calculator.py rename to solutions/basic_calculator/calculate.py diff --git a/solutions/tests/test_basic_calculator.py b/solutions/tests/test_basic_calculator.py index d9b961c2e..752f63ce1 100644 --- a/solutions/tests/test_basic_calculator.py +++ b/solutions/tests/test_basic_calculator.py @@ -10,7 +10,7 @@ Author: Ana Isabel Murillo """ -from solutions.basic_calculator.basic_calculator import calculate +from solutions.basic_calculator.calculate import calculate import unittest