From a6cab574ec1da5ca0829a5d0f593336253d8c779 Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Sun, 29 Dec 2024 10:27:07 +0200 Subject: [PATCH 01/23] mean function --- solutions/mean.py | 29 +++++++++++++++++++++ solutions/tests/test_mean.py | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 solutions/mean.py create mode 100644 solutions/tests/test_mean.py diff --git a/solutions/mean.py b/solutions/mean.py new file mode 100644 index 000000000..bb5168f3d --- /dev/null +++ b/solutions/mean.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for finding the mean of a list of numbers. + +Module contents: + - mean: finds the mean of a list of numbers. + +Created on XX XX XX +@author: Mohammed Elfadil +""" + + +def mean(a: list) -> float: + """Finds the mean of a list of numbers. + Parameter: + a: list + Return -> float: the mean of the list + Raises: + AssertionError: if the argument is not a list + >>> mean([1, 2, 3, 4, 5]) + 3.0 + >>> mean([10, 20, 30, 40, 50]) + 30.0 + """ + if not isinstance(a, list): + return "invalid input" + + return sum(a) / len(a) diff --git a/solutions/tests/test_mean.py b/solutions/tests/test_mean.py new file mode 100644 index 000000000..90b160c64 --- /dev/null +++ b/solutions/tests/test_mean.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import unittest +from solutions.mean import mean + + +class TestMean(unittest.TestCase): + """test the mean function""" + + def test_1(self): + """It should evaluate 3.0""" + actual = mean([1, 2, 3, 4, 5]) + expected = 3.0 + self.assertEqual(actual, expected) + + def test_2(self): + """It should evaluate 30.0""" + actual = mean([10, 20, 30, 40, 50]) + expected = 30.0 + self.assertEqual(actual, expected) + + def test_3(self): + """It should evaluate 0.0""" + actual = mean([0, 0, 0, 0, 0]) + expected = 0.0 + self.assertEqual(actual, expected) + + def test_4(self): + """It should evaluate 1.0""" + actual = mean([1]) + expected = 1.0 + self.assertEqual(actual, expected) + + def test_integer(self): + """It should evaluate invalid input""" + actual = mean(1) + expected = "invalid input" + self.assertEqual(actual, expected) + + def test_negatives(self): + """It should evaluate -4.0""" + actual = mean([-2, -4, -6]) + expected = -4.0 + self.assertEqual(actual, expected) + + def test_floats(self): + """It should evaluate 2.5""" + actual = mean([1.5, 2.5, 3.5]) + expected = 2.5 + self.assertEqual(actual, expected) From cec61c8be188206752287d9e4d93778fcda044fa Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 17:27:37 +0300 Subject: [PATCH 02/23] created cumulative sum in a list --- solutions/cumulative_sum.py | 58 ++++++++++++++++ solutions/tests/__init__.py | 1 - solutions/tests/test_cumulative_sum.py | 96 ++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 solutions/cumulative_sum.py delete mode 100644 solutions/tests/__init__.py create mode 100644 solutions/tests/test_cumulative_sum.py diff --git a/solutions/cumulative_sum.py b/solutions/cumulative_sum.py new file mode 100644 index 000000000..a1cb44c61 --- /dev/null +++ b/solutions/cumulative_sum.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Module: cumulative_sum + +Description: + This module provides a function to calculate the cumulative sum of + a list of numbers. It is useful for applications requiring progressive + accumulation of values, such as financial calculations, data analysis, + or custom mathematical operations. + +Module Contents: + - cumulative_sum(numbers: list) -> list: + Computes and returns a list of cumulative sums from the input list. + +Author: Falaq Youniss +Date: 29/12/2024 +""" + + +def cumulative_sum(numbers: list) -> list: + """ + Computes the cumulative sum of a list of numbers. + + Args: + numbers (list): A list of numeric values (integers or floats). + + Returns: + list: A list where each element is the cumulative sum up to that index. + + Raises: + AssertionError: + - If the input is not a list. + - If the list contains non-numeric values. + - If the input is `None`. + + >>> cumulative_sum([1, 2, 3, 4]) + [1, 3, 6, 10] + >>> cumulative_sum([-1, -2, -3, -4]) + [-1, -3, -6, -10] + >>> cumulative_sum([1.0, 2.0, 3.0, 4.0]) + [1.0, 3.0, 6.0, 10.0] + """ + # Validate input + assert numbers is not None, "Input cannot be None." + assert isinstance(numbers, list), "Input must be a list of numeric values." + assert all( + isinstance(num, (int, float)) for num in numbers + ), "All elements in the list must be numeric." + # Compute cumulative sums + cumulative_list = [] + current_sum = 0 + for num in numbers: + current_sum += num + cumulative_list.append(current_sum) + + return cumulative_list diff --git a/solutions/tests/__init__.py b/solutions/tests/__init__.py deleted file mode 100644 index 8b1378917..000000000 --- a/solutions/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/solutions/tests/test_cumulative_sum.py b/solutions/tests/test_cumulative_sum.py new file mode 100644 index 000000000..128907503 --- /dev/null +++ b/solutions/tests/test_cumulative_sum.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +""" +Module: test_cumulative_sum + +Description: + This module contains test cases for the `cumulative_sum` function defined + in the `cumulative_sum.py` module. It tests the function's behavior with + various input scenarios, ensuring that it handles different edge cases and + returns the expected cumulative sums. + +Test Categories: + - Standard cases: List of positive integers, negative integers, and floating-point numbers. + - Edge cases: Empty list, single-element list, list with zeros, large number list. + - Defensive tests: None input, Non-list inputs, list with non-numeric elements. + +Author: Falaq Youniss +Date: 29/12/2024 +""" + +import unittest +from ..cumulative_sum import cumulative_sum + + +class TestCumulativeSum(unittest.TestCase): + """Test for cumulative_sum function that handles different cases""" + + # Standard test cases + def test_positive_int(self): + """It should return a cumulative list of positive integers.""" + self.assertEqual(cumulative_sum([1, 2, 3, 4]), [1, 3, 6, 10]) + + def test_negative_int(self): + """It should return a cumulative list of negative integers.""" + self.assertEqual(cumulative_sum([-1, -2, -3, -4]), [-1, -3, -6, -10]) + + def test_positive_float(self): + """It should return a cumulative list of positive floats.""" + self.assertEqual(cumulative_sum([1.0, 2.0, 3.0, 4.0]), [1.0, 3.0, 6.0, 10.0]) + + def test_negative_float(self): + """It should return a cumulative list of negative floats.""" + self.assertEqual( + cumulative_sum([-1.0, -2.0, -3.0, -4.0]), [-1.0, -3.0, -6.0, -10.0] + ) + + def test_positive_negative(self): + """It should return a cumulative list of negative and positive integers.""" + self.assertEqual(cumulative_sum([-1, 2, -3, 4]), [-1, 1, -2, 2]) + + def test_integer_float(self): + """It should return a cumulative list of integers and floats.""" + self.assertEqual(cumulative_sum([1.0, 2, 3.0, 4]), [1, 3, 6.0, 10.0]) + + def test_combination(self): + """It should return a cumulative list of mixed positive and negative integers/floats.""" + self.assertEqual(cumulative_sum([1.0, -2, -3.0, 4]), [1, -1, -4, 0]) + + def test_same(self): + """It should return a cumulative list of same positive integers.""" + self.assertEqual(cumulative_sum([3, 3, 3]), [3, 6, 9]) + + # Edge cases + def test_zero(self): + """It should return a list of zeros.""" + self.assertEqual(cumulative_sum([0, 0, 0, 0]), [0, 0, 0, 0]) + + def test_empty(self): + """It should return an empty list.""" + self.assertEqual(cumulative_sum([]), []) + + def test_one(self): + """It should return the same single-item list.""" + self.assertEqual(cumulative_sum([1]), [1]) + + def test_large_numbers(self): + """It should correctly handle large numbers.""" + self.assertEqual(cumulative_sum([1e6, 2e6, 3e6]), [1e6, 3e6, 6e6]) + + # Defensive tests + def test_none(self): + """It should raise AssertionError for None input.""" + with self.assertRaises(AssertionError): + cumulative_sum(None) + + def test_not_list(self): + """It should raise AssertionError for non-list input.""" + with self.assertRaises(AssertionError): + cumulative_sum("hello") + + def test_not_num(self): + """It should raise AssertionError for non-numeric list elements.""" + with self.assertRaises(AssertionError): + cumulative_sum([1, "cat", 3]) From 6afd85613a024aef57f32c9dbd495e7c62f53285 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 17:58:13 +0300 Subject: [PATCH 03/23] adding the deleted file --- solutions/tests/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 solutions/tests/__init__.py diff --git a/solutions/tests/__init__.py b/solutions/tests/__init__.py new file mode 100644 index 000000000..e69de29bb From 628fd50cc1aceb3dfb0f505ac6eab523c6472776 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 18:41:10 +0300 Subject: [PATCH 04/23] fixed formatting --- .vscode/settings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..252022b48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -119,8 +119,8 @@ "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.ruff": true, - "source.organizeImports.ruff": true + "source.fixAll.ruff": "explicit", + "source.organizeImports.ruff": "explicit" } } } From 032eaf89cd195929b3f5bc584ebfea73f41265bf Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 19:11:32 +0300 Subject: [PATCH 05/23] updates in formatt --- .vscode/settings.json | 6 +++++- solutions/cumulative_sum.py | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 252022b48..d3ba05557 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -122,5 +122,9 @@ "source.fixAll.ruff": "explicit", "source.organizeImports.ruff": "explicit" } - } + }, + "cSpell.words": [ + "Falaq", + "Youniss" + ] } diff --git a/solutions/cumulative_sum.py b/solutions/cumulative_sum.py index a1cb44c61..4de900349 100644 --- a/solutions/cumulative_sum.py +++ b/solutions/cumulative_sum.py @@ -4,14 +4,14 @@ """ Module: cumulative_sum -Description: - This module provides a function to calculate the cumulative sum of - a list of numbers. It is useful for applications requiring progressive - accumulation of values, such as financial calculations, data analysis, +Description: + This module provides a function to calculate the cumulative sum of + a list of numbers. It is useful for applications requiring progressive + accumulation of values, such as financial calculations, data analysis, or custom mathematical operations. Module Contents: - - cumulative_sum(numbers: list) -> list: + - cumulative_sum(numbers: list) -> list: Computes and returns a list of cumulative sums from the input list. Author: Falaq Youniss From 639e7c5f07f3d1337c50d7f695826fb7dd474e86 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 19:22:26 +0300 Subject: [PATCH 06/23] sum formatting in function --- solutions/cumulative_sum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/cumulative_sum.py b/solutions/cumulative_sum.py index 4de900349..8f5cbb435 100644 --- a/solutions/cumulative_sum.py +++ b/solutions/cumulative_sum.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- - """ Module: cumulative_sum From 7451b3160587ca0b8096aec1e7802c7737787f42 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 19:25:14 +0300 Subject: [PATCH 07/23] some formatt changes in test file --- solutions/tests/test_cumulative_sum.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/solutions/tests/test_cumulative_sum.py b/solutions/tests/test_cumulative_sum.py index 128907503..48d61c6b3 100644 --- a/solutions/tests/test_cumulative_sum.py +++ b/solutions/tests/test_cumulative_sum.py @@ -1,20 +1,18 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- - - """ Module: test_cumulative_sum -Description: +Description: This module contains test cases for the `cumulative_sum` function defined - in the `cumulative_sum.py` module. It tests the function's behavior with - various input scenarios, ensuring that it handles different edge cases and + in the `cumulative_sum.py` module. It tests the function's behavior with + various input scenarios, ensuring that it handles different edge cases and returns the expected cumulative sums. Test Categories: - Standard cases: List of positive integers, negative integers, and floating-point numbers. - Edge cases: Empty list, single-element list, list with zeros, large number list. - - Defensive tests: None input, Non-list inputs, list with non-numeric elements. + - Defensive tests: None input, Non-list inputs, list with non-numeric elements. Author: Falaq Youniss Date: 29/12/2024 From 68a94df99e6aec5c5c2bb9285ed68c2d958232b1 Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Sun, 29 Dec 2024 18:40:02 +0200 Subject: [PATCH 08/23] setting changes --- .vscode/settings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..252022b48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -119,8 +119,8 @@ "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.ruff": true, - "source.organizeImports.ruff": true + "source.fixAll.ruff": "explicit", + "source.organizeImports.ruff": "explicit" } } } From 99d4787bb30f626eb0a9581002c5722e6ae328d1 Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Sun, 29 Dec 2024 18:46:10 +0200 Subject: [PATCH 09/23] CI checks --- solutions/IsPrime.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/IsPrime.py b/solutions/IsPrime.py index a943faff5..3e00390d8 100644 --- a/solutions/IsPrime.py +++ b/solutions/IsPrime.py @@ -30,6 +30,8 @@ def IsPrime(a: int) -> str: prime >>> IsPrime(2.5) invalid input + >>> IsPrime(-1) + not prime """ if not isinstance(a, int): return "invalid input" From bc9aeb5954b9aea3f70446a0838d96005f81a3a5 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 20:02:22 +0300 Subject: [PATCH 10/23] modified docstring --- solutions/cumulative_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/cumulative_sum.py b/solutions/cumulative_sum.py index 8f5cbb435..2e77fb9d0 100644 --- a/solutions/cumulative_sum.py +++ b/solutions/cumulative_sum.py @@ -5,7 +5,7 @@ Description: This module provides a function to calculate the cumulative sum of - a list of numbers. It is useful for applications requiring progressive + a list of numbers(integers\float). It is useful for applications requiring progressive accumulation of values, such as financial calculations, data analysis, or custom mathematical operations. From 37c36be47680adc2535814707846963212f70910 Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Sun, 29 Dec 2024 19:40:36 +0200 Subject: [PATCH 11/23] settings --- .vscode/settings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 252022b48..bbda5188d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -119,8 +119,8 @@ "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.ruff": "explicit", - "source.organizeImports.ruff": "explicit" + "source.fixAll.ruff": true, + "source.organizeImports.ruff": true } } } From cf1c9bcb05180bf2c1cf8607562fc728744d8b38 Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Sun, 29 Dec 2024 19:44:41 +0200 Subject: [PATCH 12/23] solving conflicts --- .vscode/settings.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..058679944 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -114,13 +114,7 @@ // Enable/disable update table of contents on save "markdown.extension.toc.updateOnSave": false, - "[python]": { - "editor.defaultFormatter": "charliermarsh.ruff", - "editor.formatOnSave": true, - "editor.codeActionsOnSave": { - "source.fixAll.ruff": true, - "source.organizeImports.ruff": true - } + "editor.defaultFormatter": "ms-python.black-formatter" } } From 44fcb9021cc92b5565d0e529c6c297011723f11b Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 11:58:24 +0300 Subject: [PATCH 13/23] modified test_same string --- solutions/tests/test_cumulative_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_cumulative_sum.py b/solutions/tests/test_cumulative_sum.py index 48d61c6b3..a1c29af72 100644 --- a/solutions/tests/test_cumulative_sum.py +++ b/solutions/tests/test_cumulative_sum.py @@ -57,7 +57,7 @@ def test_combination(self): self.assertEqual(cumulative_sum([1.0, -2, -3.0, 4]), [1, -1, -4, 0]) def test_same(self): - """It should return a cumulative list of same positive integers.""" + """It should return a cumulative list of positive integers.""" self.assertEqual(cumulative_sum([3, 3, 3]), [3, 6, 9]) # Edge cases From 9e8e89b3e104d9e50ef0e76c926cd890d36923b5 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 16:32:36 +0300 Subject: [PATCH 14/23] created my list to string challenge --- .vscode/settings.json | 9 ++- solutions/list_to_string.py | 61 +++++++++++++++++++ solutions/tests/test_list_to_string.py | 84 ++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 solutions/list_to_string.py create mode 100644 solutions/tests/test_list_to_string.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 058679944..c650fa46d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -116,5 +116,12 @@ "markdown.extension.toc.updateOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" - } + }, + "cSpell.words": [ + "catdog", + "cathatbat", + "Falaq", + "Truehello", + "Youniss" + ] } diff --git a/solutions/list_to_string.py b/solutions/list_to_string.py new file mode 100644 index 000000000..3435d0bb1 --- /dev/null +++ b/solutions/list_to_string.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Module: List to String Converter + +Description: + This module provides a function to convert a list of elements (integers, floats, + strings, booleans) into a single concatenated string. It handles various data + types and ensures that all elements are appropriately converted to strings and joined together. + +Module Contents: + - list_to_string(lst: list) -> str: + Converts a list of elements into a single concatenated string. + Ensures proper handling of different data types and raises appropriate errors + for invalid inputs. + +Author: Falaq Youniss +Date: 30/12/2024 +""" + + +def list_to_string(lst: list) -> str: + """ + Converts a list of elements into a single concatenated string. + + Args: + lst (list): A list of items which can include integers, floats, strings, + or booleans. + + Returns: + str: A string that combines all elements of the list. + + Raises: + AssertionError: + - If the input is not a list. + - If the input is `None`. + - If the list is empty. + + >>> list_to_string([1, -4, 3.14]) + '1-43.14' + + >>> list_to_string([True, 'hello']) + 'Truehello' + + >>> list_to_string(['cat', 'hat', 'bat']) + 'cathatbat' + """ + # Validate Input + assert isinstance(lst, list), "Input must be a list." + assert lst is not None, "The input cannot be None." + assert len(lst) > 0, "The list cannot be empty." + + # Initialize an empty result string + result = "" + + # Iterate through the list and convert each element to a string + for element in lst: + + result += str(element) # Convert each element to string and concatenate + + return result diff --git a/solutions/tests/test_list_to_string.py b/solutions/tests/test_list_to_string.py new file mode 100644 index 000000000..db3769c62 --- /dev/null +++ b/solutions/tests/test_list_to_string.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Module: test_list_to_string + +Description: + This module contains test cases for the `list_to_string` function defined + in the `list_to_string_converter.py` module. It tests the function's behavior + with various input scenarios, ensuring that it handles different edge cases and + returns the expected concatenated string. + +Test Categories: + - Standard cases: List of integers, floats, strings, and booleans. + - Edge cases: single-element list, list with mixed data types. + - Defensive tests: None input, Non-list inputs, Empty list. + +Author: Falaq Youniss +Date: 30/12/2024 +""" + +import unittest +from ..list_to_string import list_to_string + + +class TestListToString(unittest.TestCase): + """Test cases for the `list_to_string` function.""" + + # Standard test cases + def test_integers(self): + """It should return a concatenated string of integers.""" + self.assertEqual(list_to_string([1, 2, 3]), "123") + + def test_floats(self): + """It should return a concatenated string of floats.""" + self.assertEqual(list_to_string([1.1, 2.2, 3.3]), "1.12.23.3") + + def test_strings(self): + """It should return a concatenated string of strings.""" + self.assertEqual(list_to_string(["cat", "hat", "bat"]), "cathatbat") + + def test_booleans(self): + """It should return a concatenated string of booleans.""" + self.assertEqual(list_to_string([True, False]), "TrueFalse") + + # Edge cases + def test_mixed_types(self): + """It should return a concatenated string of mixed types.""" + self.assertEqual(list_to_string([1, "cat", 3.5, True]), "1cat3.5True") + + def test_single_element(self): + """It should return the same single element as a string.""" + self.assertEqual(list_to_string([42]), "42") + + def test_repeated_element(self): + """It should return repeated elements""" + self.assertEqual(list_to_string([1, 1, 1, 1]), "1111") + + def test_empty_element(self): + """It should ignore empty elements""" + self.assertEqual(list_to_string(["cat", "", "dog", ""]), "catdog") + + def test_space_element(self): + """It should return elements with space elements""" + self.assertEqual(list_to_string(["cat", " ", "dog", " "]), "cat dog ") + + # Defensive tests + def test_none_input(self): + """It should raise an AssertionError for None input.""" + with self.assertRaises(AssertionError): + list_to_string(None) + + def test_non_list_input(self): + """It should raise an AssertionError for non-list input.""" + with self.assertRaises(AssertionError): + list_to_string("not a list") + + def test_empty_list(self): + """It should raise an AssertionError for empty list.""" + with self.assertRaises(AssertionError): + list_to_string([]) + + +if __name__ == "__main__": + unittest.main() From 0616185351158844615bd499bb5b81d0df13cd0a Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 16:48:12 +0300 Subject: [PATCH 15/23] some formatting error --- solutions/list_to_string.py | 12 ++++++------ solutions/tests/test_list_to_string.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/solutions/list_to_string.py b/solutions/list_to_string.py index 3435d0bb1..363b12dc0 100644 --- a/solutions/list_to_string.py +++ b/solutions/list_to_string.py @@ -36,14 +36,14 @@ def list_to_string(lst: list) -> str: - If the input is `None`. - If the list is empty. - >>> list_to_string([1, -4, 3.14]) - '1-43.14' + >>> list_to_string([1, -4, 3.14]) + '1-43.14' - >>> list_to_string([True, 'hello']) - 'Truehello' + >>> list_to_string([True, 'hello']) + 'Truehello' - >>> list_to_string(['cat', 'hat', 'bat']) - 'cathatbat' + >>> list_to_string(['cat', 'hat', 'bat']) + 'cathatbat' """ # Validate Input assert isinstance(lst, list), "Input must be a list." diff --git a/solutions/tests/test_list_to_string.py b/solutions/tests/test_list_to_string.py index db3769c62..1c7aa2fdf 100644 --- a/solutions/tests/test_list_to_string.py +++ b/solutions/tests/test_list_to_string.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Module: test_list_to_string +Module: test_list_to_string. Description: This module contains test cases for the `list_to_string` function defined From 4c76bbadbf120f2a38f300b40471cc15b65b18b3 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 20:07:28 +0300 Subject: [PATCH 16/23] format changes --- solutions/list_to_string.py | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/list_to_string.py b/solutions/list_to_string.py index 363b12dc0..44c8e7f63 100644 --- a/solutions/list_to_string.py +++ b/solutions/list_to_string.py @@ -55,7 +55,6 @@ def list_to_string(lst: list) -> str: # Iterate through the list and convert each element to a string for element in lst: - result += str(element) # Convert each element to string and concatenate return result From aeea0dc227de130b7477e41599b3a62cf3b26050 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 20:10:15 +0300 Subject: [PATCH 17/23] modified to have all ci checks correct --- solutions/tests/test_list_to_string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_list_to_string.py b/solutions/tests/test_list_to_string.py index 1c7aa2fdf..db3769c62 100644 --- a/solutions/tests/test_list_to_string.py +++ b/solutions/tests/test_list_to_string.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Module: test_list_to_string. +Module: test_list_to_string Description: This module contains test cases for the `list_to_string` function defined From 26cadb6fa0075ba0a99389e9349a556d0da8ac0d Mon Sep 17 00:00:00 2001 From: "s.ashour82" Date: Mon, 30 Dec 2024 22:46:38 +0200 Subject: [PATCH 18/23] factorial challenge factorial challenge is done --- .vscode/settings.json | 6 +++- solutions/factorial.py | 56 +++++++++++++++++++++++++++++ solutions/tests/test_factorial.py | 59 +++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 solutions/factorial.py create mode 100644 solutions/tests/test_factorial.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 058679944..1b51e567f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -116,5 +116,9 @@ "markdown.extension.toc.updateOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" - } + }, + "cSpell.words": [ + "Ashour", + "Saad" + ] } diff --git a/solutions/factorial.py b/solutions/factorial.py new file mode 100644 index 000000000..6c9afb155 --- /dev/null +++ b/solutions/factorial.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for Computing the factorial of a non-negative integer n. + +Module contents: + - factorial: is the product of all positive integers less than or equal to n. + +Created on XX XX XX +@author: Saad M. Ashour +""" + + +def factorial(n: int) -> int: + """ + Computes the factorial of a non-negative integer n. + + The factorial of a non-negative integer n + is the product of all positive integers less than or equal to n. + + Parameters: + n (int): A non-negative integer. + + Returns: + int: The factorial of the input integer n. + + Raises: + ValueError: If n is negative, as factorial is not defined for negative numbers. + TypeError: If n is not an integer, as factorials are only defined for integers. + + Examples: + >>> factorial(0) + 1 + + >>> factorial(1) + 1 + + >>> factorial(5) + 120 + + >>> factorial(3) + 6 + """ + # Validate input type and value + if not isinstance(n, int): + raise TypeError("Input must be an integer.") + + if n < 0: + raise ValueError("Factorial is not defined for negative numbers.") + + # Base case for recursion: 0! = 1 + if n == 0 or n == 1: + return 1 + + # Recursive case: n! = n * (n-1)! + return n * factorial(n - 1) diff --git a/solutions/tests/test_factorial.py b/solutions/tests/test_factorial.py new file mode 100644 index 000000000..0c3e3f40c --- /dev/null +++ b/solutions/tests/test_factorial.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for factorial function. + +@author: Saad M. Ashour +""" + +import unittest +from solutions.factorial import factorial + + +class TestFactorial(unittest.TestCase): + """ + Unit tests for the factorial function. + + The factorial function computes the factorial of a non-negative integer using recursion. + These tests verify its correctness for various inputs, + including edge cases, valid cases, and invalid cases. + """ + + +def test_factorial_zero(self): + """It should evaluate 0 to 1""" + actual = factorial(0) + expected = 1 + self.assertEqual(actual, expected) + + +def test_factorial_one(self): + """It should evaluate 1 to 1""" + actual = factorial(1) + expected = 1 + self.assertEqual(actual, expected) + + +def test_factorial_positive(self): + """It should evaluate 5 to 120""" + actual = factorial(5) + expected = 120 + self.assertEqual(actual, expected) + + +def test_factorial_negative(self): + """factorial of negative integer will raise Error""" + with self.assertRaises(ValueError): + factorial(-1) + + +def test_factorial_non_integer(self): + """factorial of non integer will raise Error""" + with self.assertRaises(TypeError): + factorial(3.5) + with self.assertRaises(TypeError): + factorial("string") + + +if __name__ == "__main__": + unittest.main() From 7645c04a22daca870d52cabb25ab38466788cd5b Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:40:36 +0200 Subject: [PATCH 19/23] adding descriptive tests --- solutions/mean.py | 6 +++++- solutions/tests/test_mean.py | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/solutions/mean.py b/solutions/mean.py index bb5168f3d..642f2b78c 100644 --- a/solutions/mean.py +++ b/solutions/mean.py @@ -22,8 +22,12 @@ def mean(a: list) -> float: 3.0 >>> mean([10, 20, 30, 40, 50]) 30.0 + >>> mean([1.5, 2.5, 3.5]) + 2.5 + >>>mean([2]) + 2.0 """ if not isinstance(a, list): return "invalid input" - + # return the mean of the list by dividing the sum of the list by the length of the list return sum(a) / len(a) diff --git a/solutions/tests/test_mean.py b/solutions/tests/test_mean.py index 90b160c64..920dfecf3 100644 --- a/solutions/tests/test_mean.py +++ b/solutions/tests/test_mean.py @@ -1,5 +1,15 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +""" +Module: test_mean + +Description: + This module contains the unit tests for the mean function in the solutions package. + the function calculates the mean of a list of numbers. + +Author: Mohammed Elfadil +Date: 31/12/2024 +""" import unittest from solutions.mean import mean @@ -7,25 +17,25 @@ class TestMean(unittest.TestCase): """test the mean function""" - def test_1(self): + def test_positive_numbers(self): """It should evaluate 3.0""" actual = mean([1, 2, 3, 4, 5]) expected = 3.0 self.assertEqual(actual, expected) - def test_2(self): + def test_positive_numbers2(self): """It should evaluate 30.0""" actual = mean([10, 20, 30, 40, 50]) expected = 30.0 self.assertEqual(actual, expected) - def test_3(self): + def test_zeros(self): """It should evaluate 0.0""" actual = mean([0, 0, 0, 0, 0]) expected = 0.0 self.assertEqual(actual, expected) - def test_4(self): + def test_single_input(self): """It should evaluate 1.0""" actual = mean([1]) expected = 1.0 @@ -48,3 +58,9 @@ def test_floats(self): actual = mean([1.5, 2.5, 3.5]) expected = 2.5 self.assertEqual(actual, expected) + + def test_string(self): + """It should evaluate inavlid input""" + actual = mean(["a", "b", "c"]) + expected = "invalid input" + self.assertEqual(actual, expected) From 15314ccc44b0338643eb8d06c7aefb8c6f7fce7e Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:43:38 +0200 Subject: [PATCH 20/23] fix tests --- solutions/tests/test_mean.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solutions/tests/test_mean.py b/solutions/tests/test_mean.py index 920dfecf3..0b43e9845 100644 --- a/solutions/tests/test_mean.py +++ b/solutions/tests/test_mean.py @@ -11,6 +11,7 @@ Date: 31/12/2024 """ import unittest + from solutions.mean import mean @@ -61,6 +62,6 @@ def test_floats(self): def test_string(self): """It should evaluate inavlid input""" - actual = mean(["a", "b", "c"]) + actual = mean("2 4 6") expected = "invalid input" self.assertEqual(actual, expected) From 551a3ecf54614ad4d8ff84044a4c3eccb25d452f Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:45:11 +0200 Subject: [PATCH 21/23] fix formmatting --- solutions/tests/test_mean.py | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/tests/test_mean.py b/solutions/tests/test_mean.py index 0b43e9845..b1bad4fa8 100644 --- a/solutions/tests/test_mean.py +++ b/solutions/tests/test_mean.py @@ -11,7 +11,6 @@ Date: 31/12/2024 """ import unittest - from solutions.mean import mean From d62b5c1073ec555c7d6ca1660ee0f1be9728dadb Mon Sep 17 00:00:00 2001 From: Moealfadil <142026026+Moealfadil@users.noreply.github.com> Date: Tue, 31 Dec 2024 03:56:22 +0200 Subject: [PATCH 22/23] fixed formatting --- solutions/mean.py | 7 ++++--- solutions/tests/test_mean.py | 13 ++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/solutions/mean.py b/solutions/mean.py index 642f2b78c..9c9d4cf01 100644 --- a/solutions/mean.py +++ b/solutions/mean.py @@ -6,7 +6,7 @@ Module contents: - mean: finds the mean of a list of numbers. -Created on XX XX XX +Created on 29/12/2024 @author: Mohammed Elfadil """ @@ -27,7 +27,8 @@ def mean(a: list) -> float: >>>mean([2]) 2.0 """ - if not isinstance(a, list): - return "invalid input" + # check if the argument is a list + assert isinstance(a, list), "invalid input" + # return the mean of the list by dividing the sum of the list by the length of the list return sum(a) / len(a) diff --git a/solutions/tests/test_mean.py b/solutions/tests/test_mean.py index b1bad4fa8..6acc4cfc9 100644 --- a/solutions/tests/test_mean.py +++ b/solutions/tests/test_mean.py @@ -10,6 +10,7 @@ Author: Mohammed Elfadil Date: 31/12/2024 """ + import unittest from solutions.mean import mean @@ -43,9 +44,8 @@ def test_single_input(self): def test_integer(self): """It should evaluate invalid input""" - actual = mean(1) - expected = "invalid input" - self.assertEqual(actual, expected) + with self.assertRaises(AssertionError): + mean(2) def test_negatives(self): """It should evaluate -4.0""" @@ -60,7 +60,6 @@ def test_floats(self): self.assertEqual(actual, expected) def test_string(self): - """It should evaluate inavlid input""" - actual = mean("2 4 6") - expected = "invalid input" - self.assertEqual(actual, expected) + """Test that invalid input raises an AssertionError.""" + with self.assertRaises(AssertionError): + mean("2, 3, 4, 5") From ba6952dbf29cfb08dd21900e710ac04df216cf5b Mon Sep 17 00:00:00 2001 From: "s.ashour82" Date: Tue, 31 Dec 2024 13:03:51 +0200 Subject: [PATCH 23/23] Update settings.json --- .vscode/settings.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1b51e567f..058679944 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -116,9 +116,5 @@ "markdown.extension.toc.updateOnSave": false, "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" - }, - "cSpell.words": [ - "Ashour", - "Saad" - ] + } }