From 694a7c1e3e97bafc91ca5ee9d853a34d3396c948 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 7 Jan 2025 17:05:56 -0500 Subject: [PATCH 1/3] challenge_23 : about temperature_conversion --- .vscode/launch.json | 7 +++ solutions/challenge_23/__init__.py | 0 .../challenge_23/celsius_to_fahrenheit.py | 51 +++++++++++++++++++ solutions/challenge_36/factorial.py | 23 +++++++++ solutions/tests/test_celsius_to_fahrenheit.py | 36 +++++++++++++ solutions/tests/test_factorial.py | 24 +++++++++ 6 files changed, 141 insertions(+) create mode 100644 solutions/challenge_23/__init__.py create mode 100644 solutions/challenge_23/celsius_to_fahrenheit.py create mode 100644 solutions/challenge_36/factorial.py create mode 100644 solutions/tests/test_celsius_to_fahrenheit.py create mode 100644 solutions/tests/test_factorial.py diff --git a/.vscode/launch.json b/.vscode/launch.json index 4342f4c90..3241336a7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Attach to Chrome", + "port": 9222, + "request": "attach", + "type": "chrome", + "webRoot": "${workspaceFolder}" + }, { "name": "ET: Debug Python (unittest)", "type": "debugpy", diff --git a/solutions/challenge_23/__init__.py b/solutions/challenge_23/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/challenge_23/celsius_to_fahrenheit.py b/solutions/challenge_23/celsius_to_fahrenheit.py new file mode 100644 index 000000000..4a7345466 --- /dev/null +++ b/solutions/challenge_23/celsius_to_fahrenheit.py @@ -0,0 +1,51 @@ +"""conversion of temperature from celsius to fahrenheit.""" + + +def celsius_to_fahrenheit(celsius_list): + """ + Convert a list of temperatures from Celsius to Fahrenheit. + + Args: + celsius_list (list): List of temperatures in Celsius. + + Returns: + list: List of temperatures converted to Fahrenheit. + + Raises: + ValueError: If input is not a list or contains invalid elements. + """ + if not isinstance(celsius_list, list): + raise ValueError("Input must be a list.") + + fahrenheit_list = [] + + for temp in celsius_list: + if not isinstance(temp, (int, float)): + raise ValueError("List elements must be integers or floats.") + + fahrenheit = (9 / 5) * temp + 32 + fahrenheit_list.append(round(fahrenheit, 2)) + + return fahrenheit_list + + +if __name__ == "__main__": + user_input = input("Enter the temperature in celsius: ") + + input_celsius_list = [] + if not user_input.strip(): + print("Error: No input provided.") + else: + try: + input_celsius_list = [float(temp.strip()) for temp in user_input.split(",")] + except ValueError: + print( + "Error: Invalid input. Please enter a list of numbers separated by commas." + ) + input_celsius_list = [] + + try: + result = celsius_to_fahrenheit(input_celsius_list) + print("The list of temperature in fahrenheit is:", result) + except ValueError as e: + print("Error:", e) diff --git a/solutions/challenge_36/factorial.py b/solutions/challenge_36/factorial.py new file mode 100644 index 000000000..1469d15e5 --- /dev/null +++ b/solutions/challenge_36/factorial.py @@ -0,0 +1,23 @@ +def factorial(n): + """ + Calculate the factorial of a non-negative integer n using recursion. + + Args: + n (int): Non-negative integer whose factorial is to be calculated. + + Returns: + int: Factorial of the input number n. + """ + if n < 0: + raise ValueError("Factorial is not defined for negative numbers.") + if n == 0: + return 1 + return n * factorial(n - 1) + + +if __name__ == "__main__": + try: + num = int(input("Enter a non-negative integer: ")) + print(f"Factorial of {num} is {factorial(num)}") + except ValueError as e: + print(f"Error: {e}") diff --git a/solutions/tests/test_celsius_to_fahrenheit.py b/solutions/tests/test_celsius_to_fahrenheit.py new file mode 100644 index 000000000..0c2dd3ff9 --- /dev/null +++ b/solutions/tests/test_celsius_to_fahrenheit.py @@ -0,0 +1,36 @@ +"""Test the celsius_to_fahrenheit function from the challenge_23 module.""" + +import unittest +from solutions.challenge_23.celsius_to_fahrenheit import celsius_to_fahrenheit + + +class TestCelsiusToFahrenheit(unittest.TestCase): + """Unit tests for the celsius_to_fahrenheit function.""" + + def test_positive_temperatures(self): + """Test the program with positive temperature list.""" + self.assertEqual(celsius_to_fahrenheit([0, 100]), [32.0, 212.0]) + + def test_negative_temperatures(self): + """Test the program with negative temperatures list.""" + self.assertEqual(celsius_to_fahrenheit([-40, -10]), [-40.0, 14.0]) + + def test_mixed_temperatures(self): + """Test the program with mixed temperatures list""" + self.assertEqual(celsius_to_fahrenheit([-10, 0, 25]), [14.0, 32.0, 77.0]) + + def test_float_temperatures(self): + """Test the program with float temperatures.""" + result = celsius_to_fahrenheit([0.5, -0.5]) + self.assertAlmostEqual(result[0], 32.9, places=1) + self.assertAlmostEqual(result[1], 31.1, places=1) + + def test_invalid_input_non_list(self): + """Test invalid input that is not a list.""" + with self.assertRaises(ValueError): + celsius_to_fahrenheit("not a list") + + def test_invalid_input_non_numeric_elements(self): + """Test invalid input that contains non-numeric elements.""" + with self.assertRaises(ValueError): + celsius_to_fahrenheit([30, "a string", 45]) diff --git a/solutions/tests/test_factorial.py b/solutions/tests/test_factorial.py new file mode 100644 index 000000000..510e05133 --- /dev/null +++ b/solutions/tests/test_factorial.py @@ -0,0 +1,24 @@ +"""test the program factorial""" + +import unittest +from solutions.challenge_36.factorial import factorial + + +class TestFactorial(unittest.TestCase): + """test the program for positive numbers, negative numbers and zero""" + + def test_factorial_positive(self): + """test the program for positive numbers""" + self.assertEqual(factorial(5), 120) + self.assertEqual(factorial(4), 24) + self.assertEqual(factorial(3), 6) + self.assertEqual(factorial(1), 1) + + def test_factorial_zero(self): + """test the program for zero""" + self.assertEqual(factorial(0), 1) + + def test_factorial_negative(self): + """test the program for negative numbers""" + with self.assertRaises(ValueError): + factorial(-1) From 4f2eb905433fb777daf9b09bdc513d93546a6bff Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 9 Jan 2025 18:15:18 -0500 Subject: [PATCH 2/3] correction --- solutions/challenge_36/factorial.py | 23 ------------------ .../test_celsius_to_fahrenheit.py | 0 solutions/tests/test_factorial.py | 24 ------------------- 3 files changed, 47 deletions(-) delete mode 100644 solutions/challenge_36/factorial.py rename solutions/tests/{ => challenge_23}/test_celsius_to_fahrenheit.py (100%) delete mode 100644 solutions/tests/test_factorial.py diff --git a/solutions/challenge_36/factorial.py b/solutions/challenge_36/factorial.py deleted file mode 100644 index 1469d15e5..000000000 --- a/solutions/challenge_36/factorial.py +++ /dev/null @@ -1,23 +0,0 @@ -def factorial(n): - """ - Calculate the factorial of a non-negative integer n using recursion. - - Args: - n (int): Non-negative integer whose factorial is to be calculated. - - Returns: - int: Factorial of the input number n. - """ - if n < 0: - raise ValueError("Factorial is not defined for negative numbers.") - if n == 0: - return 1 - return n * factorial(n - 1) - - -if __name__ == "__main__": - try: - num = int(input("Enter a non-negative integer: ")) - print(f"Factorial of {num} is {factorial(num)}") - except ValueError as e: - print(f"Error: {e}") diff --git a/solutions/tests/test_celsius_to_fahrenheit.py b/solutions/tests/challenge_23/test_celsius_to_fahrenheit.py similarity index 100% rename from solutions/tests/test_celsius_to_fahrenheit.py rename to solutions/tests/challenge_23/test_celsius_to_fahrenheit.py diff --git a/solutions/tests/test_factorial.py b/solutions/tests/test_factorial.py deleted file mode 100644 index 510e05133..000000000 --- a/solutions/tests/test_factorial.py +++ /dev/null @@ -1,24 +0,0 @@ -"""test the program factorial""" - -import unittest -from solutions.challenge_36.factorial import factorial - - -class TestFactorial(unittest.TestCase): - """test the program for positive numbers, negative numbers and zero""" - - def test_factorial_positive(self): - """test the program for positive numbers""" - self.assertEqual(factorial(5), 120) - self.assertEqual(factorial(4), 24) - self.assertEqual(factorial(3), 6) - self.assertEqual(factorial(1), 1) - - def test_factorial_zero(self): - """test the program for zero""" - self.assertEqual(factorial(0), 1) - - def test_factorial_negative(self): - """test the program for negative numbers""" - with self.assertRaises(ValueError): - factorial(-1) From 6306d3612eab7953608b1bd5e5a8a3406543f8e4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 12 Jan 2025 09:06:01 -0500 Subject: [PATCH 3/3] Requested changes made --- solutions/challenge_23/celsius_to_fahrenheit.py | 10 ++++++++++ .../tests/challenge_23/test_celsius_to_fahrenheit.py | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/solutions/challenge_23/celsius_to_fahrenheit.py b/solutions/challenge_23/celsius_to_fahrenheit.py index 4a7345466..2a674afc0 100644 --- a/solutions/challenge_23/celsius_to_fahrenheit.py +++ b/solutions/challenge_23/celsius_to_fahrenheit.py @@ -13,7 +13,17 @@ def celsius_to_fahrenheit(celsius_list): Raises: ValueError: If input is not a list or contains invalid elements. + + >>> celsius_to_fahrenheit([0, 100]) + [32.0, 212.0] + + >>> celsius_to_fahrenheit([-40, -10]) + [-40.0, 14.0] + + >>> celsius_to_fahrenheit([25.5, 0.5]) + [77.9, 32.9] """ + if not isinstance(celsius_list, list): raise ValueError("Input must be a list.") diff --git a/solutions/tests/challenge_23/test_celsius_to_fahrenheit.py b/solutions/tests/challenge_23/test_celsius_to_fahrenheit.py index 0c2dd3ff9..8097eb518 100644 --- a/solutions/tests/challenge_23/test_celsius_to_fahrenheit.py +++ b/solutions/tests/challenge_23/test_celsius_to_fahrenheit.py @@ -5,17 +5,18 @@ class TestCelsiusToFahrenheit(unittest.TestCase): - """Unit tests for the celsius_to_fahrenheit function.""" + """This tests module provide test for the celsius_to_fahrenheit function. + It tests the function with different inputs and checks if the output is as expected.""" - def test_positive_temperatures(self): + def test__with_positive_temperatures(self): """Test the program with positive temperature list.""" self.assertEqual(celsius_to_fahrenheit([0, 100]), [32.0, 212.0]) - def test_negative_temperatures(self): + def test_with_negative_temperatures(self): """Test the program with negative temperatures list.""" self.assertEqual(celsius_to_fahrenheit([-40, -10]), [-40.0, 14.0]) - def test_mixed_temperatures(self): + def test_with_mixed_temperatures(self): """Test the program with mixed temperatures list""" self.assertEqual(celsius_to_fahrenheit([-10, 0, 25]), [14.0, 32.0, 77.0]) @@ -23,7 +24,6 @@ def test_float_temperatures(self): """Test the program with float temperatures.""" result = celsius_to_fahrenheit([0.5, -0.5]) self.assertAlmostEqual(result[0], 32.9, places=1) - self.assertAlmostEqual(result[1], 31.1, places=1) def test_invalid_input_non_list(self): """Test invalid input that is not a list.""" @@ -31,6 +31,6 @@ def test_invalid_input_non_list(self): celsius_to_fahrenheit("not a list") def test_invalid_input_non_numeric_elements(self): - """Test invalid input that contains non-numeric elements.""" + """Test the program with invalid input that contains non-numeric elements.""" with self.assertRaises(ValueError): celsius_to_fahrenheit([30, "a string", 45])