From 2010bae5bae2079bb86afc3a6bbdba5bab3a6b79 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 5 Jan 2025 21:12:51 +0100 Subject: [PATCH 1/4] fixes #29 --- solutions/sum_of_list.py | 47 ++++++++++++++++++++++ solutions/tests/test_sum_of_list.py | 60 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 solutions/sum_of_list.py create mode 100644 solutions/tests/test_sum_of_list.py diff --git a/solutions/sum_of_list.py b/solutions/sum_of_list.py new file mode 100644 index 000000000..f99079335 --- /dev/null +++ b/solutions/sum_of_list.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +# Write a function that takes a list and returns the sum of that list. + +""" +A module for getting the sum of numbers in a list + +Module contents: + sum_of_list: returns the sum of numbers in a list. + +Created on 02/01/2025 +@author: Ndubuisi Agbo +""" + + +def sum_of_list(numbers: list) -> int | float: + """Gives the sum of the elements in a list. + + Parameters: + numbers (list): the list of numbers to sum. + + Returns: + int or float: the sum of the numbers in the list. + + Raises: + AssertionError: if the list contains a non-numeric element. + AssertionError: if the argument is not a list. + + >>> sum_of_list([3,6,9]) + 18 + + >>> sum_of_list([4.2, 6.9, 7.1]) + 18.2 + + >>> sum_of_list([8, 3.8, 1, 9]) + 21.8 + """ + + assert isinstance(numbers, list), "numbers must be a list" + + # checks that all the elements in the list are a number + for num in numbers: + assert isinstance(num, int | float) + + return sum(numbers) diff --git a/solutions/tests/test_sum_of_list.py b/solutions/tests/test_sum_of_list.py new file mode 100644 index 000000000..cf49ba2ec --- /dev/null +++ b/solutions/tests/test_sum_of_list.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +A module for testing the sum_of_list function. + +Test categories: + - Standard cases: + - Edge cases: + - Defensive tests: + +Created on 02/01/2025 +@author: Ndubuisi Agbo +""" + +import unittest +from ..sum_of_list import sum_of_list + + +class TestSumOfList(unittest.TestCase): + """This module tests the sum_of_list function""" + + # Standard test cases + def test_list_of_integers_input(self): + """It should return the sum of the list""" + self.assertEqual(sum_of_list([3, 6, 9]), 18) + + def test_list_of_floats_input(self): + """It should return the sum of the list""" + self.assertEqual(sum_of_list([4.2, 6.9, 7.1]), 18.2) + + def test_list_of_mixed_number_input(self): + """It should return the sum of the list""" + self.assertEqual(sum_of_list([8, 3.8, 1, 9]), 21.8) + + # Edge cases + def test_empty_list_input(self): + """Returns zero when an empty list is passed""" + self.assertEqual(sum_of_list([]), 0) + + def test_single_element(self): + """"It should return the element in the list""" + self.assertEqual(sum_of_list([7]), 7) + + # Defensive tests + def test_list_of_strings(self): + """Raises AssertionError when a list containing a string is passed""" + with self.assertRaises(AssertionError): + sum_of_list(["3", "9", "6"]) + + def test_non_list_input(self): + """Raises AssertionError when a non-list is passed""" + with self.assertRaises(AssertionError): + sum_of_list("two") + + + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From fc74d9d31d98b92769ef3a525de1c97bc4cfa7d6 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 6 Jan 2025 03:52:06 +0100 Subject: [PATCH 2/4] corrected a mistake --- solutions/tests/test_sum_of_list.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/solutions/tests/test_sum_of_list.py b/solutions/tests/test_sum_of_list.py index cf49ba2ec..34589bd79 100644 --- a/solutions/tests/test_sum_of_list.py +++ b/solutions/tests/test_sum_of_list.py @@ -48,11 +48,6 @@ def test_list_of_strings(self): with self.assertRaises(AssertionError): sum_of_list(["3", "9", "6"]) - def test_non_list_input(self): - """Raises AssertionError when a non-list is passed""" - with self.assertRaises(AssertionError): - sum_of_list("two") - From b09c19624d0d8915b2a78b0f92f382be99ee7cae Mon Sep 17 00:00:00 2001 From: root Date: Mon, 6 Jan 2025 04:10:20 +0100 Subject: [PATCH 3/4] formatted files --- solutions/tests/test_sum_of_list.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/solutions/tests/test_sum_of_list.py b/solutions/tests/test_sum_of_list.py index 34589bd79..7f17be1e8 100644 --- a/solutions/tests/test_sum_of_list.py +++ b/solutions/tests/test_sum_of_list.py @@ -21,35 +21,38 @@ class TestSumOfList(unittest.TestCase): """This module tests the sum_of_list function""" # Standard test cases - def test_list_of_integers_input(self): + def test_list_of_integers(self): """It should return the sum of the list""" self.assertEqual(sum_of_list([3, 6, 9]), 18) - def test_list_of_floats_input(self): + def test_list_of_floats(self): """It should return the sum of the list""" self.assertEqual(sum_of_list([4.2, 6.9, 7.1]), 18.2) - def test_list_of_mixed_number_input(self): + def test_list_of_mixed_number(self): """It should return the sum of the list""" self.assertEqual(sum_of_list([8, 3.8, 1, 9]), 21.8) # Edge cases - def test_empty_list_input(self): + def test_empty_list(self): """Returns zero when an empty list is passed""" self.assertEqual(sum_of_list([]), 0) - + def test_single_element(self): - """"It should return the element in the list""" + """ "It should return the element in the list""" self.assertEqual(sum_of_list([7]), 7) - + # Defensive tests def test_list_of_strings(self): """Raises AssertionError when a list containing a string is passed""" with self.assertRaises(AssertionError): sum_of_list(["3", "9", "6"]) - + def test_non_list(self): + """Raises AssertionError when the argument is not a list""" + with self.assertRaises(AssertionError): + sum_of_list("two") if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() From ee86f5b15a28ec5ef03c32988219f8ab9431621e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 8 Jan 2025 06:25:01 +0100 Subject: [PATCH 4/4] removed extra quotation marks --- solutions/tests/test_sum_of_list.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solutions/tests/test_sum_of_list.py b/solutions/tests/test_sum_of_list.py index 7f17be1e8..c62ea3c7b 100644 --- a/solutions/tests/test_sum_of_list.py +++ b/solutions/tests/test_sum_of_list.py @@ -3,11 +3,10 @@ """ A module for testing the sum_of_list function. - -Test categories: - - Standard cases: - - Edge cases: - - Defensive tests: +This module tests the following categories: + - Standard cases + - Edge cases + - Defensive tests Created on 02/01/2025 @author: Ndubuisi Agbo @@ -39,7 +38,7 @@ def test_empty_list(self): self.assertEqual(sum_of_list([]), 0) def test_single_element(self): - """ "It should return the element in the list""" + """It should return the element in the list""" self.assertEqual(sum_of_list([7]), 7) # Defensive tests