From cec61c8be188206752287d9e4d93778fcda044fa Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Sun, 29 Dec 2024 17:27:37 +0300 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 44fcb9021cc92b5565d0e529c6c297011723f11b Mon Sep 17 00:00:00 2001 From: Falaq Abdulmajeed Date: Mon, 30 Dec 2024 11:58:24 +0300 Subject: [PATCH 8/8] 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