This repository was archived by the owner on Jun 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmath.py
More file actions
138 lines (115 loc) · 3.35 KB
/
math.py
File metadata and controls
138 lines (115 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import math
from statistics import mean, variance
from datetime import datetime
# Basic arithmetic functions
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def divide(a, b):
if b == 0:
raise ValueError("Division by zero is not allowed")
return a / b
# Factorial using iterative approach
def factorial(n):
if n < 0:
raise ValueError("Negative numbers do not have factorials")
result = 1
for i in range(1, n + 1):
result *= i
return result
# Check if a number is prime
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Fibonacci number generator (nth number)
def fibonacci(n):
if n < 0:
raise ValueError("n must be a non-negative integer")
if n == 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
# Calculator class with memory
class Calculator:
def __init__(self):
self.memory = 0
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
def divide(self, a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def store(self, value):
self.memory = value
def recall(self):
return self.memory
# String manipulation functions
class StringManipulator:
@staticmethod
def reverse_string(s):
return s[::-1]
@staticmethod
def is_palindrome(s):
cleaned = ''.join(c.lower() for c in s if c.isalnum())
return cleaned == cleaned[::-1]
# Data processing class for numerical lists
class DataProcessor:
def __init__(self, data):
if not data:
raise ValueError("Data list cannot be empty")
self.data = data
def get_mean(self):
return mean(self.data)
def get_variance(self):
if len(self.data) < 2:
raise ValueError("At least two data points are required to compute variance")
return variance(self.data)
def normalize(self):
m = self.get_mean()
try:
std_dev = math.sqrt(variance(self.data))
except Exception:
raise ValueError("Could not compute standard deviation")
if std_dev == 0:
raise ValueError("Standard deviation is zero, cannot normalize.")
return [(x - m) / std_dev for x in self.data]
# Date parsing function with a default format
def parse_date(date_str, fmt="%Y-%m-%d"):
try:
return datetime.strptime(date_str, fmt)
except ValueError:
raise ValueError("Incorrect date format, should be YYYY-MM-DD")
# Safe access to list elements with default value
def safe_list_access(lst, index, default=None):
try:
return lst[index]
except IndexError:
return default
# Merge two dictionaries recursively
def merge_dicts(dict1, dict2):
result = dict1.copy()
for key, value in dict2.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = merge_dicts(result[key], value)
else:
result[key] = value
return result