Skip to content

Commit 188d65c

Browse files
authored
Add custom power and equation functions
Implement custom power and equation functions with call counting.
1 parent 93f8dd6 commit 188d65c

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

‎Week04/functions_ibrahim_as.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
3+
custom_power = lambda x=0, /, e=1: x ** e
4+
5+
def custom_equation(
6+
x: int = 0,
7+
y: int = 0,
8+
/,
9+
a: int = 1,
10+
b: int = 1,
11+
*,
12+
c: int = 1,
13+
) -> float:
14+
"""Calculate (x**a + y**b) / c.
15+
16+
:param x: First base.
17+
:type x: int
18+
:param y: Second base.
19+
:type y: int
20+
:param a: Exponent of x.
21+
:type a: int
22+
:param b: Exponent of y.
23+
:type b: int
24+
:param c: Divisor.
25+
:type c: int
26+
:returns: The result of the equation.
27+
:rtype: float
28+
:raises ZeroDivisionError: If c is zero.
29+
"""
30+
return (x ** a + y ** b) / c
31+
32+
33+
_total_calls = 0
34+
_caller_counts: dict[str, int] = {}
35+
36+
37+
def fn_w_counter() -> tuple[int, dict[str, int]]:
38+
"""Count calls, grouped by the caller's module name."""
39+
global _total_calls
40+
41+
caller_name = sys._getframe(1).f_globals.get("__name__", "<unknown>")
42+
43+
_total_calls += 1
44+
_caller_counts[caller_name] = _caller_counts.get(caller_name, 0) + 1
45+
46+
return _total_calls, _caller_counts.copy()
47+
48+
49+
if __name__ == "__main__":
50+
print(custom_power(2))
51+
print(custom_power(2, 3))
52+
print(custom_power(2, e=2))
53+
54+
print(custom_equation(2, 3))
55+
print(custom_equation(2, 3, 2))
56+
print(custom_equation(2, 3, 2, 3))
57+
print(custom_equation(3, 5, a=2, b=3, c=4))
58+
print(custom_equation(3, 5, 2, b=3, c=4))
59+
print(custom_equation(3, 5, 2, 3, c=4))
60+
61+
for i in range(10):
62+
fn_w_counter()
63+
64+
print(fn_w_counter())

0 commit comments

Comments
 (0)