From 33c8afc2d2b4908a41b3b2e9b67ff11da633eeeb Mon Sep 17 00:00:00 2001 From: Sandy <7pandeys@gmail.com> Date: Thu, 7 Nov 2024 01:43:07 +0530 Subject: [PATCH] Add factorial --- Basic/Basic_Of_Python/src/factorial.py | 43 ++++++++++++++++++++++++++ Basic/Basic_Of_Python/src/fibonaci.py | 9 +++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Basic/Basic_Of_Python/src/factorial.py b/Basic/Basic_Of_Python/src/factorial.py index e69de29..33f4e03 100644 --- a/Basic/Basic_Of_Python/src/factorial.py +++ b/Basic/Basic_Of_Python/src/factorial.py @@ -0,0 +1,43 @@ +from typing import Union, Any +import logging + +# Configuring logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') +logger = logging.getLogger(__name__) + + +def generate_factorial_sequence(value: int) -> dict[int, Union[int, Any]]: + """ + Generate a factorial sequence list up to a specified number of elements + + Args: + value: The number of factorial numbers to generate. + + Returns: + List[int]: A list of Factorial numbers up to the specified limit. + """ + logger.info("Generating Factorial sequence for limit: %d", value) + sequence = {} + next_value = 1 + if value == 0: + sequence[0] = 1 + elif value < 0: + logger.error("The limit must be at least greater than 0 to generate a valid Factorial sequence.") + # raise ValueError("The limit must be at least greater than 0 to generate a valid Factorial sequence.") + else: + for i in range(1, value+1): + next_value = next_value*i + sequence[i] = next_value + + logger.info("Factorial sequence generated: %s", sequence) + return sequence + + +if __name__ == "__main__": + + try: + generate_factorial_sequence(5) # [1, 2, 6, 24, 120] + generate_factorial_sequence(-1) + generate_factorial_sequence(0) + except ValueError as e: + logger.exception("Exception occurred: %s", e) diff --git a/Basic/Basic_Of_Python/src/fibonaci.py b/Basic/Basic_Of_Python/src/fibonaci.py index 2041353..394a854 100644 --- a/Basic/Basic_Of_Python/src/fibonaci.py +++ b/Basic/Basic_Of_Python/src/fibonaci.py @@ -18,14 +18,13 @@ def generate_fibonacci_sequence(limit: int) -> List[int]: """ if limit < 2: logger.error("The limit must be at least 2 to generate a valid Fibonacci sequence.") - raise ValueError("The limit must be at least 2 to generate a valid Fibonacci sequence.") + # raise ValueError("The limit must be at least 2 to generate a valid Fibonacci sequence.") logger.info("Generating Fibonacci sequence for limit: %d", limit) # Initialize the first two Fibonacci numbers first, second = 0, 1 sequence = [first, second] - # Generate the Fibonacci sequence up to the specified limit for _ in range(2, limit): next_value = first + second @@ -33,14 +32,14 @@ def generate_fibonacci_sequence(limit: int) -> List[int]: logger.debug("Generated next Fibonacci number: %d", next_value) first, second = second, next_value - logger.info("Fibonacci sequence generated: %s", sequence) return sequence if __name__ == "__main__": + try: - print(generate_fibonacci_sequence(1)) # Output: [0, 1, 1, 2, 3] + generate_fibonacci_sequence(2) # Output: [0, 1, 1, 2, 3] + generate_fibonacci_sequence(-2) except ValueError as e: logger.exception("Exception occurred: %s", e) -