From 036660a187b7811db0f0747ce25fc67b45bb562e Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 06:56:44 +0530 Subject: [PATCH 01/13] Add FizzBuzz logic building problem Implement FizzBuzz function with input validation and examples. --- logic_building_problems/fizz_buzz.py | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 logic_building_problems/fizz_buzz.py diff --git a/logic_building_problems/fizz_buzz.py b/logic_building_problems/fizz_buzz.py new file mode 100644 index 000000000000..89ce8be72a0d --- /dev/null +++ b/logic_building_problems/fizz_buzz.py @@ -0,0 +1,61 @@ +"""FizzBuzz Problem. + +This module contains a solution to the classic FizzBuzz problem. +For numbers 1 to n, print 'Fizz' for multiples of 3, 'Buzz' for multiples of 5, +and 'FizzBuzz' for multiples of both. +""" + + +def fizz_buzz(n: int) -> list[str]: + """ + Return a list of FizzBuzz results for numbers from 1 to n. + + For each number from 1 to n: + - Return 'FizzBuzz' if divisible by both 3 and 5 + - Return 'Fizz' if divisible by 3 + - Return 'Buzz' if divisible by 5 + - Return the number as a string otherwise + + Args: + n: Positive integer representing the range + + Returns: + A list of strings containing FizzBuzz results + + Raises: + ValueError: If n is not a positive integer + + Examples: + >>> fizz_buzz(5) + ['1', '2', 'Fizz', '4', 'Buzz'] + >>> fizz_buzz(15) + ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz'] + >>> fizz_buzz(0) + Traceback (most recent call last): + ... + ValueError: n must be a positive integer + >>> fizz_buzz(-5) + Traceback (most recent call last): + ... + ValueError: n must be a positive integer + """ + if not isinstance(n, int) or n <= 0: + raise ValueError("n must be a positive integer") + + result = [] + for i in range(1, n + 1): + if i % 15 == 0: + result.append("FizzBuzz") + elif i % 3 == 0: + result.append("Fizz") + elif i % 5 == 0: + result.append("Buzz") + else: + result.append(str(i)) + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 074a3f300c0453370eabd6de491b44123c73f983 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:04:53 +0530 Subject: [PATCH 02/13] Add palindrome checker logic building problem This module provides a function to check if a string is a palindrome, including examples and doctest integration. --- logic_building_problems/palindrome.py | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 logic_building_problems/palindrome.py diff --git a/logic_building_problems/palindrome.py b/logic_building_problems/palindrome.py new file mode 100644 index 000000000000..9fb5b6375ff3 --- /dev/null +++ b/logic_building_problems/palindrome.py @@ -0,0 +1,46 @@ +"""Palindrome Checker. + +This module contains functions to check if a string is a palindrome. +A palindrome is a word, phrase, or sequence that reads the same backward as forward. +""" + + +def is_palindrome(text: str) -> bool: + """ + Check if a given string is a palindrome. + + A palindrome is a string that reads the same forward and backward, + ignoring case, spaces, and punctuation. + + Args: + text: The string to check + + Returns: + True if the string is a palindrome, False otherwise + + Examples: + >>> is_palindrome("racecar") + True + >>> is_palindrome("hello") + False + >>> is_palindrome("A man a plan a canal Panama") + True + >>> is_palindrome("race a car") + False + >>> is_palindrome("") + True + >>> is_palindrome("a") + True + >>> is_palindrome("Madam") + True + """ + # Remove non-alphanumeric characters and convert to lowercase + cleaned = "".join(char.lower() for char in text if char.isalnum()) + # Check if the cleaned string equals its reverse + return cleaned == cleaned[::-1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 49c997593503cdbeaccbcfb8c5936bef8497aa53 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:06:01 +0530 Subject: [PATCH 03/13] Add pangram checker logic building problem This module includes a function to check if a string is a pangram, with examples provided in the docstring. --- logic_building_problems/pangram.py | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 logic_building_problems/pangram.py diff --git a/logic_building_problems/pangram.py b/logic_building_problems/pangram.py new file mode 100644 index 000000000000..50a3c651e733 --- /dev/null +++ b/logic_building_problems/pangram.py @@ -0,0 +1,46 @@ +"""Pangram Checker. + +This module contains functions to check if a string is a pangram. +A pangram is a sentence that contains every letter of the alphabet at least once. +""" + +import string + + +def is_pangram(text: str) -> bool: + """ + Check if a given string is a pangram. + + A pangram is a string that contains every letter of the alphabet + at least once, ignoring case and non-alphabetic characters. + + Args: + text: The string to check + + Returns: + True if the string is a pangram, False otherwise + + Examples: + >>> is_pangram("The quick brown fox jumps over the lazy dog") + True + >>> is_pangram("Hello World") + False + >>> is_pangram("Pack my box with five dozen liquor jugs") + True + >>> is_pangram("abcdefghijklmnopqrstuvwxyz") + True + >>> is_pangram("") + False + >>> is_pangram("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + True + """ + # Convert text to lowercase and get all unique letters + letters = set(char.lower() for char in text if char.isalpha()) + # Check if all 26 letters of the alphabet are present + return len(letters) == 26 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 66f8c2284b60f6c3c2b83363a5aed37fec1f983a Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:07:17 +0530 Subject: [PATCH 04/13] Add array traversal logic building problem This module includes functions for finding the maximum, minimum, and sum of elements in an array, with appropriate error handling and examples. --- logic_building_problems/array_traversal.py | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 logic_building_problems/array_traversal.py diff --git a/logic_building_problems/array_traversal.py b/logic_building_problems/array_traversal.py new file mode 100644 index 000000000000..8a57dd8ab917 --- /dev/null +++ b/logic_building_problems/array_traversal.py @@ -0,0 +1,96 @@ +"""Array Traversal. + +This module contains functions for common array traversal operations, +including finding maximum, minimum, and sum of elements. +""" + + +def find_max(arr: list[int | float]) -> int | float: + """ + Find the maximum element in an array. + + Args: + arr: List of numbers + + Returns: + The maximum value in the array + + Raises: + ValueError: If the array is empty + + Examples: + >>> find_max([1, 5, 3, 9, 2]) + 9 + >>> find_max([-1, -5, -3]) + -1 + >>> find_max([42]) + 42 + >>> find_max([1.5, 2.7, 1.2]) + 2.7 + >>> find_max([]) + Traceback (most recent call last): + ... + ValueError: Array cannot be empty + """ + if not arr: + raise ValueError("Array cannot be empty") + return max(arr) + + +def find_min(arr: list[int | float]) -> int | float: + """ + Find the minimum element in an array. + + Args: + arr: List of numbers + + Returns: + The minimum value in the array + + Raises: + ValueError: If the array is empty + + Examples: + >>> find_min([1, 5, 3, 9, 2]) + 1 + >>> find_min([-1, -5, -3]) + -5 + >>> find_min([42]) + 42 + >>> find_min([]) + Traceback (most recent call last): + ... + ValueError: Array cannot be empty + """ + if not arr: + raise ValueError("Array cannot be empty") + return min(arr) + + +def array_sum(arr: list[int | float]) -> int | float: + """ + Calculate the sum of all elements in an array. + + Args: + arr: List of numbers + + Returns: + The sum of all values in the array + + Examples: + >>> array_sum([1, 2, 3, 4, 5]) + 15 + >>> array_sum([-1, 1, -2, 2]) + 0 + >>> array_sum([1.5, 2.5, 3.0]) + 7.0 + >>> array_sum([]) + 0 + """ + return sum(arr) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From b9a867edc42ef9aaa4d0c9bc8cb2ce9fde8e2127 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:10:59 +0530 Subject: [PATCH 05/13] Implement bubble sort algorithm in bubble_sort.py This module implements the bubble sort algorithm, which sorts a list by repeatedly swapping adjacent elements that are in the wrong order. It includes a function with examples demonstrating its usage. --- logic_building_problems/bubble_sort.py | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 logic_building_problems/bubble_sort.py diff --git a/logic_building_problems/bubble_sort.py b/logic_building_problems/bubble_sort.py new file mode 100644 index 000000000000..637604ce3a55 --- /dev/null +++ b/logic_building_problems/bubble_sort.py @@ -0,0 +1,64 @@ +"""Bubble Sort Algorithm. + +This module implements the bubble sort algorithm, a simple sorting technique +that repeatedly steps through the list, compares adjacent elements and swaps +them if they are in the wrong order. +""" + + +def bubble_sort(arr: list[int | float]) -> list[int | float]: + """ + Sort an array using the bubble sort algorithm. + + Bubble sort works by repeatedly swapping adjacent elements if they are + in the wrong order. This process continues until no more swaps are needed. + + Args: + arr: List of numbers to be sorted + + Returns: + The sorted list in ascending order + + Examples: + >>> bubble_sort([64, 34, 25, 12, 22, 11, 90]) + [11, 12, 22, 25, 34, 64, 90] + >>> bubble_sort([5, 2, 8, 1, 9]) + [1, 2, 5, 8, 9] + >>> bubble_sort([1]) + [1] + >>> bubble_sort([]) + [] + >>> bubble_sort([3, 3, 3, 3]) + [3, 3, 3, 3] + >>> bubble_sort([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] + >>> bubble_sort([1.5, 2.3, 0.8, 1.1]) + [0.8, 1.1, 1.5, 2.3] + """ + # Create a copy to avoid modifying the original list + arr_copy = arr.copy() + n = len(arr_copy) + + # Traverse through all array elements + for i in range(n): + # Flag to optimize by detecting if array is already sorted + swapped = False + + # Last i elements are already in place + for j in range(0, n - i - 1): + # Swap if the element found is greater than the next element + if arr_copy[j] > arr_copy[j + 1]: + arr_copy[j], arr_copy[j + 1] = arr_copy[j + 1], arr_copy[j] + swapped = True + + # If no swapping occurred, array is sorted + if not swapped: + break + + return arr_copy + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 54bbe1d1f7a89a95b26a048bd1f4023910c850cd Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:29:07 +0530 Subject: [PATCH 06/13] Create __init__.py --- logic_building_problems/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 logic_building_problems/__init__.py diff --git a/logic_building_problems/__init__.py b/logic_building_problems/__init__.py new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/logic_building_problems/__init__.py @@ -0,0 +1 @@ + From d5e74ce929389f3394d35c2cb794ed4aeebcb9b4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 01:59:36 +0000 Subject: [PATCH 07/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- logic_building_problems/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/logic_building_problems/__init__.py b/logic_building_problems/__init__.py index 8b137891791f..e69de29bb2d1 100644 --- a/logic_building_problems/__init__.py +++ b/logic_building_problems/__init__.py @@ -1 +0,0 @@ - From 8a8757c8a1d1b50e5124c93303977554b240ca7a Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:30:32 +0530 Subject: [PATCH 08/13] Fix PIE808: Remove unnecessary start argument in range() for bubble_sort.py Removed unnecessary range start in bubble sort loop. --- logic_building_problems/bubble_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logic_building_problems/bubble_sort.py b/logic_building_problems/bubble_sort.py index 637604ce3a55..1f2385e0788c 100644 --- a/logic_building_problems/bubble_sort.py +++ b/logic_building_problems/bubble_sort.py @@ -45,7 +45,7 @@ def bubble_sort(arr: list[int | float]) -> list[int | float]: swapped = False # Last i elements are already in place - for j in range(0, n - i - 1): + for j in range(n - i - 1): # Swap if the element found is greater than the next element if arr_copy[j] > arr_copy[j + 1]: arr_copy[j], arr_copy[j + 1] = arr_copy[j + 1], arr_copy[j] From 33303aa745fc34f171fa4afac598cae92558f8a1 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:32:05 +0530 Subject: [PATCH 09/13] Fix E501: Fix line length issue in fizz_buzz.py Updated docstring formatting for clarity. --- logic_building_problems/fizz_buzz.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/logic_building_problems/fizz_buzz.py b/logic_building_problems/fizz_buzz.py index 89ce8be72a0d..7815ef9487a9 100644 --- a/logic_building_problems/fizz_buzz.py +++ b/logic_building_problems/fizz_buzz.py @@ -1,8 +1,8 @@ """FizzBuzz Problem. This module contains a solution to the classic FizzBuzz problem. -For numbers 1 to n, print 'Fizz' for multiples of 3, 'Buzz' for multiples of 5, -and 'FizzBuzz' for multiples of both. +For numbers 1 to n, print 'Fizz' for multiples of 3, 'Buzz' for +multiples of 5, and 'FizzBuzz' for multiples of both. """ @@ -52,6 +52,7 @@ def fizz_buzz(n: int) -> list[str]: result.append("Buzz") else: result.append(str(i)) + return result From 9088eb4a3d719364508769dfe972c0b51a34316b Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:34:30 +0530 Subject: [PATCH 10/13] Fix F401 and C401: Remove unused import and use set comprehension in pangram.py --- logic_building_problems/pangram.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/logic_building_problems/pangram.py b/logic_building_problems/pangram.py index 50a3c651e733..cf10b4c955d4 100644 --- a/logic_building_problems/pangram.py +++ b/logic_building_problems/pangram.py @@ -4,8 +4,6 @@ A pangram is a sentence that contains every letter of the alphabet at least once. """ -import string - def is_pangram(text: str) -> bool: """ @@ -35,7 +33,8 @@ def is_pangram(text: str) -> bool: True """ # Convert text to lowercase and get all unique letters - letters = set(char.lower() for char in text if char.isalpha()) + letters = {char.lower() for char in text if char.isalpha()} + # Check if all 26 letters of the alphabet are present return len(letters) == 26 From 26b083a7b364817a5a5e0c4c200f1a4ddd837b19 Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:36:43 +0530 Subject: [PATCH 11/13] Fix E501: Split long doctest line in fizz_buzz.py Updated doctest for fizz_buzz function to normalize whitespace in output. --- logic_building_problems/fizz_buzz.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/logic_building_problems/fizz_buzz.py b/logic_building_problems/fizz_buzz.py index 7815ef9487a9..60b559803b61 100644 --- a/logic_building_problems/fizz_buzz.py +++ b/logic_building_problems/fizz_buzz.py @@ -28,8 +28,9 @@ def fizz_buzz(n: int) -> list[str]: Examples: >>> fizz_buzz(5) ['1', '2', 'Fizz', '4', 'Buzz'] - >>> fizz_buzz(15) - ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz'] + >>> fizz_buzz(15) # doctest: +NORMALIZE_WHITESPACE + ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', + 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz'] >>> fizz_buzz(0) Traceback (most recent call last): ... From 9d95fc820b359f433a26317fd3e504c53433bd6f Mon Sep 17 00:00:00 2001 From: Tejasrahane <161036451+Tejasrahane@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:39:58 +0530 Subject: [PATCH 12/13] Rename parameter 'n' to 'num' in fizz_buzz function for better readability --- logic_building_problems/fizz_buzz.py | 36 ++++++++++------------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/logic_building_problems/fizz_buzz.py b/logic_building_problems/fizz_buzz.py index 60b559803b61..143c61a447f9 100644 --- a/logic_building_problems/fizz_buzz.py +++ b/logic_building_problems/fizz_buzz.py @@ -1,29 +1,19 @@ -"""FizzBuzz Problem. +def fizz_buzz(num: int) -> list[str]: + """FizzBuzz Problem. -This module contains a solution to the classic FizzBuzz problem. -For numbers 1 to n, print 'Fizz' for multiples of 3, 'Buzz' for -multiples of 5, and 'FizzBuzz' for multiples of both. -""" - - -def fizz_buzz(n: int) -> list[str]: - """ - Return a list of FizzBuzz results for numbers from 1 to n. - - For each number from 1 to n: - - Return 'FizzBuzz' if divisible by both 3 and 5 - - Return 'Fizz' if divisible by 3 - - Return 'Buzz' if divisible by 5 + - Return "Fizz" if the number is divisible by 3 + - Return "Buzz" if the number is divisible by 5 + - Return "FizzBuzz" if the number is divisible by both 3 and 5 - Return the number as a string otherwise Args: - n: Positive integer representing the range + num: Positive integer representing the range Returns: A list of strings containing FizzBuzz results Raises: - ValueError: If n is not a positive integer + ValueError: If num is not a positive integer Examples: >>> fizz_buzz(5) @@ -34,17 +24,18 @@ def fizz_buzz(n: int) -> list[str]: >>> fizz_buzz(0) Traceback (most recent call last): ... - ValueError: n must be a positive integer + ValueError: num must be a positive integer >>> fizz_buzz(-5) Traceback (most recent call last): ... - ValueError: n must be a positive integer + ValueError: num must be a positive integer """ - if not isinstance(n, int) or n <= 0: - raise ValueError("n must be a positive integer") + + if not isinstance(num, int) or num <= 0: + raise ValueError("num must be a positive integer") result = [] - for i in range(1, n + 1): + for i in range(1, num + 1): if i % 15 == 0: result.append("FizzBuzz") elif i % 3 == 0: @@ -59,5 +50,4 @@ def fizz_buzz(n: int) -> list[str]: if __name__ == "__main__": import doctest - doctest.testmod() From 252a65492c7119d1895f18037b696b4ebccf45a2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 02:10:55 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- logic_building_problems/fizz_buzz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/logic_building_problems/fizz_buzz.py b/logic_building_problems/fizz_buzz.py index 143c61a447f9..114f62e28d4a 100644 --- a/logic_building_problems/fizz_buzz.py +++ b/logic_building_problems/fizz_buzz.py @@ -50,4 +50,5 @@ def fizz_buzz(num: int) -> list[str]: if __name__ == "__main__": import doctest + doctest.testmod()