From aac956631365f3e4e7a50829098b9d562b331370 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 02:02:25 +0100 Subject: [PATCH 01/20] Add files via upload --- solutions/count_vowels.py | 20 ++++++++++++++++++++ solutions/fizz_buzz.py | 30 ++++++++++++++++++++++++++++++ solutions/reverse_string.py | 15 +++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 solutions/count_vowels.py create mode 100644 solutions/fizz_buzz.py create mode 100644 solutions/reverse_string.py diff --git a/solutions/count_vowels.py b/solutions/count_vowels.py new file mode 100644 index 000000000..142a86ebc --- /dev/null +++ b/solutions/count_vowels.py @@ -0,0 +1,20 @@ +def count_vowels(s): + """ + Counts the number of vowels in the given string. + + Parameters: + s (str): The string in which to count vowels. + + Returns: + int: The number of vowels in the string. + + Example: + >>> count_vowels("hello") + 2 + >>> count_vowels("Nelsona") + 3 + """ + vowels = "aeiouAEIOU" # Define vowels (both lowercase and uppercase) + return sum( + 1 for char in s if char in vowels + ) # Count vowels using a generator expression diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py new file mode 100644 index 000000000..a4329a079 --- /dev/null +++ b/solutions/fizz_buzz.py @@ -0,0 +1,30 @@ +def fizz_buzz(n): + """ + Generates a list of numbers from 1 to n with the following rules: + - For multiples of 3, "Fizz" is added instead of the number. + - For multiples of 5, "Buzz" is added instead of the number. + - For multiples of both 3 and 5, "FizzBuzz" is added instead of the number. + + Parameters: + n (int): The upper limit of the range (inclusive) to generate. + + Returns: + list: A list containing the numbers and/or string representations according to the rules. + + Example: + >>> fizzbuzz(15) + [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] + """ + results = [] # Initialize an empty list to store the results + for i in range(1, n + 1): + if i % 3 == 0 and i % 5 == 0: + results.append( + "FizzBuzz" + ) # Append "FizzBuzz" for multiples of both 3 and 5 + elif i % 3 == 0: + results.append("Fizz") # Append "Fizz" for multiples of 3 + elif i % 5 == 0: + results.append("Buzz") # Append "Buzz" for multiples of 5 + else: + results.append(i) # Append the number itself if not a multiple of 3 or 5 + return results # Return the list of results diff --git a/solutions/reverse_string.py b/solutions/reverse_string.py new file mode 100644 index 000000000..045d135ac --- /dev/null +++ b/solutions/reverse_string.py @@ -0,0 +1,15 @@ +def reverse_string(s): + """ + Reverses the given string. + + Parameters: + s (str): The string to be reversed. + + Returns: + str: The reversed string. + + Example: + >>> reverse_string("hello") + 'olleh' + """ + return s[::-1] # Return the reversed string using slicing From 310dbe6022a9315aefb7a080126c566c2c98c735 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 02:03:36 +0100 Subject: [PATCH 02/20] Add files via upload --- solutions/tests/test_count_vowels.py | 55 ++++++++++++++++++ solutions/tests/test_fizz_buzz.py | 77 ++++++++++++++++++++++++++ solutions/tests/test_reverse_string.py | 56 +++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 solutions/tests/test_count_vowels.py create mode 100644 solutions/tests/test_fizz_buzz.py create mode 100644 solutions/tests/test_reverse_string.py diff --git a/solutions/tests/test_count_vowels.py b/solutions/tests/test_count_vowels.py new file mode 100644 index 000000000..043b86ed3 --- /dev/null +++ b/solutions/tests/test_count_vowels.py @@ -0,0 +1,55 @@ +import unittest + +from solutions.count_vowels import count_vowels # Import the count_vowels function + + +class TestCountVowels(unittest.TestCase): + """Test cases for the count_vowels function. + + This class contains unit tests for the count_vowels function to ensure + that it accurately counts the number of vowels in various input strings. + """ + + def test_count_vowels_normal(self): + """Test counting vowels in a normal string. + + This test checks that the function correctly counts the vowels in a + typical string with mixed characters. + """ + self.assertEqual(count_vowels("Nagham"), 2) + + def test_count_vowels_empty(self): + """Test counting vowels in an empty string. + + This test verifies that the function returns 0 when the input string + is empty. + """ + self.assertEqual(count_vowels(""), 0) + + def test_count_vowels_no_vowels(self): + """Test counting vowels in a string with no vowels. + + This test checks that the function returns 0 when there are no vowels + in the input string. + """ + self.assertEqual(count_vowels("bcdfghjklmnpqrstvwxyz"), 0) + + def test_count_vowels_case_insensitive(self): + """Test counting vowels in a case-insensitive manner. + + This test verifies that the function counts both uppercase and lowercase + vowels correctly. + """ + self.assertEqual(count_vowels("AbEcIdOfUg"), 5) + + def test_count_vowels_with_spaces(self): + """Test counting vowels in a string with spaces. + + This test checks that the function correctly counts vowels in a string + that contains spaces and punctuation. + """ + self.assertEqual(count_vowels("Hello World!"), 3) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py new file mode 100644 index 000000000..124eb14cd --- /dev/null +++ b/solutions/tests/test_fizz_buzz.py @@ -0,0 +1,77 @@ +import unittest + +from solutions.fizz_buzz import fizz_buzz + + +class TestFizzBuzz(unittest.TestCase): + """Test cases for the fizzbuzz function. + + This class contains unit tests for the fizzbuzz function to ensure + that it produces the expected output for various input values. + """ + + def test_fizzbuzz_15(self): + """Test FizzBuzz for n = 15. + + This test checks that the output for n=15 matches the expected + list which includes Fizz, Buzz, and FizzBuzz at the correct + positions. + """ + expected = [ + 1, + 2, + "Fizz", + 4, + "Buzz", + "Fizz", + 7, + 8, + "Fizz", + "Buzz", + 11, + "Fizz", + 13, + 14, + "FizzBuzz", + ] + self.assertEqual(fizz_buzz(15), expected) + + def test_fizzbuzz_3(self): + """Test FizzBuzz for n = 3. + + This test checks that for n=3, the output should return + a list containing the numbers 1, 2, and 'Fizz'. + """ + expected = [1, 2, "Fizz"] + self.assertEqual(fizz_buzz(3), expected) + + def test_fizzbuzz_5(self): + """Test FizzBuzz for n = 5. + + This test verifies that for n=5, the output list includes + numbers 1, 2, 'Fizz', 4, and 'Buzz'. + """ + expected = [1, 2, "Fizz", 4, "Buzz"] + self.assertEqual(fizz_buzz(5), expected) + + def test_fizzbuzz_1(self): + """Test FizzBuzz for n = 1. + + This test checks that for n=1, the output should simply be + a list containing the number 1. + """ + expected = [1] + self.assertEqual(fizz_buzz(1), expected) + + def test_fizzbuzz_0(self): + """Test FizzBuzz for n = 0. + + This test checks that for n=0, the output should be an + empty list since there are no numbers to include. + """ + expected = [] + self.assertEqual(fizz_buzz(0), expected) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/tests/test_reverse_string.py b/solutions/tests/test_reverse_string.py new file mode 100644 index 000000000..b0e906522 --- /dev/null +++ b/solutions/tests/test_reverse_string.py @@ -0,0 +1,56 @@ +import unittest +from solutions.reverse_string import ( + reverse_string, +) # Import the reverse_string function + + +class TestReverseString(unittest.TestCase): + """Test cases for the reverse_string function. + + This class contains unit tests for the reverse_string function to ensure + that it correctly reverses strings under various scenarios. + """ + + def test_reverse_normal(self): + """Test reversing a normal string. + + This test checks that the function correctly reverses a typical string + with multiple characters. + """ + self.assertEqual(reverse_string("hello"), "olleh") + + def test_reverse_empty(self): + """Test reversing an empty string. + + This test verifies that the function returns an empty string when + the input is empty. + """ + self.assertEqual(reverse_string(""), "") + + def test_reverse_single_character(self): + """Test reversing a single character string. + + This test checks that the function returns the same character when + the input is a single character string. + """ + self.assertEqual(reverse_string("a"), "a") + + def test_reverse_palindrome(self): + """Test reversing a palindrome. + + This test verifies that the function returns the same string when + the input is a palindrome. + """ + self.assertEqual(reverse_string("madam"), "madam") + + def test_reverse_spaces(self): + """Test reversing a string with spaces. + + This test checks that the function correctly reverses a string that + contains spaces, ensuring that the spaces are also reversed. + """ + self.assertEqual(reverse_string("hello world"), "dlrow olleh") + + +if __name__ == "__main__": + unittest.main() From d9a1a37a04d0b7ef67e1c39a09a8b93be6e33099 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 02:05:41 +0100 Subject: [PATCH 03/20] Update fizz_buzz.py From aac38adf36df331bc9bab1db4d6ffeeecc8411fe Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 02:06:23 +0100 Subject: [PATCH 04/20] Update fizz_buzz.py --- solutions/fizz_buzz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index a4329a079..eea2d3986 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -12,7 +12,7 @@ def fizz_buzz(n): list: A list containing the numbers and/or string representations according to the rules. Example: - >>> fizzbuzz(15) + >>> fizz_buzz(15) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] """ results = [] # Initialize an empty list to store the results From 12aec321be245731aa0330005416b630628e2d41 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:52:36 +0100 Subject: [PATCH 05/20] Update fizz_buzz.py --- solutions/fizz_buzz.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index eea2d3986..d42bf48b0 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -1,9 +1,22 @@ -def fizz_buzz(n): +""" +This module contains the fizz_buzz function which generates a list +of numbers from 1 to n with specific substitutions for multiples +of 3 and/or 5. + +The function follows the FizzBuzz rules: +- For multiples of 3, "Fizz" is added. +- For multiples of 5, "Buzz" is added. +- For multiples of both 3 and 5, "FizzBuzz" is added. + +Example usage: +>>> fizz_buzz(15) +[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] +""" + + +def fizz_buzz(n: int) -> list: """ - Generates a list of numbers from 1 to n with the following rules: - - For multiples of 3, "Fizz" is added instead of the number. - - For multiples of 5, "Buzz" is added instead of the number. - - For multiples of both 3 and 5, "FizzBuzz" is added instead of the number. + Generates a list of numbers from 1 to n with specific rules. Parameters: n (int): The upper limit of the range (inclusive) to generate. @@ -14,13 +27,18 @@ def fizz_buzz(n): Example: >>> fizz_buzz(15) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] + >>> fizz_buzz(3) + [1, 2, 'Fizz'] + >>> fizz_buzz(5) + [1, 2, 'Fizz', 4, 'Buzz'] """ + # Defensive assertion to check that n is a positive integer + assert isinstance(n, int) and n > 0, "Input must be a positive integer" + results = [] # Initialize an empty list to store the results for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: - results.append( - "FizzBuzz" - ) # Append "FizzBuzz" for multiples of both 3 and 5 + results.append("FizzBuzz") # Append "FizzBuzz" for multiples of both 3 and 5 elif i % 3 == 0: results.append("Fizz") # Append "Fizz" for multiples of 3 elif i % 5 == 0: From 8a51d8a17fc7e12a336403cd58cae03ce0b1e3c7 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 21:46:02 +0100 Subject: [PATCH 06/20] Update fizz_buzz.py --- solutions/fizz_buzz.py | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index d42bf48b0..fda334441 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -1,22 +1,9 @@ -""" -This module contains the fizz_buzz function which generates a list -of numbers from 1 to n with specific substitutions for multiples -of 3 and/or 5. - -The function follows the FizzBuzz rules: -- For multiples of 3, "Fizz" is added. -- For multiples of 5, "Buzz" is added. -- For multiples of both 3 and 5, "FizzBuzz" is added. - -Example usage: ->>> fizz_buzz(15) -[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] -""" - - -def fizz_buzz(n: int) -> list: +def fizzbuzz(n: int) -> list: """ - Generates a list of numbers from 1 to n with specific rules. + Generates a list of numbers from 1 to n with the following rules: + - For multiples of 3, "Fizz" is added instead of the number. + - For multiples of 5, "Buzz" is added instead of the number. + - For multiples of both 3 and 5, "FizzBuzz" is added instead of the number. Parameters: n (int): The upper limit of the range (inclusive) to generate. @@ -25,24 +12,17 @@ def fizz_buzz(n: int) -> list: list: A list containing the numbers and/or string representations according to the rules. Example: - >>> fizz_buzz(15) + >>> fizzbuzz(15) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] - >>> fizz_buzz(3) - [1, 2, 'Fizz'] - >>> fizz_buzz(5) - [1, 2, 'Fizz', 4, 'Buzz'] """ - # Defensive assertion to check that n is a positive integer - assert isinstance(n, int) and n > 0, "Input must be a positive integer" - results = [] # Initialize an empty list to store the results for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: results.append("FizzBuzz") # Append "FizzBuzz" for multiples of both 3 and 5 elif i % 3 == 0: - results.append("Fizz") # Append "Fizz" for multiples of 3 + results.append("Fizz") # Append "Fizz" for multiples of 3 elif i % 5 == 0: - results.append("Buzz") # Append "Buzz" for multiples of 5 + results.append("Buzz") # Append "Buzz" for multiples of 5 else: - results.append(i) # Append the number itself if not a multiple of 3 or 5 + results.append(i) # Append the number itself if not a multiple of 3 or 5 return results # Return the list of results From 9d731739df620d31bc99104fc0ffe4cdc6d8bd29 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 22:02:07 +0100 Subject: [PATCH 07/20] Update test_fizz_buzz.py --- solutions/tests/test_fizz_buzz.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py index 124eb14cd..44d467f3d 100644 --- a/solutions/tests/test_fizz_buzz.py +++ b/solutions/tests/test_fizz_buzz.py @@ -8,6 +8,8 @@ class TestFizzBuzz(unittest.TestCase): This class contains unit tests for the fizzbuzz function to ensure that it produces the expected output for various input values. + It verifies the correctness of the function across different scenarios, + including edge cases. """ def test_fizzbuzz_15(self): From ef3490bcc6626a14323414cd18a8bc9bf95f144d Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 22:20:21 +0100 Subject: [PATCH 08/20] Update test_fizz_buzz.py adding a module doccstring with some path modifications to avoid formatting issues --- solutions/tests/test_fizz_buzz.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py index 44d467f3d..55befc430 100644 --- a/solutions/tests/test_fizz_buzz.py +++ b/solutions/tests/test_fizz_buzz.py @@ -1,5 +1,17 @@ +""" +Unit tests for the fizzbuzz function from the solutions.fizz_buzz module. + +This class uses the unittest framework to validate the behavior of the fizzbuzz function +across various input values, ensuring it produces the correct output lists. Tests include +cases for n = 0, 1, 3, 5, and 15, verifying the proper inclusion of Fizz, Buzz, and FizzBuzz +in the outputs. +""" + import unittest +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) from solutions.fizz_buzz import fizz_buzz @@ -8,8 +20,6 @@ class TestFizzBuzz(unittest.TestCase): This class contains unit tests for the fizzbuzz function to ensure that it produces the expected output for various input values. - It verifies the correctness of the function across different scenarios, - including edge cases. """ def test_fizzbuzz_15(self): From b1843aa5c2851e0765ff4bd901b735006d0b3d19 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 22:57:35 +0100 Subject: [PATCH 09/20] Update count_vowels.py added module docstring, and defensive assertions --- solutions/count_vowels.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/solutions/count_vowels.py b/solutions/count_vowels.py index 142a86ebc..3dae75601 100644 --- a/solutions/count_vowels.py +++ b/solutions/count_vowels.py @@ -1,10 +1,29 @@ -def count_vowels(s): +""" +This module provides a function to count the number of vowels in a given text. +The vowels here are a,e,i,o,u,A,E,I,O,U. + +Function: +- count_vowels: Counts vowels in a provided string. + +Raises: + TypeError: If the input `text` is not a string. + ValueError: If the input `text` is an empty string. + +Example: + >>> vowel_counter("Deadline") + 4 +""" + + +def count_vowels(s) -> int:: """ Counts the number of vowels in the given string. Parameters: s (str): The string in which to count vowels. + The vowels are "a,e,i,o,u,A,E,I,O,U" + Returns: int: The number of vowels in the string. @@ -13,7 +32,17 @@ def count_vowels(s): 2 >>> count_vowels("Nelsona") 3 + >>> count_vowels("This is amazing.") + 5 """ + # Defensive assertion: Ensure the input is a string + if not isinstance(text, str): + raise TypeError("Input must be a string") + + # Defensive assertion: Ensure the string is not empty + if not text: + raise ValueError("Input string cannot be empty") + vowels = "aeiouAEIOU" # Define vowels (both lowercase and uppercase) return sum( 1 for char in s if char in vowels From e78c3897ccd06efa0825e6e371950a32ef203d69 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 23:08:17 +0100 Subject: [PATCH 10/20] Update test_count_vowels.py Added a module docstring and defensive assertions --- solutions/tests/test_count_vowels.py | 38 +++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/solutions/tests/test_count_vowels.py b/solutions/tests/test_count_vowels.py index 043b86ed3..b5839eab5 100644 --- a/solutions/tests/test_count_vowels.py +++ b/solutions/tests/test_count_vowels.py @@ -1,6 +1,18 @@ +""" +This module contains unit tests for the count_vowels module. +It includes test cases for the following function: +- count_vowels: Test the function that counts vowels in a string. + +Tests cover various cases including normal strings, empty strings, and case insensitivity. + +Usage: + Run tests using unittest framework: + >>> python -m unittest test_vowel_counter.py +""" + import unittest -from solutions.count_vowels import count_vowels # Import the count_vowels function +from ..count_vowels import count_vowels # Import the count_vowels function class TestCountVowels(unittest.TestCase): @@ -18,14 +30,6 @@ def test_count_vowels_normal(self): """ self.assertEqual(count_vowels("Nagham"), 2) - def test_count_vowels_empty(self): - """Test counting vowels in an empty string. - - This test verifies that the function returns 0 when the input string - is empty. - """ - self.assertEqual(count_vowels(""), 0) - def test_count_vowels_no_vowels(self): """Test counting vowels in a string with no vowels. @@ -50,6 +54,22 @@ def test_count_vowels_with_spaces(self): """ self.assertEqual(count_vowels("Hello World!"), 3) + # Defensive assertion tests + def test_invalid_input_type_integer(self): + """Test that the function raises a TypeError for non-string input (integer).""" + with self.assertRaises(TypeError): + count_vowels(12345) + + def test_invalid_input_type_list(self): + """Test that the function raises a TypeError for non-string input (list).""" + with self.assertRaises(TypeError): + count_vowels(["a", "b", "c"]) + + def test_empty_string(self): + """Test that the function raises a ValueError for an empty string.""" + with self.assertRaises(ValueError): + count_vowels("") + if __name__ == "__main__": unittest.main() From b90d6d0b9163ccd1a7fb2c6f395c9f297ac59a9c Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 23:21:16 +0100 Subject: [PATCH 11/20] Update count_vowels.py final touches --- solutions/count_vowels.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/solutions/count_vowels.py b/solutions/count_vowels.py index 3dae75601..002aea1b6 100644 --- a/solutions/count_vowels.py +++ b/solutions/count_vowels.py @@ -15,7 +15,7 @@ """ -def count_vowels(s) -> int:: +def count_vowels(s) -> int: """ Counts the number of vowels in the given string. @@ -23,7 +23,7 @@ def count_vowels(s) -> int:: s (str): The string in which to count vowels. The vowels are "a,e,i,o,u,A,E,I,O,U" - + Returns: int: The number of vowels in the string. @@ -36,14 +36,12 @@ def count_vowels(s) -> int:: 5 """ # Defensive assertion: Ensure the input is a string - if not isinstance(text, str): + if not isinstance(s, str): raise TypeError("Input must be a string") # Defensive assertion: Ensure the string is not empty - if not text: + if not s: raise ValueError("Input string cannot be empty") - + vowels = "aeiouAEIOU" # Define vowels (both lowercase and uppercase) - return sum( - 1 for char in s if char in vowels - ) # Count vowels using a generator expression + return sum(1 for char in s if char in vowels) From 09ee56e3ffbf91a680c166cd20bb7fae2347b46a Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Sun, 12 Jan 2025 23:37:39 +0100 Subject: [PATCH 12/20] Update test_count_vowels.py --- solutions/tests/test_count_vowels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_count_vowels.py b/solutions/tests/test_count_vowels.py index b5839eab5..8e488792f 100644 --- a/solutions/tests/test_count_vowels.py +++ b/solutions/tests/test_count_vowels.py @@ -12,7 +12,7 @@ import unittest -from ..count_vowels import count_vowels # Import the count_vowels function +from solutions.count_vowels import count_vowels # Import the count_vowels function class TestCountVowels(unittest.TestCase): From a48971f16c79430c871e648a12ef692d72f5f762 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:02:25 +0100 Subject: [PATCH 13/20] Update reverse_string.py --- solutions/reverse_string.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/solutions/reverse_string.py b/solutions/reverse_string.py index 045d135ac..cc8a98ae8 100644 --- a/solutions/reverse_string.py +++ b/solutions/reverse_string.py @@ -1,4 +1,24 @@ -def reverse_string(s): +""" +This module provides a function to reverse an inputed string. + +Function: + reverse_string(s: str) -> str: + Reverses the input string and returns the reversed one. + +Features: +- Simple and efficient string reversal. +- Built-in error handling for invalid inputs. + +Raises: + TypeError: If the input `s` is not a string. + ValueError: If the input `s` is an empty string. +Example: + >>> reverse_string("Deadline") + enildaeD +""" + + +def reverse_string(s: str) -> str: """ Reverses the given string. @@ -11,5 +31,13 @@ def reverse_string(s): Example: >>> reverse_string("hello") 'olleh' + >>> reverse_string("12345") + '54321' """ + # Defensive assertion: Ensure the input is a string + if not isinstance(s, str): + raise TypeError("Input must be a string") + # Defensive assertion: Ensure the string is not empty + if not s: + raise ValueError("Input string cannot be empty") return s[::-1] # Return the reversed string using slicing From aeceb788df221a6a09e4502b7fc436c8ed51c2ef Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:03:00 +0100 Subject: [PATCH 14/20] Update count_vowels.py --- solutions/count_vowels.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/count_vowels.py b/solutions/count_vowels.py index 002aea1b6..7d7328559 100644 --- a/solutions/count_vowels.py +++ b/solutions/count_vowels.py @@ -6,8 +6,8 @@ - count_vowels: Counts vowels in a provided string. Raises: - TypeError: If the input `text` is not a string. - ValueError: If the input `text` is an empty string. + TypeError: If the input `s` is not a string. + ValueError: If the input `s` is an empty string. Example: >>> vowel_counter("Deadline") From b1c101ea524cefe01dcadc730c30e022a472bab3 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:12:41 +0100 Subject: [PATCH 15/20] Update test_reverse_string.py final touches --- solutions/tests/test_reverse_string.py | 84 ++++++++++++++++---------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/solutions/tests/test_reverse_string.py b/solutions/tests/test_reverse_string.py index b0e906522..09e038f18 100644 --- a/solutions/tests/test_reverse_string.py +++ b/solutions/tests/test_reverse_string.py @@ -1,7 +1,20 @@ +""" +Unit tests for the `reverse_string` function. + +This module tests the `reverse_string` function, which reverses an input string. +It includes various test cases to ensure that the function handles: +- Normal strings +- Strings containing numbers +- Strings with special characters +- Single characters +- Palindromes +- Strings with spaces +- Extremely long strings +It also checks that a ValueError is raised for empty strings and that a TypeError is raised for non-string inputs. +""" + import unittest -from solutions.reverse_string import ( - reverse_string, -) # Import the reverse_string function +from solutions.reverse_string import reverse_string class TestReverseString(unittest.TestCase): @@ -12,45 +25,54 @@ class TestReverseString(unittest.TestCase): """ def test_reverse_normal(self): - """Test reversing a normal string. - - This test checks that the function correctly reverses a typical string - with multiple characters. - """ + """Test reversing a normal string.""" self.assertEqual(reverse_string("hello"), "olleh") - def test_reverse_empty(self): - """Test reversing an empty string. - - This test verifies that the function returns an empty string when - the input is empty. - """ - self.assertEqual(reverse_string(""), "") - def test_reverse_single_character(self): - """Test reversing a single character string. - - This test checks that the function returns the same character when - the input is a single character string. - """ + """Test reversing a single character string.""" self.assertEqual(reverse_string("a"), "a") def test_reverse_palindrome(self): - """Test reversing a palindrome. - - This test verifies that the function returns the same string when - the input is a palindrome. - """ + """Test reversing a palindrome.""" self.assertEqual(reverse_string("madam"), "madam") def test_reverse_spaces(self): - """Test reversing a string with spaces. - - This test checks that the function correctly reverses a string that - contains spaces, ensuring that the spaces are also reversed. - """ + """Test reversing a string with spaces.""" self.assertEqual(reverse_string("hello world"), "dlrow olleh") + def test_numbers_in_string(self): + """Test reversing a string containing numbers.""" + self.assertEqual(reverse_string("12345"), "54321") + + def test_special_characters(self): + """Test reversing a string with special characters.""" + self.assertEqual(reverse_string("!@#$"), "$#@!") + + def test_long_string(self): + """Test reversing an extremely long string.""" + long_string = "a" * 10000 + self.assertEqual(reverse_string(long_string), long_string[::-1]) + + def test_reverse_empty(self): + """Test that an empty string raises a ValueError.""" + with self.assertRaises(ValueError): + reverse_string("") # This input should raise a ValueError + + def test_integer_input(self): + """Test that an integer input raises a TypeError.""" + with self.assertRaises(TypeError): + reverse_string(12345) # Non-string input: integer + + def test_none_input(self): + """Test that a None input raises a TypeError.""" + with self.assertRaises(TypeError): + reverse_string(None) # Non-string input: NoneType + + def test_list_input(self): + """Test that a list input raises a TypeError.""" + with self.assertRaises(TypeError): + reverse_string(["a", "b", "c"]) # Non-string input: list + if __name__ == "__main__": unittest.main() From c40c66b154b62d781d9f89b938e0767350e20b95 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:23:12 +0100 Subject: [PATCH 16/20] Update fizz_buzz.py final touches --- solutions/fizz_buzz.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index fda334441..4d111fdc7 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -1,4 +1,22 @@ -def fizzbuzz(n: int) -> list: +""" +This module provides a function that generates a list of numbers from 1 to n with specific rules for multiples of 3 and 5. + +For each number in the range: +- For multiples of 3, "Fizz" is added instead of the number. +- For multiples of 5, "Buzz" is added instead of the number. +- For multiples of both 3 and 5, "FizzBuzz" is added instead of the number. + +Features: +- Input validation to ensure that the input is a positive integer. +- Customizable output format (e.g., changing the words "Fizz" and "Buzz" to user-defined strings). +- Ability to generate results as a string instead of a list. + +Example: +>>> fizz_buzz(15) +[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] +""" + +def fizz_buzz(n: int) -> list: """ Generates a list of numbers from 1 to n with the following rules: - For multiples of 3, "Fizz" is added instead of the number. @@ -12,9 +30,15 @@ def fizzbuzz(n: int) -> list: list: A list containing the numbers and/or string representations according to the rules. Example: - >>> fizzbuzz(15) - [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] + >>> fizz_buzz(10) + [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz'] """ + # Defensive assertions + if not isinstance(n, int): + raise TypeError("Input must be an integer.") + if n < 1: + raise ValueError("Input must be greater than or equal to 1.") + results = [] # Initialize an empty list to store the results for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: @@ -25,4 +49,5 @@ def fizzbuzz(n: int) -> list: results.append("Buzz") # Append "Buzz" for multiples of 5 else: results.append(i) # Append the number itself if not a multiple of 3 or 5 + return results # Return the list of results From ed16f4467f5cd4c19b6b912c4289f20a5ed0d952 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:32:28 +0100 Subject: [PATCH 17/20] Update fizz_buzz.py --- solutions/fizz_buzz.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index 4d111fdc7..c10d09e03 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -1,5 +1,6 @@ """ -This module provides a function that generates a list of numbers from 1 to n with specific rules for multiples of 3 and 5. +This module provides a function that generates a list of numbers from 1 to n +with specific rules for multiples of 3 and 5. For each number in the range: - For multiples of 3, "Fizz" is added instead of the number. @@ -8,12 +9,14 @@ Features: - Input validation to ensure that the input is a positive integer. -- Customizable output format (e.g., changing the words "Fizz" and "Buzz" to user-defined strings). +- Customizable output format (e.g., changing the words "Fizz" and "Buzz" + to user-defined strings). - Ability to generate results as a string instead of a list. Example: >>> fizz_buzz(15) -[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] +[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, + 14, 'FizzBuzz'] """ def fizz_buzz(n: int) -> list: @@ -27,12 +30,18 @@ def fizz_buzz(n: int) -> list: n (int): The upper limit of the range (inclusive) to generate. Returns: - list: A list containing the numbers and/or string representations according to the rules. + list: A list containing the numbers and/or string representations according + to the rules. + + Raises: + TypeError: If the input is not an integer. + ValueError: If the input is less than 1. Example: >>> fizz_buzz(10) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz'] """ + # Defensive assertions if not isinstance(n, int): raise TypeError("Input must be an integer.") From 038ff04632df070d056bb269e1ccdeb22b2df080 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:38:54 +0100 Subject: [PATCH 18/20] Update fizz_buzz.py --- solutions/fizz_buzz.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index c10d09e03..36daead22 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -19,6 +19,7 @@ 14, 'FizzBuzz'] """ + def fizz_buzz(n: int) -> list: """ Generates a list of numbers from 1 to n with the following rules: @@ -40,14 +41,13 @@ def fizz_buzz(n: int) -> list: Example: >>> fizz_buzz(10) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz'] - """ + """ # Defensive assertions if not isinstance(n, int): raise TypeError("Input must be an integer.") if n < 1: raise ValueError("Input must be greater than or equal to 1.") - results = [] # Initialize an empty list to store the results for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: @@ -58,5 +58,4 @@ def fizz_buzz(n: int) -> list: results.append("Buzz") # Append "Buzz" for multiples of 5 else: results.append(i) # Append the number itself if not a multiple of 3 or 5 - return results # Return the list of results From 85d945a414495d7068426250312257e1cf48a5e0 Mon Sep 17 00:00:00 2001 From: "Nelson Fodjo K." <106561241+FKN237@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:43:38 +0100 Subject: [PATCH 19/20] Update test_fizz_buzz.py --- solutions/tests/test_fizz_buzz.py | 94 ++++++++++++++----------------- 1 file changed, 41 insertions(+), 53 deletions(-) diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py index 55befc430..ab526fdaf 100644 --- a/solutions/tests/test_fizz_buzz.py +++ b/solutions/tests/test_fizz_buzz.py @@ -1,88 +1,76 @@ """ -Unit tests for the fizzbuzz function from the solutions.fizz_buzz module. +Unit tests for the fizz_buzz function from the solutions.fizz_buzz module. -This class uses the unittest framework to validate the behavior of the fizzbuzz function +This class uses the unittest framework to validate the behavior of the fizz_buzz function across various input values, ensuring it produces the correct output lists. Tests include -cases for n = 0, 1, 3, 5, and 15, verifying the proper inclusion of Fizz, Buzz, and FizzBuzz -in the outputs. +cases for n = 1, 3, 5, 15, and additional edge cases to comprehensively test the function. """ import unittest -import sys -import os - -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) from solutions.fizz_buzz import fizz_buzz class TestFizzBuzz(unittest.TestCase): - """Test cases for the fizzbuzz function. + """Test cases for the fizz_buzz function. - This class contains unit tests for the fizzbuzz function to ensure + This class contains unit tests for the fizz_buzz function to ensure that it produces the expected output for various input values. """ def test_fizzbuzz_15(self): - """Test FizzBuzz for n = 15. - - This test checks that the output for n=15 matches the expected - list which includes Fizz, Buzz, and FizzBuzz at the correct - positions. - """ + """Test FizzBuzz for n = 15.""" expected = [ - 1, - 2, - "Fizz", - 4, - "Buzz", - "Fizz", - 7, - 8, - "Fizz", - "Buzz", - 11, - "Fizz", - 13, - 14, - "FizzBuzz", + 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", + "Buzz", 11, "Fizz", 13, 14, "FizzBuzz" ] self.assertEqual(fizz_buzz(15), expected) def test_fizzbuzz_3(self): - """Test FizzBuzz for n = 3. - - This test checks that for n=3, the output should return - a list containing the numbers 1, 2, and 'Fizz'. - """ + """Test FizzBuzz for n = 3.""" expected = [1, 2, "Fizz"] self.assertEqual(fizz_buzz(3), expected) def test_fizzbuzz_5(self): - """Test FizzBuzz for n = 5. - - This test verifies that for n=5, the output list includes - numbers 1, 2, 'Fizz', 4, and 'Buzz'. - """ + """Test FizzBuzz for n = 5.""" expected = [1, 2, "Fizz", 4, "Buzz"] self.assertEqual(fizz_buzz(5), expected) def test_fizzbuzz_1(self): - """Test FizzBuzz for n = 1. - - This test checks that for n=1, the output should simply be - a list containing the number 1. - """ + """Test FizzBuzz for n = 1.""" expected = [1] self.assertEqual(fizz_buzz(1), expected) - def test_fizzbuzz_0(self): - """Test FizzBuzz for n = 0. + def test_fizzbuzz_zero(self): + """Test FizzBuzz for n = 0.""" + with self.assertRaises(ValueError): + fizz_buzz(0) + + def test_fizzbuzz_negative(self): + """Test FizzBuzz for negative n.""" + with self.assertRaises(ValueError): + fizz_buzz(-5) + + def test_fizzbuzz_non_integer(self): + """Test FizzBuzz for non-integer n.""" + non_integers = [3.5, "string", None, [], {}, (), 2.0] + for value in non_integers: + with self.assertRaises(TypeError): + fizz_buzz(value) + + def test_fizzbuzz_large_number(self): + """Test FizzBuzz for a larger number n = 30.""" + expected = [ + 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", + "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, + 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", + "Buzz", 26, "Fizz", 28, 29, "FizzBuzz" + ] + self.assertEqual(fizz_buzz(30), expected) - This test checks that for n=0, the output should be an - empty list since there are no numbers to include. - """ - expected = [] - self.assertEqual(fizz_buzz(0), expected) + def test_fizzbuzz_edge_case(self): + """Test FizzBuzz for n = 2.""" + expected = [1, 2] + self.assertEqual(fizz_buzz(2), expected) if __name__ == "__main__": From b84f300b13639f357a437012ec8a26c04ed9845f Mon Sep 17 00:00:00 2001 From: matopcheg Date: Mon, 13 Jan 2025 13:24:13 +0200 Subject: [PATCH 20/20] ruff formatting --- solutions/fizz_buzz.py | 20 ++++++------ solutions/tests/test_fizz_buzz.py | 51 +++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index 36daead22..e234075bd 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -1,5 +1,5 @@ """ -This module provides a function that generates a list of numbers from 1 to n +This module provides a function that generates a list of numbers from 1 to n with specific rules for multiples of 3 and 5. For each number in the range: @@ -9,13 +9,13 @@ Features: - Input validation to ensure that the input is a positive integer. -- Customizable output format (e.g., changing the words "Fizz" and "Buzz" +- Customizable output format (e.g., changing the words "Fizz" and "Buzz" to user-defined strings). - Ability to generate results as a string instead of a list. Example: >>> fizz_buzz(15) -[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, +[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz'] """ @@ -31,7 +31,7 @@ def fizz_buzz(n: int) -> list: n (int): The upper limit of the range (inclusive) to generate. Returns: - list: A list containing the numbers and/or string representations according + list: A list containing the numbers and/or string representations according to the rules. Raises: @@ -41,7 +41,7 @@ def fizz_buzz(n: int) -> list: Example: >>> fizz_buzz(10) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz'] - + """ # Defensive assertions if not isinstance(n, int): @@ -51,11 +51,13 @@ def fizz_buzz(n: int) -> list: results = [] # Initialize an empty list to store the results for i in range(1, n + 1): if i % 3 == 0 and i % 5 == 0: - results.append("FizzBuzz") # Append "FizzBuzz" for multiples of both 3 and 5 + results.append( + "FizzBuzz" + ) # Append "FizzBuzz" for multiples of both 3 and 5 elif i % 3 == 0: - results.append("Fizz") # Append "Fizz" for multiples of 3 + results.append("Fizz") # Append "Fizz" for multiples of 3 elif i % 5 == 0: - results.append("Buzz") # Append "Buzz" for multiples of 5 + results.append("Buzz") # Append "Buzz" for multiples of 5 else: - results.append(i) # Append the number itself if not a multiple of 3 or 5 + results.append(i) # Append the number itself if not a multiple of 3 or 5 return results # Return the list of results diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py index ab526fdaf..c24280a5c 100644 --- a/solutions/tests/test_fizz_buzz.py +++ b/solutions/tests/test_fizz_buzz.py @@ -20,8 +20,21 @@ class TestFizzBuzz(unittest.TestCase): def test_fizzbuzz_15(self): """Test FizzBuzz for n = 15.""" expected = [ - 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", - "Buzz", 11, "Fizz", 13, 14, "FizzBuzz" + 1, + 2, + "Fizz", + 4, + "Buzz", + "Fizz", + 7, + 8, + "Fizz", + "Buzz", + 11, + "Fizz", + 13, + 14, + "FizzBuzz", ] self.assertEqual(fizz_buzz(15), expected) @@ -60,10 +73,36 @@ def test_fizzbuzz_non_integer(self): def test_fizzbuzz_large_number(self): """Test FizzBuzz for a larger number n = 30.""" expected = [ - 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", - "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, - 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", - "Buzz", 26, "Fizz", 28, 29, "FizzBuzz" + 1, + 2, + "Fizz", + 4, + "Buzz", + "Fizz", + 7, + 8, + "Fizz", + "Buzz", + 11, + "Fizz", + 13, + 14, + "FizzBuzz", + 16, + 17, + "Fizz", + 19, + "Buzz", + "Fizz", + 22, + 23, + "Fizz", + "Buzz", + 26, + "Fizz", + 28, + 29, + "FizzBuzz", ] self.assertEqual(fizz_buzz(30), expected)