| | import math |
| | import random |
| | from typing import List, Tuple |
| |
|
| | def calculate_factorial(n: int) -> int: |
| | """Calculate factorial of a number recursively.""" |
| | if n <= 1: |
| | return 1 |
| | return n * calculate_factorial(n - 1) |
| |
|
| | def fibonacci_sequence(n: int) -> List[int]: |
| | """Generate fibonacci sequence up to n terms.""" |
| | if n <= 0: |
| | return [] |
| | elif n == 1: |
| | return [0] |
| | elif n == 2: |
| | return [0, 1] |
| | |
| | sequence = [0, 1] |
| | for i in range(2, n): |
| | sequence.append(sequence[i-1] + sequence[i-2]) |
| | return sequence |
| |
|
| | def is_prime(num: int) -> bool: |
| | """Check if a number is prime.""" |
| | if num < 2: |
| | return False |
| | if num == 2: |
| | return True |
| | if num % 2 == 0: |
| | return False |
| | |
| | for i in range(3, int(math.sqrt(num)) + 1, 2): |
| | if num % i == 0: |
| | return False |
| | return True |
| |
|
| | def find_primes_in_range(start: int, end: int) -> List[int]: |
| | """Find all prime numbers in a given range.""" |
| | primes = [] |
| | for num in range(start, end + 1): |
| | if is_prime(num): |
| | primes.append(num) |
| | return primes |
| |
|
| | def calculate_statistics(numbers: List[float]) -> dict: |
| | """Calculate basic statistics for a list of numbers.""" |
| | if not numbers: |
| | return {"error": "Empty list provided"} |
| | |
| | n = len(numbers) |
| | mean = sum(numbers) / n |
| | sorted_nums = sorted(numbers) |
| | |
| | |
| | if n % 2 == 0: |
| | median = (sorted_nums[n//2 - 1] + sorted_nums[n//2]) / 2 |
| | else: |
| | median = sorted_nums[n//2] |
| | |
| | |
| | variance = sum((x - mean) ** 2 for x in numbers) / n |
| | std_dev = math.sqrt(variance) |
| | |
| | return { |
| | "count": n, |
| | "mean": mean, |
| | "median": median, |
| | "min": min(numbers), |
| | "max": max(numbers), |
| | "variance": variance, |
| | "std_deviation": std_dev |
| | } |
| |
|
| | def monte_carlo_pi(iterations: int) -> float: |
| | """Estimate Pi using Monte Carlo method.""" |
| | inside_circle = 0 |
| | |
| | for _ in range(iterations): |
| | x, y = random.random(), random.random() |
| | if x*x + y*y <= 1: |
| | inside_circle += 1 |
| | |
| | return 4 * inside_circle / iterations |
| |
|
| | def process_data_pipeline(data: List[int]) -> dict: |
| | """Complex data processing pipeline demonstrating function calls.""" |
| | |
| | positive_data = [] |
| | for x in data: |
| | if x > 0: |
| | positive_data.append(x) |
| | |
| | |
| | primes_in_data = [] |
| | for x in positive_data: |
| | if is_prime(x): |
| | primes_in_data.append(x) |
| | |
| | |
| | positive_data_floats = [] |
| | for x in positive_data: |
| | positive_data_floats.append(float(x)) |
| | stats = calculate_statistics(positive_data_floats) |
| | |
| | |
| | max_val = max(positive_data) if positive_data else 0 |
| | fib_count = min(max_val, 20) |
| | fib_sequence = fibonacci_sequence(fib_count) |
| | |
| | |
| | small_numbers = [] |
| | for x in positive_data: |
| | if x <= 10: |
| | small_numbers.append(x) |
| | factorials = {} |
| | for x in small_numbers: |
| | factorials[x] = calculate_factorial(x) |
| | |
| | return { |
| | "original_count": len(data), |
| | "positive_count": len(positive_data), |
| | "primes_found": primes_in_data, |
| | "statistics": stats, |
| | "fibonacci_sequence": fib_sequence, |
| | "factorials": factorials, |
| | "pi_estimate": monte_carlo_pi(1000) |
| | } |
| |
|
| | def main(): |
| | """Main function demonstrating the pipeline.""" |
| | sample_data = [1, 2, 3, 5, 8, 13, 21, -1, 0, 17, 19, 23] |
| | result = process_data_pipeline(sample_data) |
| | print(f"Processing complete: {len(result)} metrics calculated") |
| | return result |
| |
|
| | if __name__ == "__main__": |
| | main() |