diff --git a/collaboration/learning_goals.md b/collaboration/learning_goals.md index 878a874b5..3c9261a17 100644 --- a/collaboration/learning_goals.md +++ b/collaboration/learning_goals.md @@ -93,7 +93,7 @@ the challenges in this class --- -### Chrismy +### Chrismy Augustin - Gain more knowledge and hands-on experience with industry-standard tools and technologies like VS Code, Python, GitHub. diff --git a/solutions/challenge_9/__init__.py b/solutions/challenge_9/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/challenge_9/sum_of_digits.py b/solutions/challenge_9/sum_of_digits.py new file mode 100644 index 000000000..a758eaf37 --- /dev/null +++ b/solutions/challenge_9/sum_of_digits.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for calculating the sum of the digits of a positive integer. + +Module contents: + sum_of_digits: calculates the sum of the digits of a positive integer. + +Created on January 3rd, 2025 +@author: Semira Tesfai +""" + + +def sum_of_digits(n: int) -> int: + """Calculate the sum of the digits of a positive integer. + + Parameters + ---------- + n : int + The non-negative integer whose digits will be summed. + + Returns + ------- + int + The sum of the digits of the given integer. + + Raises + ------ + ValueError + If the argument is not a non-negative integer. + + Examples + -------- + >>> sum_of_digits(123) + 6 + >>> sum_of_digits(4567) + 22 + >>> sum_of_digits(0) + 0 + """ + + if not isinstance(n, int) or n < 0: + raise ValueError("input must be a non-negative integer.") + + return sum(int(digit) for digit in str(n)) diff --git a/solutions/tests/challenge_9/__init__.py b/solutions/tests/challenge_9/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/tests/challenge_9/test_sum_of_digits.py b/solutions/tests/challenge_9/test_sum_of_digits.py new file mode 100644 index 000000000..a509212cf --- /dev/null +++ b/solutions/tests/challenge_9/test_sum_of_digits.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Unit tests for the sum_of_digits function. +Created on January 3rd, 2025 +""" + +import unittest + +from solutions.challenge_9.sum_of_digits import sum_of_digits + + +class TestSumOfDigits(unittest.TestCase): + """Tests for the sum_of_digits function.""" + + def test_with_positive_integer(self): + """Ensure that the sum of digits of a positive integer is calculated correctly.""" + self.assertEqual(sum_of_digits(123), 6) + + def test_with_large_integer(self): + """Ensure that the sum of digits of a large positive integer is calculated correctly.""" + self.assertEqual(sum_of_digits(4567), 22) + + def test_with_zero(self): + """Ensure that the sum of digits of zero is zero.""" + self.assertEqual(sum_of_digits(0), 0) + + def test_with_invalid_input(self): + """Ensure that the function raises an assertion error for invalid inputs.""" + with self.assertRaises(ValueError): + sum_of_digits(-5) + with self.assertRaises(ValueError): + sum_of_digits(4.5) + with self.assertRaises(ValueError): + sum_of_digits("123") + + +if __name__ == "__main__": + unittest.main()