From 5130f3a6c01c595f1a542a984f8235a06451429a Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Mon, 7 Jul 2025 14:43:25 +0530 Subject: [PATCH 1/2] Add simple recursion examples: factorial, fibonacci, sum_of_digits --- other/simple_recursion.py | 100 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 other/simple_recursion.py diff --git a/other/simple_recursion.py b/other/simple_recursion.py new file mode 100644 index 000000000000..0af35816859f --- /dev/null +++ b/other/simple_recursion.py @@ -0,0 +1,100 @@ +""" +Simple Recursion Examples. + +This module contains a few basic examples of recursion for beginners. + +Functions: +- factorial(n): Calculate factorial of n. +- fibonacci(n): Calculate nth Fibonacci number. +- sum_of_digits(n): Calculate sum of digits of n. + +Each function has doctests to verify correct behavior. +""" + +def factorial(n: int) -> int: + """ + Calculate the factorial of a non-negative integer n using recursion. + + Args: + n (int): Non-negative integer. + + Returns: + int: Factorial of n. + + Raises: + ValueError: If n is negative. + + Examples: + >>> factorial(5) + 120 + >>> factorial(0) + 1 + >>> factorial(1) + 1 + """ + if n < 0: + raise ValueError("Input must be a non-negative integer") + if n == 0: + return 1 + return n * factorial(n - 1) + + +def fibonacci(n: int) -> int: + """ + Calculate the nth Fibonacci number using recursion. + + Args: + n (int): Non-negative integer index. + + Returns: + int: nth Fibonacci number. + + Raises: + ValueError: If n is negative. + + Examples: + >>> fibonacci(0) + 0 + >>> fibonacci(1) + 1 + >>> fibonacci(7) + 13 + """ + if n < 0: + raise ValueError("Input must be a non-negative integer") + if n <= 1: + return n + return fibonacci(n - 1) + fibonacci(n - 2) + + +def sum_of_digits(n: int) -> int: + """ + Calculate the sum of digits of a non-negative integer n using recursion. + + Args: + n (int): Non-negative integer. + + Returns: + int: Sum of digits of n. + + Raises: + ValueError: If n is negative. + + Examples: + >>> sum_of_digits(123) + 6 + >>> sum_of_digits(0) + 0 + >>> sum_of_digits(999) + 27 + """ + if n < 0: + raise ValueError("Input must be a non-negative integer") + if n == 0: + return 0 + return n % 10 + sum_of_digits(n // 10) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From d08a47d3c9ec8e662705308ac40657041ac88f86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Jul 2025 09:24:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/simple_recursion.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/other/simple_recursion.py b/other/simple_recursion.py index 0af35816859f..7f964dcfe041 100644 --- a/other/simple_recursion.py +++ b/other/simple_recursion.py @@ -11,6 +11,7 @@ Each function has doctests to verify correct behavior. """ + def factorial(n: int) -> int: """ Calculate the factorial of a non-negative integer n using recursion. @@ -97,4 +98,5 @@ def sum_of_digits(n: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod()