Skip to content

Commit

Permalink
Add factorial
Browse files Browse the repository at this point in the history
  • Loading branch information
7pandeys committed Nov 6, 2024
1 parent aa7b72f commit 33c8afc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
43 changes: 43 additions & 0 deletions Basic/Basic_Of_Python/src/factorial.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 4 additions & 5 deletions Basic/Basic_Of_Python/src/fibonaci.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@ 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
sequence.append(next_value)
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)

0 comments on commit 33c8afc

Please sign in to comment.