From e07dc5a3ff440d89938bbc2547f0ed2d12646402 Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Sun, 22 Dec 2024 22:30:33 +0200 Subject: [PATCH 01/14] Add is_even and is_positive functions to solutions --- solutions/is_even.py | 58 ++++++++++++++++++++++++++++++++++++++++ solutions/is_positive.py | 58 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 solutions/is_even.py create mode 100644 solutions/is_positive.py diff --git a/solutions/is_even.py b/solutions/is_even.py new file mode 100644 index 000000000..bc2b96d6c --- /dev/null +++ b/solutions/is_even.py @@ -0,0 +1,58 @@ +""" +A module for checking if a number is even. + + +Author: Luyando Ennie Chitindi +Date: 12/22/2024 + + +This module has a function that checks if a number is even. +The function takes an integer as an input and returns a boolean value that +indicates whether the number is divisible by 2. + + +Function: +- is_even(number: int) -> bool + + +Exception: +- Raises TypeError if an input is not an integer. + + +Example: +>>> is_even(4) +True +>>> is_even(3) +False +>>> is_even(0) +True +""" + +def is_even(number: int) -> bool: + """ + This will check if number is even. + + + Argument: + number (int): The number to use to check if it is even. + + + Returns: + bool: Only true if the number is even, false otherwise. + + + Raises: + TypeError: If the argument provided is not an integer. + + + Example: + >>> is_even(6) + True + >>> is_even(7) + False + >>> is_even(0) + True + """ + if not isinstance(number, int): + raise TypeError("Input must be an integer.") + return number % 2 == 0 diff --git a/solutions/is_positive.py b/solutions/is_positive.py new file mode 100644 index 000000000..2a8e7bce2 --- /dev/null +++ b/solutions/is_positive.py @@ -0,0 +1,58 @@ +""" +A module for checking if a number is positive. + + +Author: Luyando Chitindi +Date: 12/22/2024 + + +This module contains a function that deals with checking if a number is positive. +The function will take an integer as an input and returns a boolean value indicating +whether the number is greater than zero. + +Function: +- is positive(number: int) -> bool + + +Exceptions: +-Raises TypeError if the input is not an integer. + + +For Example: +>>> is_positive(5) +True +>>> is_positive(-3) +False +>>> is_positive(0) +False +""" + + +def is_positive(number: int) -> bool: + """ + This will check if the number is positive. + + + Arguments: + number (int): The number to check if it is positive. + + + Returns: + bool: True if the number is positive, false otherwise. + + + Raises: + TypeError: If the argument that is provided is not an integer. + + + Example: + >>> is_positive(10) + True + >>> is_positive(-5) + False + >>> is_positive(0) + False + """ + if not isinstance(number, int): + raise TypeError("Input must be an integer.") + return number > 0 From 84280de25c4dd4fb393d7420f7598f186d534f46 Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Mon, 23 Dec 2024 01:30:33 +0200 Subject: [PATCH 02/14] Removed is_even.py from positive-number-function branch --- solutions/is_even.py | 58 -------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 solutions/is_even.py diff --git a/solutions/is_even.py b/solutions/is_even.py deleted file mode 100644 index bc2b96d6c..000000000 --- a/solutions/is_even.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -A module for checking if a number is even. - - -Author: Luyando Ennie Chitindi -Date: 12/22/2024 - - -This module has a function that checks if a number is even. -The function takes an integer as an input and returns a boolean value that -indicates whether the number is divisible by 2. - - -Function: -- is_even(number: int) -> bool - - -Exception: -- Raises TypeError if an input is not an integer. - - -Example: ->>> is_even(4) -True ->>> is_even(3) -False ->>> is_even(0) -True -""" - -def is_even(number: int) -> bool: - """ - This will check if number is even. - - - Argument: - number (int): The number to use to check if it is even. - - - Returns: - bool: Only true if the number is even, false otherwise. - - - Raises: - TypeError: If the argument provided is not an integer. - - - Example: - >>> is_even(6) - True - >>> is_even(7) - False - >>> is_even(0) - True - """ - if not isinstance(number, int): - raise TypeError("Input must be an integer.") - return number % 2 == 0 From cec61c8be188206752287d9e4d93778fcda044fa Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 17:27:37 +0300 Subject: [PATCH 03/14] 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 04/14] 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 05/14] 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 06/14] 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 07/14] 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 08/14] 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 bc9aeb5954b9aea3f70446a0838d96005f81a3a5 Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 20:02:22 +0300 Subject: [PATCH 09/14] 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 462809144520f72929459c3d1fc6e350cd25bc7e Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Sun, 29 Dec 2024 20:18:34 +0200 Subject: [PATCH 10/14] Add test_is_positive.py from is-even branch --- solutions/tests/test_is_positive.py | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 solutions/tests/test_is_positive.py diff --git a/solutions/tests/test_is_positive.py b/solutions/tests/test_is_positive.py new file mode 100644 index 000000000..cadb586d0 --- /dev/null +++ b/solutions/tests/test_is_positive.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for the is_positive function. + +@author: Luyando .E. Chitindi +""" + +import unittest +from solutions.is_positive import is_positive + + +class TestIsPositive(unittest.TestCase): + """Test the is_positive function""" + + def test_positive_number(self): + """It should evaluate 4 to True""" + actual = is_positive(4) + expected = True + self.assertEqual(actual, expected) + + def test_negative_number(self): + """It should evaluate -3 to False""" + actual = is_positive(-3) + expected = False + self.assertEqual(actual, expected) + + def test_zero(self): + """It should evaluate 0 to False""" + actual = is_positive(0) + expected = False + self.assertEqual(actual, expected) + + def test_non_integer_input(self): + """It should raise an assertion error if the argument is not an integer""" + with self.assertRaises(AssertionError): + is_positive(3.5) + + with self.assertRaises(AssertionError): + is_positive("string") + + +if __name__ == "__main__": + unittest.main() From f94e33e3a59a69b563382a9c37c62b9988bdb5db Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Sun, 29 Dec 2024 20:32:02 +0200 Subject: [PATCH 11/14] updated is_positive.py --- solutions/is_positive.py | 76 +++++++++++++++------------------------- 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/solutions/is_positive.py b/solutions/is_positive.py index 2a8e7bce2..37cb1015c 100644 --- a/solutions/is_positive.py +++ b/solutions/is_positive.py @@ -1,58 +1,38 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- """ -A module for checking if a number is positive. +Created on XX XX XX +A module for checking if an integer is positive. -Author: Luyando Chitindi -Date: 12/22/2024 - - -This module contains a function that deals with checking if a number is positive. -The function will take an integer as an input and returns a boolean value indicating -whether the number is greater than zero. - -Function: -- is positive(number: int) -> bool - - -Exceptions: --Raises TypeError if the input is not an integer. - - -For Example: ->>> is_positive(5) -True ->>> is_positive(-3) -False ->>> is_positive(0) -False +@author: Luyando .E. Chitindi """ def is_positive(number: int) -> bool: """ - This will check if the number is positive. - - - Arguments: - number (int): The number to check if it is positive. - - - Returns: - bool: True if the number is positive, false otherwise. - - - Raises: - TypeError: If the argument that is provided is not an integer. - - - Example: - >>> is_positive(10) - True - >>> is_positive(-5) - False - >>> is_positive(0) - False + This checks if an integer is positive. + + Parameters: + number: int, the number to check + + Returns -> bool: + True if the number is positive, false otherwise. + + Raises: + AssertionError: if the input is not an integer + + Example: + >>> is_positive(4) + True + >>> is_positive(-3) + False + >>> is_positive(0) + False + >>> is_positive("hello") + Traceback (most recent call last): + ... + AssertionError: Input must be an integer. """ - if not isinstance(number, int): - raise TypeError("Input must be an integer.") + assert isinstance(number, int), "Input must be an integer" return number > 0 From ae50aad0427d6c81585183e1ca5e262275fdbbc3 Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Sun, 29 Dec 2024 21:50:57 +0200 Subject: [PATCH 12/14] Resolve conflicts in .vscode/settings.json --- .vscode/settings.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index bbda5188d..c25cbf91b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -122,5 +122,8 @@ "source.fixAll.ruff": true, "source.organizeImports.ruff": true } + }, + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" } } From f84a739d797934ce79960efb9e84bb3aef80a868 Mon Sep 17 00:00:00 2001 From: Luyando Chitindi Date: Sun, 29 Dec 2024 21:59:48 +0200 Subject: [PATCH 13/14] Resolve conflicts in .vscode/settings.json --- .vscode/settings.json | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c25cbf91b..058679944 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -114,15 +114,6 @@ // 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 - } - }, "[python]": { "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 14/14] 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