Skip to content

Commit d419690

Browse files
Add functions for custom power and equation with counter
This file contains a custom power function, a custom equation function with parameter validation, and a function that counts calls to it.
1 parent 71f5b39 commit d419690

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Week04/functions_aysegul_yildiz.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import inspect
2+
3+
4+
# custom_power
5+
custom_power = lambda x=0, /, e=1: x**e
6+
7+
8+
# custom_equation
9+
def custom_equation(
10+
x: int = 0,
11+
y: int = 0,
12+
/,
13+
a: int = 1,
14+
b: int = 1,
15+
*,
16+
c: int = 1,
17+
) -> float:
18+
"""
19+
Calculate a custom equation.
20+
:param x: positional-only integer
21+
:param y: positional-only integer
22+
:param a: positional-or-keyword integer
23+
:param b: positional-or-keyword integer
24+
:param c: keyword-only integer
25+
:return: result of the equation
26+
"""
27+
28+
for name, value in {"x": x, "y": y, "a": a, "b": b, "c": c}.items():
29+
if not isinstance(value, int):
30+
raise TypeError(f"{name} must be int")
31+
32+
return (x**a + y**b) / c
33+
34+
35+
# fn_w_counter
36+
_counter = 0
37+
_callers = {}
38+
39+
40+
def fn_w_counter() -> (int, dict[str, int]):
41+
global _counter # ✅ sadece bu global gerekli
42+
43+
_counter += 1
44+
45+
module_name = inspect.getmodule(fn_w_counter).__name__
46+
_callers[module_name] = _callers.get(module_name, 0) + 1
47+
48+
return _counter, {module_name: _callers[module_name]}

0 commit comments

Comments
 (0)